Referencing form-module variables

J

John Geddes

In Access 2000, I have an instance of a form set up like this:

set bb = New Form_frmBook

I would like to be able to reference variables declared within the form
module, programatically, from other modules.

If frmBook has :
dim kids as integer

I can happily reference bb.children in hard coding

Had I set "kids " up as a form field, I could reference this as bb(xx)
if xx had the string "children" in it.

But I can't find a way of achieving the same with a module variable.

I've tried building a string with "bb." in it, appending the variable
name ("kids") and then using Eval, ie
eval("bb.kids")

but this raises a 2482 error complaining that Access cannot find the
name "bb".

Ideas, please.

John Geddes
 
A

Arvin Meyer

You cannot reference variables inside a module since they are out of scope
as soon as the module has finished running (or hasn't run yet) You can set
form or public (global) variables in the declaration section of a module and
have them refer to a value in a function like:

Option Explicit
Public strA As String

Private Function Foo()
strA = "Hello World"
End Function

Now all you need to do is reference strA. You can also do this:

Public Function Foo() As String
Foo = "Hello World"
End Function

If the module is in a form, you can refer to it when the form is open, or
you can put it in a Standard module and refer to =Foo() anythime.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads:
http://www.datastrat.com
http://www.mvps.org/access
 
M

Marshall Barton

John said:
In Access 2000, I have an instance of a form set up like this:

set bb = New Form_frmBook

I would like to be able to reference variables declared within the form
module, programatically, from other modules.

If frmBook has :
dim kids as integer

I can happily reference bb.children in hard coding

Had I set "kids " up as a form field, I could reference this as bb(xx)
if xx had the string "children" in it.

But I can't find a way of achieving the same with a module variable.


Be sure to keep a clear picture of the differences between
standard and class modules.

Class module Public variables are properties of the class.
This means that you need to use:

Public Kids As Integer

and as long as the form instance is in scope, you can refer
to it this way:

bb.Kids
 
G

Guest

John, hate to be contrary, but the answer is Dont Do That!!. It is very poor
design. There are so many problems and things that can go wrong that I wont
even address them here. Find another way.
 

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