handle error in save button because of (docmd.Requery) | Newbie | | Join Date: Oct 2009
Posts: 15
| |
hi, I am developing a form for adding employees where you can find three fields in the table employees (firstname, middlename, lastname, hiredate (Is Not Null)
and ADezii prompt me to use this code and it gives me the msgboxes which I need to be viewed to the user but after that It gives me an error because the code which I used for save cmdbutton is - private sub save_click()
-
docmd.save
-
docmd.requery (the error because of this I think)
-
end sub
- Private Sub Form_BeforeUpdate(Cancel As Integer)
-
If IsNull(Me![firstname]) Then
-
MsgBox "You must enter a First Name before this Record can be saved"
-
Cancel = True: Me![firstname].SetFocus
-
ElseIf IsNull(Me![middlename]) Then
-
MsgBox "You must enter a Middle Name before this Record can be saved"
-
Cancel = True: Me![middlename].SetFocus
-
ElseIf IsNull(Me![lastname]) Then
-
MsgBox "You must enter a Last Name before this Record can be saved"
-
Cancel = True: Me![lastname].SetFocus
-
ElseIf IsNull(Me![hiredate]) Then
-
MsgBox "You must enter a Hire Date before this Record can be saved"
-
Cancel = True: Me![hiredate].SetFocus
-
End If
-
End Sub
please help me handling this problem because I want to make a form for adding employees without any error
thanks in advance for any help you may provide me
| |
best answer - posted by topher23 |
Okay, this code works in the OnClick event of your save button without issue. I added 3 different Star Wars characters in a row with it. - Private Sub Save_Click()
-
On Error GoTo Err_Save_Click
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
-
Exit_Save_Click:
-
Exit Sub
-
Err_Save_Click:
-
MsgBox Err.Description
-
Resume Exit_Save_Click
-
End Sub
-
Also, your "Close" button is hiding behind the tab control. As soon as you click on a text box, you lose the button, but if you click where it "should" be, it closes the form. To fix this, open the form in design view, selec the Close button and go to Format > Move To Front on the Menu bar.
Let us know if you have more issues.
|  | Expert | | Join Date: Jan 2008 Location: Sydney
Posts: 790
| | | re: handle error in save button because of (docmd.Requery)
you don't tell us what the error is ???
Perhaps using might solve it.
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery)
Mseo,
It's really no good at all posting in code that you write out freestyle. This wastes everybody's time and energy.
When posting any code on here please : - Ensure you have Option Explicit set (See Require Variable Declaration).
- Try to compile it. If it doesn't compile for any reason please explain that clearly - including the error message and which line of your code it appears on. Compilation is done from the Visual Basic Editor menu - Debug \ Compile Project (Where Project is the actual name of your project).
- Copy your code (using the Clipboard - Cut / Copy / Paste) from your project directly into your post. Typing in code is not appreciated as it is likely to introduce typos which cause members to waste their time unnecessarily.
- Ensure that the code in your post is enveloped within CODE tags. The hash (#) button in the posting page helps with this. Simply select your code and click on the hash button to have it enveloped automatically.
If all these points are covered then all members will be better able to understand, and therefore attempt to answer, your question.
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery)
When you've posted the code properly, you will need to include exactly which line caused the error message (Don't just guess. Find out before posting.) and post the error message itself. Error messages are there for a reason. They are there to help people determine what the problem is and fix it. Posting the problem without the message is just wasting people's time.
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
the error is
3021 No Current record
comes from this line (Me.Requery)
|  | Expert | | Join Date: Jan 2008 Location: Sydney
Posts: 790
| | | re: handle error in save button because of (docmd.Requery)
The error suggests that it might be coming from the previous line
The reason I say that is that the error suggests that an operation (save) on a record is being attempted but there is no record for that operation to work on.
Not sure why that is, and there is not much info to go off.
The form is bound to a table .... right ?
Or if you are binding the form to a query...it is an updatable query?
Sometimes when creating queries, they can become unable to make updates because of the way they are constructed. You can tell if you run the query directly. The create new record button will be greyed out
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
It looks to me like you may not understand how to use the right methods of the DoCmd object. The method you're using (DoCmd.Save) will save the current form, not the data in the form. You should use - DoCmd.RunCommand acCmdSaveRecord
to save the record, then Me.Requery will probably work correctly.
Also, it is helpful to know which version of Access you're using (2000, 2003, 2007, etc.) so we can help you better.
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery) Quote:
Originally Posted by mseo the error is
3021 No Current record
comes from this line (Me.Requery) It's so tedious writing the same thing twice.
Please refer back to post #5 : - Post the code properly.
- Indicate exactly the correct line the error was triggered by (Don't just guess. Find out before posting.) Clearly not the one you claim.
The error message is correct at least.
|  | Expert | | Join Date: Jan 2008 Location: Sydney
Posts: 790
| | | re: handle error in save button because of (docmd.Requery)
Another point to consider.
Microft has for a long long time (since 1998 I think) been discouraging the use of docmd stating that it will eventually become obsolete and removed.
Yea right, 11 years later its still there.
Anyway, I hardly ever use it anymore
about the only thing I ever use it for is
Oh, and I agree with Neopa whole heartedly
Its very difficult to help you without those basic steps on your part.
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery) Quote:
Originally Posted by Delerna Microft has for a long long time (since 1998 I think) been discouraging the use of docmd stating that it will eventually become obsolete and removed.
Yea right, 11 years later its still there. Hah! Yeah, when I was upgrading to 2000, I was told that MS was "deprecating" the DoCmd object, and that I should start using macros instead. Yeah, right (I was programming in BASIC almost before I was out of diapers, what do you mean, macros?!). Yet, with 2007, they've added 13 new methods to the DoCmd object and there is no mention on any MS resource of any plans to get rid of it. I'd say it's safe as long as there are pure programmers out there who like to avoid macros whenever possible.
Sorry, don't wanna hijack the thread. mseo, any news or progress?
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery)
There's only so much a software provider can do as far as removing facilities goes from a popular product.
Unfortunately, they seem to have found a way to make pure coders suffer with the security difficulties they've put in the way of allowing code to run. This is part of their push to encourage the usage of macros (It obviously makes life much easier for them). Let's hope they continue to find it unprofitable to emasculate the code side of things - otherwise there would really be no reason not to switch to OpenOffice.
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
hi,
I didn't intend to make duplicate thread but i don't know how to follow up or submit more detail about my question
I used this - RunCommand acCmdSaveRecord
in stead of
but get two error
1- run-time error 2046 (the command or action "saverecord" isn't available now
2- if the Id generated and I press save I get error 3021 (no current record)
thanks
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery)
I will move this for you now, but each post of each thread has a link (bottom right of the post) to Quote that post and include it in a reply, which is appended to the end of the thread. Also, each thread has a Quick Reply section where you can type in what you want and click on the button to submit it.
I suggest also, that you read all the responses you get and respond to them. I have two posts in your thread that instruct you (not a request - this is an instruction I am giving you as an administrator) on what you must do before continuing to ask for more help. Responding to this is not optional. You must do so or receive an infraction for breaking the rules.
Whether you respond to other posts is a matter of good manners. We prefer you to show that, but not doing so is not something we discipline for.
If there is any point (like here where you weren't able to follow the instructions without further explanation) that you are confused about then please just say so and I will endeavour to explain more clearly.
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery) Quote:
Originally Posted by NeoPa I have two two posts in your thread... Is that twenty-two or two-plus-two? = )
I'm getting to the point where I'm wondering what's going on behind this form, as there appear to be some real problems here.
mseo, you may want to spend some time studying the "Northwind.mdb" database that ships with Access. If you don't have that on your system, you can download the Access 2000 version (the only version available for download) at http://www.microsoft.com/downloads/d...displaylang=EN |  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery) Quote:
Originally Posted by topher23 Is that twenty-two or two-plus-two? = ) Good spot. Fixed now thanks.
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
the version of microsoft access is 2003
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
hi,
this code for save button for handling the 3021 error and any other error which happen during the save action - Private Sub Save_Click()
-
On Error GoTo Err_Save_Click
-
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
-
Exit_Save_Click:
-
Exit Sub
-
Err_Save_Click:
-
MsgBox Err.Description, vbDefaultButton1 + vbCritical + vbOKOnly, "Hi-Tech SysAdmin"
-
Resume Exit_Save_Click
-
End Sub
but when I click save the command excutes the save command but I need to set the form to new record after saving the previous one but using the same code
the second question is how can attach ms-access in here?
thank you
|  | Expert | | Join Date: Jan 2008 Location: Sydney
Posts: 790
| | | re: handle error in save button because of (docmd.Requery)
something like -
DoCmd.gotoRecord acNewRecord
-
which would also save any unsaved changes to the current record before moving on to the new record....if memory serves me correctly
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
Delerna's almost got it. In specific, its - DoCmd.GoToRecord , , acNewRec
However, you can find that easily in the help file, or as an answer on countless threads spread across the Internet. Really, it would be so much better for you, mseo, if you'd look for the answer yourself before posting it up as a question.
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
As for the second question about attaching your database, see these guidelines by NeoPa, then click "Manage Attachments" in the "Additional Options" section of the advanced reply page. From there, you should be able to handle it. Quote:
Originally Posted by NeoPa When attaching your work please follow the following steps first : 1. Remove anything not relevant to the problem. This is not necessary in all circumstances but some databases can be very bulky and some things do not effect the actual problem at all. 2. Likewise, not entirely necessary in all cases, but consider saving your database in a version not later than 2003 as many of our experts don't use Access 2007. Largely they don't want to, but some also don't have access to it. Personally I will wait until I'm forced to before using it. 3. If the process depends on any linked tables then make local copies in your database to replace the linked tables. 4. If you've done anything in steps 1 to 3 then make sure that the problem you're experiencing is still evident in the updated version. 5. Compile the database (From the Visual Basic Editor select Debug / Compile {Project Name}). 6. Compact the database. 7. Compress the database into a ZIP file. 8. When posting, scroll down the page and select Manage Attachments (Pressing on that leads you to a page where you can add or remove your attachments. It also lists the maximum file sizes for each of the allowed file types.) and add this new ZIP file.
It's also a good idea to include some instructions that enable us to find the issue you'd like help with. Maybe some instructions of what to select, click on, enter etc that ensures we'll see what you see and have the same problems. | | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
hi, the problem is not about saving the uncurrent changes before moving to new record but it is all about how to move to the next record, excuse me because this code must be included within the previous code I posted and I really tried this code
and nothing changed the same thing appears for me the save record in unavailable error number 3046
and I didn't mean to tease anyone but I am in real problem so I want attach the database form to see the real problem yourselves.
thanks
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
hi,
I attached the form which I have the problem with
please try to help me handling it
thanks
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
Okay, this code works in the OnClick event of your save button without issue. I added 3 different Star Wars characters in a row with it. - Private Sub Save_Click()
-
On Error GoTo Err_Save_Click
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
-
Exit_Save_Click:
-
Exit Sub
-
Err_Save_Click:
-
MsgBox Err.Description
-
Resume Exit_Save_Click
-
End Sub
-
Also, your "Close" button is hiding behind the tab control. As soon as you click on a text box, you lose the button, but if you click where it "should" be, it closes the form. To fix this, open the form in design view, selec the Close button and go to Format > Move To Front on the Menu bar.
Let us know if you have more issues.
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
Slight modification to the code. This makes it so that when the update is cancelled because the user didn't type in everything, it doesn't give them a "RunCommand has been cancelled." dialog box by telling the error handler not to display anything when that specific error code pops up. - Private Sub Save_Click()
-
On Error GoTo Err_Save_Click
-
DoCmd.RunCommand acCmdSaveRecord
-
DoCmd.GoToRecord , , acNewRec
-
Exit_Save_Click:
-
Exit Sub
-
Err_Save_Click:
-
Select Case Err
-
Case 2046, 2501
-
'do nothing
-
Case Else
-
MsgBox Err & ", " & Err.Description
-
End Select
-
Resume Exit_Save_Click
-
End Sub
-
| | Newbie | | Join Date: Oct 2009
Posts: 15
| | | re: handle error in save button because of (docmd.Requery)
thanks topher23 for your answer it is the really best answer
but I get error msg for error 3021
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,730
| | | re: handle error in save button because of (docmd.Requery)
Mseo - Please check your PMs.
I need a confirmation of receipt from you when you've read it please.
| | Expert | | Join Date: Oct 2008 Location: Cedar City, Utah, USA
Posts: 100
| | | re: handle error in save button because of (docmd.Requery)
I spent a bit more time with your form, and the only way was able to replicate a 3021 error was when I tried to save the record without filling out all of the required fields.
If an error is being generated and you know you can just ignore it because it's not really an error, add it to the Case statement from the last post: Quote:
Originally Posted by topher23 So it looks like
The only problem with this is that sometimes you might ignore a legitimate error, so it's best to make sure that the only way that specific error is generated is in a way that is safe to be ignored.
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|