Declaring global variables

rkmaitra

Registered User.
Local time
Today, 16:33
Joined
Jul 11, 2011
Messages
40
Hello,
I have declared a global variable in the procedure of one of my forms and wish to recall the same variable in when a different form opens. I don't know why it tells me 'variable not declared'. What is the error? Can anyone help?

Option Compare Database
Option Explicit
'declaring the public variable
Public gblnOpenedByAnother As Boolean

'on click of the command button, this happens
Private Sub Insert_Record_Before_Click()
Dim lngmRecno As Long
lngmRecno = Me.Recno
CurrentDb.Execute "insert into tblMain_Table (Recno) Values (" & lngmRecno & ")", dbFailOnError
gblnOpenedByAnother = True
DoCmd.OpenForm "Append"
'opens the second form
DoCmd.GoToRecord , , acLast
End Sub

'on opening the second form
Private Sub Form_Open(Cancel As Integer)
If gblnOpenedByAnother = False Then 'error here
DoCmd.GoToRecord , , acNewRec
End If
End Sub
 
Hi
Ithink your variable needs to be declared in a general module, not in the form.
 
Hi

If I'm correct, the variable can be called as long as the form is open but as bob rightly suggests, it should be in a module not the form. I tend to have a module specifically for declarations to keep things tidy otherwise you might find variables everywhere


Cheers

Nidge
 

Users who are viewing this thread

Back
Top Bottom