473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

No current record after adding IF THEN ELSE

17
hello guys,

i have made a button in a form (access 2007) for saving a record, the record will be only saved with this button with this command in the buttons onclick event...

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSaveRecord
now i thought, i wanna add a msgbox question like below.

Expand|Select|Wrap|Line Numbers
  1. Dim blnsave As Integer
  2.  
  3. blnsave = MsgBox("Are you sure you want to save this record?", vbYesNo + vbQuestion, "Save Confirmation")
  4.  
  5. If blnsave = vbYes Then
  6.  
  7. DoCmd.RunCommand acCmdSaveRecord
  8.  
  9. Else
  10. Msgbox "Continue working on your record!"
  11. End If
  12.  
so i can say NO, if i pressed the save button to early.
but now every time i press save and say YES, i get the error ...

Run-time error '3021'
No current record.

what did i do wrong here? can somebody help?
Aug 6 '15 #1

✓ answered by jforbes

Are you getting "No Current Record" as an Error or are you getting "Runtime error '2501': The RunCommand action was canceled."?
If it is the second one, you can try this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2.     On Error Resume Next
  3.     DoCmd.RunCommand acCmdSaveRecord 
  4. End Sub
Actually, you can try it anyway. =)

6 2968
jforbes
1,107 Expert 1GB
I'm not sure why you are getting an error. I don't see anything obvious about it.

Although it should work, and you might get it to work, I would approach this differently. You could revert your buttons code back to save and then create something like this for the Before Update Event of your Form and probably get the results you are wanting:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.     If Not (vbYes = MsgBox("Are you sure you want to save this record?", vbYesNo + vbQuestion, "Save Confirmation")) Then Cancel = True
  3. End Sub
The nice thing about doing it this way is that the question will be shown to the user anytime the Record is to be saved (through the Form). Including using Access' native save and by navigating through records.
Aug 6 '15 #2
rod4
17
ok,
i hope i got you right. i changed back the button close onclick event to

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2.  
  3. DoCmd.RunCommand acCmdSaveRecord
  4.  
  5. End Sub
  6.  
and in the forms beforeupdate event i put in your ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2.         If Not (vbYes = MsgBox("Are you sure you want to save this record?", vbYesNo + vbQuestion, "Save Confirmation")) Then Cancel = True
  3. End Sub
  4.  
if thats correct, i get now the "no currrent record"-error when i press NO to the question whether i wanna really save the record. with YES the record is saved correctly and even when i press the normal close button and press NO for not saving the record, it works fine.

i think we are almost there! :)
Aug 7 '15 #3
jforbes
1,107 Expert 1GB
So, everything works except when you use custom button to Save and then Answer No to the prompt to save?
Aug 7 '15 #4
rod4
17
yes! i press the SAVE button and then decide to press NO, because i might forgot something in the record! then i get the error message ...

no current record
Aug 7 '15 #5
jforbes
1,107 Expert 1GB
Are you getting "No Current Record" as an Error or are you getting "Runtime error '2501': The RunCommand action was canceled."?
If it is the second one, you can try this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2.     On Error Resume Next
  3.     DoCmd.RunCommand acCmdSaveRecord 
  4. End Sub
Actually, you can try it anyway. =)
Aug 7 '15 #6
rod4
17
Sorry, yes i mean the runtime error! but not the runtime error 2501 its the runtime error 3021. whatever difference that makes. i will try now the error resume next routine...

and it works fine! :) thanks.

just with a minor change, because i have some msgboxes coming after the SaveRecord.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Score_Click()
  2.  
  3. On Error GoTo GetOut
  4.  
  5. DoCmd.RunCommand acCmdSaveRecord
  6. MsgBox "Your Result is: " & intResult
  7.  
  8. GetOut:
  9.  
  10. End Sub
  11.  
Thanks for your help!!!
Aug 7 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John | last post by:
Hi Is there a way to print/e-mail the current record as it appears on a form? Thanks Regards
1
by: Tony | last post by:
I have a form in access that has 5 fields for a certain record: 1)Date mailed, 2)Date received, 3)Date completed 4)Foreign Content amount and 5)record number. If the foreign content (FC)is >15% of...
2
by: Tony | last post by:
Hello, I am having difficulty in getting the current record of the current form to show after pressing a command button that takes me to another form. The command button takes me to another...
2
by: misscrf | last post by:
I have a search form that is great. I have modified it in such a way, that when search results come up I can bring it back to a useful spot, say an entry form or a report. Here is my lemon (...
8
by: Zlatko Matiæ | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
1
by: 4004 | last post by:
I would like to open a columnar form (so I can see all the details) from a datasheet form (so I can see what is there) but keep the same recordset and current record. I can do the recordset set...
5
by: eighthman11 | last post by:
Hi everyone: This is probably a pretty simple problem but it is driving me nuts. Using Access 2000. I have a continuous form which list several thousand inventory items. The user can enter a...
4
ollyb303
by: ollyb303 | last post by:
Hi, I have a problem I need some help with. In my Access 2000 database (used for logging complaints to my company), I'm using the following code to send an email to our finance dept when a...
0
by: ramachandradurai | last post by:
hi, i am getting the error like.. Either EOF or BOF is true, or the current record has been deleted.Requested operation requires the current data. <%@ LANGUAGE="VBSCRIPT" %>...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.