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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mseo
    New Member
    • Oct 2009
    • 183

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

    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

    Code:
    private sub save_click()
    docmd.save
    docmd.requery  (the error because of this I think)
    end sub
    Code:
    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
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    you don't tell us what the error is ???

    Perhaps using

    me.requery
    might solve it.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32668

      #3
      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.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32668

        #4
        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.

        Comment

        • mseo
          New Member
          • Oct 2009
          • 183

          #5
          the error is
          3021 No Current record
          comes from this line (Me.Requery)

          Comment

          • Delerna
            Recognized Expert Top Contributor
            • Jan 2008
            • 1134

            #6
            The error suggests that it might be coming from the previous line
            Code:
            docmd.save
            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

            Comment

            • topher23
              Recognized Expert New Member
              • Oct 2008
              • 234

              #7
              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
              Code:
              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.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32668

                #8
                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 :
                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.

                Comment

                • Delerna
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1134

                  #9
                  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
                  Code:
                  docmd.SetWarnings
                  Oh, and I agree with Neopa whole heartedly
                  Its very difficult to help you without those basic steps on your part.

                  Comment

                  • topher23
                    Recognized Expert New Member
                    • Oct 2008
                    • 234

                    #10
                    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 "deprecatin g" 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?

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32668

                      #11
                      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.

                      Comment

                      • mseo
                        New Member
                        • Oct 2009
                        • 183

                        #12
                        Save cmdbutton code

                        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
                        Code:
                        RunCommand acCmdSaveRecord
                        in stead of
                        Code:
                        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

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32668

                          #13
                          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.
                          Last edited by NeoPa; Nov 3 '09, 04:58 PM. Reason: Removed the double "two"

                          Comment

                          • topher23
                            Recognized Expert New Member
                            • Oct 2008
                            • 234

                            #14
                            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

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32668

                              #15
                              Originally posted by topher23
                              Is that twenty-two or two-plus-two? = )
                              Good spot. Fixed now thanks.

                              Comment

                              Working...