473,396 Members | 1,707 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,396 software developers and data experts.

handle error in save button because of (docmd.Requery)

mseo
181 100+
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

Expand|Select|Wrap|Line Numbers
  1. private sub save_click()
  2. docmd.save
  3. docmd.requery  (the error because of this I think)
  4. end sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer) 
  2. If IsNull(Me![firstname]) Then 
  3.   MsgBox "You must enter a First Name before this Record can be saved" 
  4.     Cancel = True: Me![firstname].SetFocus 
  5. ElseIf IsNull(Me![middlename]) Then 
  6.   MsgBox "You must enter a Middle Name before this Record can be saved" 
  7.     Cancel = True: Me![middlename].SetFocus 
  8. ElseIf IsNull(Me![lastname]) Then 
  9.   MsgBox "You must enter a Last Name before this Record can be saved" 
  10.     Cancel = True: Me![lastname].SetFocus 
  11. ElseIf IsNull(Me![hiredate]) Then 
  12.   MsgBox "You must enter a Hire Date before this Record can be saved" 
  13.     Cancel = True: Me![hiredate].SetFocus 
  14. End If 
  15. 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
Nov 1 '09 #1

✓ answered 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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. On Error GoTo Err_Save_Click
  3.     DoCmd.RunCommand acCmdSaveRecord
  4.     DoCmd.GoToRecord , , acNewRec
  5. Exit_Save_Click:
  6.     Exit Sub
  7. Err_Save_Click:
  8.     MsgBox Err.Description
  9.     Resume Exit_Save_Click
  10. End Sub
  11.  
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.

26 6864
Delerna
1,134 Expert 1GB
you don't tell us what the error is ???

Perhaps using

me.requery
might solve it.
Nov 1 '09 #2
NeoPa
32,556 Expert Mod 16PB
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 :
  1. Ensure you have Option Explicit set (See Require Variable Declaration).
  2. 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).
  3. 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.
  4. 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.
Nov 1 '09 #3
NeoPa
32,556 Expert Mod 16PB
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.
Nov 1 '09 #4
mseo
181 100+
the error is
3021 No Current record
comes from this line (Me.Requery)
Nov 2 '09 #5
Delerna
1,134 Expert 1GB
The error suggests that it might be coming from the previous line
Expand|Select|Wrap|Line Numbers
  1. docmd.save
  2.  
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
Nov 2 '09 #6
topher23
234 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 2 '09 #7
NeoPa
32,556 Expert Mod 16PB
@mseo
It's so tedious writing the same thing twice.

Please refer back to post #5 :
  1. Post the code properly.
  2. 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.
Nov 2 '09 #8
Delerna
1,134 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. docmd.SetWarnings
  2.  
Oh, and I agree with Neopa whole heartedly
Its very difficult to help you without those basic steps on your part.
Nov 2 '09 #9
topher23
234 Expert 100+
@Delerna
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?
Nov 2 '09 #10
NeoPa
32,556 Expert Mod 16PB
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.
Nov 3 '09 #11
mseo
181 100+
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
Expand|Select|Wrap|Line Numbers
  1. RunCommand acCmdSaveRecord 
in stead of
Expand|Select|Wrap|Line Numbers
  1. docmd.save
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
Nov 3 '09 #12
NeoPa
32,556 Expert Mod 16PB
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.
Nov 3 '09 #13
topher23
234 Expert 100+
@NeoPa
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
Nov 3 '09 #14
NeoPa
32,556 Expert Mod 16PB
@topher23
Good spot. Fixed now thanks.
Nov 3 '09 #15
mseo
181 100+
the version of microsoft access is 2003
Nov 3 '09 #16
mseo
181 100+
hi,
this code for save button for handling the 3021 error and any other error which happen during the save action

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. On Error GoTo Err_Save_Click
  3.     DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  4. Exit_Save_Click:
  5.     Exit Sub
  6. Err_Save_Click:
  7.     MsgBox Err.Description, vbDefaultButton1 + vbCritical + vbOKOnly, "Hi-Tech SysAdmin"
  8.     Resume Exit_Save_Click
  9. 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
Nov 4 '09 #17
Delerna
1,134 Expert 1GB
something like

Expand|Select|Wrap|Line Numbers
  1. DoCmd.gotoRecord acNewRecord
  2.  
which would also save any unsaved changes to the current record before moving on to the new record....if memory serves me correctly
Nov 4 '09 #18
topher23
234 Expert 100+
Delerna's almost got it. In specific, its

Expand|Select|Wrap|Line Numbers
  1. 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.
Nov 4 '09 #19
topher23
234 Expert 100+
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. @NeoPa
Nov 4 '09 #20
mseo
181 100+
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
Expand|Select|Wrap|Line Numbers
  1. docmd.gotorecord,, acrec
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
Nov 4 '09 #21
mseo
181 100+
hi,
I attached the form which I have the problem with
please try to help me handling it
thanks
Nov 4 '09 #22
topher23
234 Expert 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. On Error GoTo Err_Save_Click
  3.     DoCmd.RunCommand acCmdSaveRecord
  4.     DoCmd.GoToRecord , , acNewRec
  5. Exit_Save_Click:
  6.     Exit Sub
  7. Err_Save_Click:
  8.     MsgBox Err.Description
  9.     Resume Exit_Save_Click
  10. End Sub
  11.  
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.
Nov 4 '09 #23
topher23
234 Expert 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Save_Click()
  2. On Error GoTo Err_Save_Click
  3.     DoCmd.RunCommand acCmdSaveRecord
  4.     DoCmd.GoToRecord , , acNewRec
  5. Exit_Save_Click:
  6.     Exit Sub
  7. Err_Save_Click:
  8.     Select Case Err
  9.     Case 2046, 2501
  10.         'do nothing
  11.     Case Else
  12.         MsgBox Err & ", " & Err.Description
  13.     End Select
  14.     Resume Exit_Save_Click
  15. End Sub
  16.  
Nov 4 '09 #24
mseo
181 100+
thanks topher23 for your answer it is the really best answer
but I get error msg for error 3021
Nov 7 '09 #25
NeoPa
32,556 Expert Mod 16PB
Mseo - Please check your PMs.

I need a confirmation of receipt from you when you've read it please.
Nov 8 '09 #26
topher23
234 Expert 100+
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:

@topher23
So it looks like

Expand|Select|Wrap|Line Numbers
  1.     Case 2046, 2501, 3021
  2.  
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.
Nov 9 '09 #27

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

Similar topics

8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
2
by: ColinWard | last post by:
whenever I run the following code I get an error saying that 'Trade Show and Conference Contacts database could not find the form 'Events_Form' referred to in a macro or in Visual Basic Code. The...
2
by: Ronny Sigo | last post by:
Hello all, I have made a form with a button to delete the current record. When clicking the button, I get a errmsg "Reserved error" I don't know what this means. And even though I have put...
0
by: Jamey | last post by:
I perused old posts for an answer to this for at least an hour, and I've found a work-around, but no definitive answer. Synopsis of the problem: On NotInList or ctl.Requery commands where a...
4
by: WJA | last post by:
I'm probably missing something here but I can't understand the following. When 2 users try to save a record with the same primary key (a number field), the first record saves as expected, but the...
19
by: sara | last post by:
I am getting "Type Mismatch Error" when the following code executes. I am trying to notify the user if she attempts to add a customer with the same FirstName, LastName, Address(line1) and City as...
22
by: Br | last post by:
First issue: When using ADPs you no longer have the ability to issue a me.refresh to save the current record on a form (the me.refresh does a requery in an ADP). We usually do this before...
7
by: Mathew Butler | last post by:
I'm investigating an issue I have when pulling data back from MS access I'm retrieving all rows from a column in a table of 5000 rows - accessing a column of type "memo" ( can be 65353 character...
1
by: resqtech | last post by:
I am having an issue with an Access Database that worked at one time and after a Windows Update that screwed up the profile it stopped working. The following is what VB is stating causes the error. ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.