Passing Variable HELP HELP HELP

G

Guest

I am using Access 2002 and have a variable that I need to access throughout
the program several times. I have set it as a Public variable in a module.
I use the Forms!F4562.G_SeqNum to carry the variable from open form to open
form but when I close the first form I can then no longer access the data in
the G_SeqNum variable.

I have also tried to set this variable in the module as
Global G_SeqNum as String

Nothing I do though allows the variable data to be displayed on the 2nd form
once the first form is closed.


Thanks in advance for any assistance provided!!!!!!!!!!!
Dwight
 
G

Guest

To expose a variable globally throughout the database you must declare it
Public in a standard module (the sort you see in he module's tab of the
database window), not a form's or report's class module, so if you've done
the latter it would explain your problem. You can declare a variable public
in a form's class module and expose it as property of the class elsewhere,
while the form is open by qualifying it with the class name, e.g.
Form_MyForm.MyVariable, when you refer to it elsewhere, but I don't think
that's what you have in mind.

Bear in mind that you can also pass values as arguments of functions or
sub-procedures or as the OpenArgs argument of the OpenForm method. Generally
speaking using global variables is best avoided if possible as the value is
persistent and thus can be unpredictable, whereas passing values as arguments
gives you control over the value at any one time.

As regards the use of the OpenArgs mechanism I have developed a module which
allows you to pass a value list or multiple named arguments between forms. I
can post the code here if you think it might be useful.
 
G

Guest

1. I have G_SeqNum as String in the Tab Module of the database and then I
set it in a form procedure when the SeqNum field is changed in the On Dirty
event otherwise it is only set the one time upon opening the form.

I have a field named G_SeqNum on the form just so I can see what the value
is all the time however it was not changing for some reason either.

Please post your code and maybe it will help cause your scenerio about
changing it several times is exactly what I need.

THANKS for you ASSISTANCE......

Dwight Cornett
 
G

Guest

I continue with my Public variable problem.

I have in the Module tab a Module I call GVariables where I establish all of
my Public variables.

G_SeqNum as string

In my form1 I set the G_SeqNum to equal what is in field E600. This works
fine.

I open a macro as I leave Form1 which closes Form 1 of record 1 and opens
Form 2 or Form Page two of the same record 1.

I can not get the Form or the Variable to display the G_SeqNum value. I
have tried Forms!Form1.E600
Forms!Form1.G_SeqNum

nothing seems to work yet everything I read says it should.

The G_SeqNum field however is not part of any table it is used only to tell
what records SeqNum to open on when going to page 2.

THANKS FOR YOUR HELP in advance. I know this should not be that hard but I
have a brain dead week starting I can tell...

Thanks AGAIN!!!!!!!

Dwight Cornett
 
G

Guest

Lets look at each aspect of this:

The variable should be declare in the declarations area of any standard
module:

Public G_SeqNum as String

To assign a value of the E600 field in Form 1 you should firstly do so in
the form's Load event procedure if you want the variable to contain whatever
is in the field when the form opens. As the variable is of String data type
you need to allow for the field being Null by converting any Null to a
zero-length string with the Nz function:

G_SeqNum = Nz(Me.E600,"")

If the form is bound to a table and you want the variable set to the value
of the E600 field as you move from record to record, then do the above in the
form's Current event procedure also:

G_SeqNum = Nz(Me.E600,"")

To assign the value to the variable when the user updates field E600 then do
the same in the E600 control's AfterUpdate event procedure:

G_SeqNum = Nz(Me.E600,"")

To get the value of the variable in Form2 you can do one of two things:

In code simply refer to the variable by name. If you want the value of the
variable in an unbound control then add a function to a standard module:

Function GetSeqNum() As String

GetSeqNum = G_SeqNum

End Function

You can then get the value of the variable in a text box any where in the
database by setting its ControlSource to:

=GetSeqNum()

However, as you seem to want to pass a value form one form to another I
think using the OpenArgs mechanism would be a lot better. I'm posting the
code for my module in my reply to your other post but in this case to pass
the value of the E600 field from Form 1 to Form 2 then you don't need any of
the fancy stuff; you can simply do it when you open Form 2 from Form 1 with
the OpenForm method:

DoCmd.OpenForm "Form2", OpenArgs:= Me.E600

Then in Form 2's module refer to its OpenArgs property which will contain
the value passed to the form, e.g. in code you could assign it to a local
variable:

Dim G_SeqNum As String

G_SeqNum = Me.OpenArgs
 
G

Guest

Here's the code for my module and I'm explaining below how its used. Note
that if you paste this code into a new blank module in Access you should
first delete the two lines which are already in place in the module by
default.

'<module starts>'
Private Const OFFSET As Long = 127
Private Const ASSIGNOP As String = "=="
Function Arg(buffer, idx) As Variant

If IsNumeric(idx) Then
i& = InStr(1, buffer, Chr(idx + OFFSET - 1))
token$ = Chr(idx + OFFSET)
i& = InStr(i&, buffer, ASSIGNOP) + 2
Else
i& = InStr(1, buffer, idx) + Len(idx) + 2
token$ = Chr(Asc(Mid(buffer, InStr(1, buffer, idx) - 1, 1)) + 1)
End If
Arg = Mid(buffer, i&, InStr(i&, buffer, token$) - i&)

End Function

Function Argname(buffer, idx) As String

i& = InStr(1, buffer, Chr(idx + OFFSET - 1))
token$ = Chr(idx + OFFSET)
Argname = Mid(buffer, i& + 1, InStr(i&, buffer, ASSIGNOP) - (i& + 1))

End Function

Function ArgCount(buffer) As Long

ArgCount = Asc(Right(Chr(OFFSET) & buffer, 1)) - OFFSET

End Function

Sub AddArg(buffer, Argname, argval)

If Len(buffer & "") = 0 Then buffer = Chr(OFFSET)
If IsNumeric(Argname) Then Argname = ArgCount(buffer) + 1
buffer = buffer & Argname & ASSIGNOP & argval & Chr(Asc(Right(buffer,
1)) + 1)

End Sub

Sub AddArgList(buffer, ParamArray Tokens())

For i& = 0 To UBound(Tokens)
AddArg buffer, i& + 1, Tokens(i&)
Next

End Sub
'<module ends>'

Here'show its used:

Values can be passed individually as named arguments:

Dim args As String

' add some named arguments
AddArg args, "First", "Apples"
AddArg args, "Second", "Pears"
AddArg args, "Third", "Bananas"

Or as an arguments list:

AddArgList args, "Oranges", "Peaches", "Grapefruit"

The list is then passed to the form with:

DoCmd.OpenForm "frmMyForm", OpenArgs:=args

In the form's module the values can be extracted like so:

Dim args As String, i As Integer

args = Me.OpenArgs

' get some named named arguments
Debug.Print Arg(args, "First")
Debug.Print Arg(args, "Second")
Debug.Print Arg(args, "Third")
' get some arguments by ordinal position
Debug.Print Arg(args, 4)
Debug.Print Arg(args, 5)
Debug.Print Arg(args, 6)
' list all arguments
For i = 1 To ArgCount(args)
Debug.Print Argname(args, i), Arg(args, i)
Next i
' get count of arguments
Debug.Print ArgCount(args)
 
G

Guest

Thanks I can not believe it takes this much code to save a single public
variable so it can display it's current value from anywhere in the database.

Anyway thanks a bunch hopefull I can figure it all out...

Thanks again!!!!!!!!!!!1111
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top