Solved Working with Global Variables (1 Viewer)

pooldead

Registered User.
Local time
Yesterday, 19:22
Joined
Sep 4, 2019
Messages
136
I have a variable (strQry) in Form A. I am trying to declare it globally so that I can use it in Form B. However, when I compile, I get the "Variable not defined" error on the Form B sub at the "strQry" variable.
Code:
'FORM A'
Option Explicit
Option Compare Database
Public strQry As String

Public Sub cmdPW180_Click()
    
    DoCmd.OpenForm "frm_SA-Review"
    strQry = "SELECT samAccountName, pwdLastSet, lastLogonTimestamp, swhEmployeeStatus FROM SA_Master WHERE pwdLastSet <= DateAdd('d',-180,Now()) ORDER BY samAccountName"
    Forms![frm_SA-Review]!lboxData.RowSource = strQry

End Sub
Code:
'FORM B - frm_SA-Review'
Public Sub cmdExport_Click()

    Dim fileName As String
    
    fileName = "ServiceAccount_QueryResults_" & Format(Now(), "dd-mm-yy") & ".pdf"
    DoCmd.OutputTo acOutputQuery, strQry, acFormatPDF, fileName

End Sub
 

Isaac

Lifelong Learner
Local time
Yesterday, 19:22
Joined
Mar 14, 2017
Messages
8,774
Here is a method that would work:

1. declare a global variable inside a regular Module, near the top. (I keep globals in their own module for ease). global varname as type
2. in form A's code, assign that variable a value. code varname=value
3. in form B's code, freely refer to varname
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 19:22
Joined
Oct 29, 2018
Messages
21,453
Hi. If you want to use global variables, you'll have to declare them in a Standard Module. As an alternative, you can try using TempVars.

Edit: Oops, too slow...
 

pooldead

Registered User.
Local time
Yesterday, 19:22
Joined
Sep 4, 2019
Messages
136

Thank you both!
 

Users who are viewing this thread

Top Bottom