Results 1 to 12 of 12
  1. #1
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328

    No current recpord. 3021

    I am using a2007 (sometimes2003)

    I would like a form to not be allowed to close if a certain condition exists.

    I tried:


    Private Sub Form_Unload(Cancel As Integer) If alpha>beta then
    msgbox "Message---etc"
    cancel =true
    End Sub

    This works, but I always get the message "No current record 3021". I have looked and looked, but this error seems to be more connected to queries than what I am doing.

    Any help much appreciated.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I assume you're using a bound form (a form tied directly to a table or query) you are likely getting that error when you are already on a 'new' record so you would have to check for the presence of the PK field (assuming it's an autonumber)
    What is the entirety of your code for the unload event because you're definitely missing code which makes what you're trying to do opaque.

  3. #3
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    It is a bound form-query-and does have an autonumber PK field. There are a group of single forms all tied to each customer. Each single form has an autonumber primary key for the object being purchased. I just want the user to be unable to close the form if a certain condition exists.
    I don't understand about being on a "new" record. The user has only the record he is working with on his screen. When he tries to close the form, I would like to say" no-you can't because etc." and then keep the form open until proper changes are made.

    Quote Originally Posted by rpeare View Post
    I assume you're using a bound form (a form tied directly to a table or query) you are likely getting that error when you are already on a 'new' record so you would have to check for the presence of the PK field (assuming it's an autonumber)
    What is the entirety of your code for the unload event because you're definitely missing code which makes what you're trying to do opaque.

  4. #4
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Using the code you've posted, I can't reproduce the error, even while closing on a New Record. By the time you get to the Form_Unload event the last Record you were on has already been saved. Checking for this kind of thing is normally done in the Form_BeforeUpdate event; if you can't save the Record you can't close the Form. Also, if you enter more than one Record, alpha > beta could be true for one Record but not for the last Record entered, and it wouldn't be caught with code in Form_Unload.

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.alpha > Me.beta then
       Msgbox "Message---etc"
       Cancel =True
     End Sub


    If you're running more than one validation you need to add a line to Exit the Sub after a given test fails, otherwise Access will just keep 'dropping through' to the next test:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.alpha > Me.beta then
       Msgbox "Message---etc"
       Cancel =True
       Exit Sub 
     End Sub


    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    There are missing "End If" statements.....

    Form_Unload
    Code:
    Private Sub Form_Unload(Cancel As Integer)
        If alpha > beta Then
            MsgBox "Message---etc"
            Cancel = True
        End If
    End Sub
    Before Update
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Me.alpha > Me.beta Then
            MsgBox "Message---etc"
            Cancel = True
        End If
    End Sub

    I'm just sayin..........

  6. #6
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Sorry I didn't reply sooner-too many other things happening. I tried your code and it works, does just what I need it to do. However, I get this message after I close the generated message:

    "The setting you entered isn't valid for this property."

    The error message doesn't include an error number. I googled it, but, unlike this great site, it is hard to make sense. of dialogues.

    Is there some way to get rid of the error message?

    Here is actual code I used on a simple form as a test. There are about 100 records in the source table It does have an autogenerated primary key.

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.city = "xxx" Then
    MsgBox "Message---etc"
    Cancel = True
    Exit Sub
    End If
    End Sub

  7. #7
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by ssanfu View Post
    There are missing "End If" statements.....

    I'm just sayin..........
    Sloppy copy & pasting!

    Thanks for the assist, Steve!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    Typos abound. I caught the missing "end if", no big deal. Glad to see that others do it. Did you get a chance to look at the cause of the error message.?

  9. #9
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Error 3021 is one of the most elusive errors to figure out in Access. When looping through records to accomplish a task it usually means that you've moved beyond the end of the recordset, and the fix is to check for EOF (End of File) before attempting the operation. At times it can be that you are attempting to do something to record that hasn't been saved, as rpeare suggested. At other times the root cause is never found, and the solution is to simply trap the error.

    My guess here, and it is just a guess, is that by the time you reach the Form_Unload event, there is, indeed, no current record!

    The workaround here, as I said, is to place your code in the Form_BeforeUpdate event. Since the code after closing the Messagebox

    Cancel = True
    Exit Sub


    doesn't actually 'enter a setting for any property,' I have no where it's coming from. Sadly, the error messaging part of Access is probably one of its weakest components, second only to its 'help file;' error messages frequently have little to do with the actual error. My guess, here, would be that it is actually coming from somewhere else in your code...or you're dealing with corruption.

    If you'd care to remove any confidential data, save a copy of the file and zip it up ans attach it to a post, I'd be happy to take a look at it. Saving it in a 2003 file format would allow more members to open it an take a look. Also include instructions as to the form involved.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    I think corruption is what it is. I tried to setting up a copy to send you and it now does nada. Ignores the code. Not even an error message. Sometimes I hate Access. I will reload program and try again. Thanks so much. I always appreciate very much the time you folks donate to us minor leaguers. It wouldn't be possible to do projects without you.

  11. #11
    gg80 is offline Competent Performer
    Windows 7 Access 2007
    Join Date
    Jul 2010
    Posts
    328
    I inserted cd and tried running the repair feature. BeforeUnload now works! Just as it did the first time you tried it o your computer. So, there was a corruption. Thanks much for all the help and for describing what is going on underneath when you use these "Before" functions. Help screens just don't always do that.

  12. #12
    Missinglinq's Avatar
    Missinglinq is online now VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad you got it working!

    Good luck with your project!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. sql error . runtime 3021 - no current record
    By princess12 in forum Access
    Replies: 3
    Last Post: 04-10-2015, 09:26 AM
  2. 3021 Error
    By RonL in forum Programming
    Replies: 6
    Last Post: 05-10-2013, 03:45 PM
  3. Error 3021 no current record
    By bbrazeau in forum Programming
    Replies: 10
    Last Post: 12-13-2012, 04:22 PM
  4. Error 3021
    By Marianna_Air in forum Forms
    Replies: 27
    Last Post: 08-20-2012, 01:13 PM
  5. Replies: 8
    Last Post: 05-16-2011, 06:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums