global variables lose values

cyberpac9

Registered User.
Local time
Yesterday, 20:14
Joined
Jun 6, 2005
Messages
70
i have a form (frm1) and subform (sfrm1) that when a room number is double-clicked in the subform another form (frm2) and subform (sfrm2) loads. frm1 has building info and frm2 is used for inspection info. if a room has never been inspected then when frm2 loads the field [room] will be empty. however, after the first inspection the room number is obviously entered for frm2.

so i have created a global variable that holds the value of [lab_room] from frm1 and is supposed to use this value on frm2 if the room has never been inspected - ie, [room] is null. however, the value of intRoom (global variable) is lost after is leaves frm1 and goes to frm2. below is my code:

Code:
[B]modGlobal (module with global variables declared)[/B] 
Option Compare Database

Global intVar As Variant
Global intRoom As String

Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGuid) As Long

Private Type GUID 'Memory structure used by CoCreateGuid
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type

Private Const S_OK = 0 'Return value from CoCreateGuid

[B]frm1 subform double click event[/B] 
Private Sub Form_DblClick(Cancel As Integer)
On Error GoTo Err_DblClick

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim stMsg As String
    
    'if the user double clicks on an area that has no lab_id associated with it
    'prevents a null value for lab_id
    If IsNull(Me!lab_id) Then
        stMsg = "There is no lab associated with this room."
        intRes = MsgBox(stMsg, vbOKOnly + vbExclamation, "Error - No Lab Selected")
        GoTo Exit_DblClick
    End If

    'opens lab forms associated with this lab_id
    intVar = Me!lab_id
    intRoom = Me!lab_room
    stDocName = "ehs_lab_safety_surveys"
    stLinkCriteria = "[lab_id]=" & "'" & intVar & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    [Forms]![ehs_lab_safety_surveys].Form.RecordsetClone.Find ("lab_id = '" & intVar & "'")
    
    DoCmd.Close acForm, "copy_lab_search_by_bldg"

Exit_DblClick:
    Exit Sub

Err_DblClick:
    MsgBox Err.Description
    Resume Exit_DblClick
End Sub

[B]frm2[/B] 
Private Sub Form_Load()

    Dim strNewguid As String  
    
    'checks for null values of lab_id, room, id and safety_survey_date. if they are
    'null or empty a value is inserted.
    
    If IsNull(Me!lab_id) Or (Me!lab_id = "") Then
        Me!lab_id = intVar
    End If
    
    If IsNull(Me!room) Then
        Me!room.Value = intRoom
    End If
    
    If IsNull(Me!safety_survey_date) Then
        Me!safety_survey_date = Date
    End If
    
    If IsNull(Me!id) Or (Me!id = "") Then
        Set x = CreateObject("Scriptlet.TypeLib")
        strNewguid = Left(x.GUID, 38)
        Me!id.Value = strNewguid
    End If
       
    DoCmd.Maximize

End Sub

can anyone see why the value is being lost from frm1 to frm2? i'm ripping my hair out trying to figure this one out....
 
Global variables need to be defined in standard code modules if you want them to be available throughout the application. I create a separate module that is used only for defining global variables to make them easy to find.
 
have you checked that it really is null?

maybe something like
if isnull(intRoom) then
msgbox "intRoom = Null!"
else
msgbox " intRoom = " & Introom
end if

Put at the end of the first sub and the start of the second may help pin down what is going on.

Peter
 
Pat:
i'm not sure i completely see what you're saying. modGlobal is a module that contains the global variables. it also contains the script for creating GUID. are you saying i should move the GUID script somewhere else and leave modGlobal with only the global variables?

Bat17:
i have not used your technique but i debugged my forms. as i step through the procedures i analyze the variables. they are always null (always null on frm2 - frm1 obviously has the value since that's where the value is first declared).

let me throw this out there to just completely mess you guys up. :D
i created the forms in a file called test.adp and tested the forms. after they worked perfectly, i imported them to master.adp and now they are broken. i changed nothing, did nothing to them, just imported them and now they are not working. does that tell you anything?

thanks for your responses...please keep them coming...
 
Sorry, I saw the form_ events and assumed your code was in the form's class module.

I had some trouble with global variables and it turns out that certain types of errors cause Access to loose global values. I think it doesn't matter if they are trapped or not. I ended up creating a hidden form that I open when the db opens and I use this form to hold any application variables. This turned out to be quite helpful because while you are developing, you can leave the form visible so you can always see the values in your variables.
 
Another observation is that you do briefly have both forms open at the same time. During this period you can transfer your data directly...

Code:
[B]frm1 subform double click event [/B]
Private Sub Form_DblClick(Cancel As Integer)
On Error GoTo Err_DblClick

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim stMsg As String
    
    'if the user double clicks on an area that has no lab_id associated with it
    'prevents a null value for lab_id
    If IsNull(Me!lab_id) Then
        stMsg = "There is no lab associated with this room."
        intRes = MsgBox(stMsg, vbOKOnly + vbExclamation, "Error - No Lab Selected")
    Else
        'opens lab forms associated with this lab_id
        stDocName = "ehs_lab_safety_surveys"
        stLinkCriteria = "[lab_id]=" & "'" & intVar & "'"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
[COLOR=Green]        'call Public FormSetup() on frm2 and pass in the parameters here[/COLOR]
[COLOR=DarkRed]        Forms(strDocName).FormSetup(Me.lab_id, Me.lab_room)[/COLOR]
        DoCmd.Close acForm, "copy_lab_search_by_bldg"
    End If
    
Exit_DblClick:
    Exit Sub

Err_DblClick:
    MsgBox Err.Description
    Resume Exit_DblClick
End Sub

[B]frm2 [/B]
[COLOR=DarkRed]Public Sub FormSetup(intVar As Variant, intRoom as String)[/COLOR]
[COLOR=Green]'   This routine, declared as public, is now run from form1 with parameters
'   passed in directly.
[/COLOR]
    Dim strNewguid As String  

    
    'checks for null values of lab_id, room, id and safety_survey_date. if they are
    'null or empty a value is inserted.
    
    If IsNull(Me!lab_id) Or (Me!lab_id = "") Then
        Me!lab_id = intVar
    End If
    
    If IsNull(Me!room) Then
        Me!room.Value = intRoom
    End If
    
    If IsNull(Me!safety_survey_date) Then
        Me!safety_survey_date = Date
    End If
    
    If IsNull(Me!id) Or (Me!id = "") Then
        Set x = CreateObject("Scriptlet.TypeLib")
        strNewguid = Left(x.GUID, 38)
        Me!id.Value = strNewguid
    End If
       
    DoCmd.Maximize

End Sub

In moving your forms to the new project did you create the global variables they depend on? I'd generally argue in favour of getting objects to communicate directly.
 
It is a good idea to avoid global variables wherever possible. In this case, I believe you could avoid using a global variable by the following method.

On the second form, the form to be opened, provide it with a private variables:

Code:
Private intVar As Variant
Private intRoom As String


'And a public function on the second form:


Public Function fPassTheVariables(passedIntVar As Variant, passedIntRoom as String)
    intVar = passedIntVar
    intRoom = passedIntRoom
End Function

In the calling form, the first form:

Code:
'opens lab forms associated with this lab_id
    intVar = Me!lab_id
    intRoom = Me!lab_room
    stDocName = "ehs_lab_safety_surveys"
    stLinkCriteria = "[lab_id]=" & "'" & intVar & "'"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

With Forms(stDocName)
         .fPassTheVariables(Me!lab_id, Me!lab_room)
         .Form.RecordsetClone.Find ("lab_id = '" & intVar & "'") 'I think this should work, but I can't checkit
End With

    DoCmd.Close acForm, "copy_lab_search_by_bldg"
I'm not sure if that will all work as is, because it is "air code" you may have to correct it slightly.

I see Lagbolt beat me to it, However I've done the deed so I thought I might as well post it.
 
wow...lots of info here :D ....thanks for replying pat, lagbolt and uncle gizmo....i'll give them a shot and let you know what i come up with...thanks a bunch!
 
I have a long post that is similar to this, but I don't want to crowd this thread. I am going to post it now under this forms forum. I would appreciate it if anyone can help me.

Thanks!
 
lagbolt:
i tried your way first (seemed easiest and the logical step)...i get a compile error for the line
Code:
Forms(strDocName).FormSetup(me.lab_id, me.lab_room)
it says "compile error. expected: =".....any thoughts on that?
 
Ya, my syntax is wrong. Remove the parenthesis around the parameters...
Code:
Forms(strDocName).FormSetup me.lab_id, me.lab_room
 
wow, i tried various things and that is not one of them. :o that works perfectly....thanks a bunch!
 
p.s. I found that open args is a simple way to accomplish this
 

Users who are viewing this thread

Back
Top Bottom