Results 1 to 15 of 15
  1. #1
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93

    Form Reference in VBA?

    Accessors,



    Hello- I am attempting to have Access 'uncheck' a checkbox before the database closes. I am using this code in a close event of a form.

    Code:
    Private Sub Form_Close()    frmAutoAllOut!chkLogOutAllUsers = False
    End Sub
    However, the error keeps saying "Compile error: Variable not defined". How do I define this kind of variable?

    Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    I am still struggling- I have attempted altering the line to those suggested on the list pbaldy gave. Hmm...

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Uh, like which specifically? You can use the "Me" reference if the check box is on the same form, otherwise "Forms!..."
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    I have tried to put in:

    Forms!frmAutoAllOut!chkLogOutAllUsers = False
    Forms!frmAutoAllOut.chkLogOutAllUsers = False
    Forms!frmAutoAllOut!chkLogOutAllUsers.Enabled = False

    All to no luck!!!

  6. #6
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    The line is referring to a separate form with the checkbox on it. Maybe it should refer to the field of the checkbox in the table instead...? I am not sure how to write the referring line for a checkbox field in a table...

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would expect this to work:

    Forms!frmAutoAllOut.chkLogOutAllUsers = False

    Do you get an error or what?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    I got an error on each of the lines I have attempted above, including yours.

    I even double-checked the spelling!

    Odd!

  9. #9
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    do I refer to the name of the command or the control source of the command in the form?

  10. #10
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    The line is referring to a separate form with the checkbox on it.
    Is this form open ? I am asking this because the syntax given by Paul should work.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    What error do you get (the description)? You'd refer to the checkbox, as you appear to be doing. As amrut noted, the form does have to be open.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    It now works when the referred form is open. -___- Is there anyway I can refer it to the form while it is closed? Or would referring to a table the referred form's checkbox is referring to be a better method?

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No, you can't refer to a form if it's closed. You could use a recordset or update query to update the table directly. Presumably either would a criteria to update the appropriate record(s).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    excellenthelp is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Mar 2014
    Posts
    93
    pbaldy,

    Thanks! I figured a way to do this- it sure does cost a minor fraction of the machine's speed but at least it gets the job done. I made a form that will stay permanently hidden and the code is written below to its close event:

    Code:
    Option Compare DatabaseOption Explicit
    
    
    Private Sub Form_Close()
        DoCmd.OpenForm "frmAutoAllOut"
        Forms!frmAutoAllOut.chkLogOutAllUsers = False
        DoCmd.Close acForm, "frmAutoAllOut"
    End Sub
    Marking this as SOLVED and pbaldy, I am giving you a rep point. =D

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! As you said, not as efficient but certainly will work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Reference recordset of different form
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 07-09-2013, 10:35 AM
  2. Sub-form reference incorrect?
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 11-30-2012, 06:45 PM
  3. VBA Code to reference Form Image
    By jondavidf in forum Reports
    Replies: 1
    Last Post: 07-27-2012, 12:34 PM
  4. Control Reference on a Form
    By gmitchell@exbrief.com in forum Forms
    Replies: 5
    Last Post: 01-24-2011, 12:31 PM
  5. Form Name Reference in VBA
    By Simon Sweet in forum Programming
    Replies: 0
    Last Post: 05-22-2008, 01:55 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