Create Form type using Form's Name retrieved from a table

  • Thread starter Thread starter Ray
  • Start date Start date
R

Ray

I would like to be able to store a form name in a field in a "Config"
table - then when the application initializes, it gets the name of the
form, sets a public variable declared as a Form type, and use this
form object throughout the code.

What I haven't latched onto yet is how to take the entry from the
database and use it to create the form variable...

for instance...

Dim objMainForm as Form
Dim sFormName as String

... Read Database Info and retrieve form name

sFormName = rs![Name of Form To use]


Here's where I believe I'm missing it... After retrieving the name of
the form as a string...how do you use it to set the objMainForm
variable? Even if I retrieve it as a Variant - would that help?

How do I transfer the value of sFormName to Forms!Form! ???


Suggestions and instructions are appreciated..
A slapping of the hands for stupidity is also accepted.

Thanks
Ray
 
One approach:

DoCmd.OpenForm sFormName
Set objMainForm = Forms(sFormName)

The Forms collection consists only of Open Forms, so you have to open the
form before you can set the Form object reference.
By the same token, objMainForm will remain valid only while the form remains
open.

Another approach:
More recent versions of Access (starting with 2000? 2002?) have an AllForms
collection that doesn't have that particular limitation. (I've barely used
AllForms myself, so the following might be *very* wrong :-). If you go that
route it might look something like:

Dim objMainForm as AccessObject

'(retrieve sFormName)

Set objMainForm = CurrentProject.AllForms(sFormName)
DoCmd.OpenForm objMainForm.Name

HTH,
 
Perfect!
I knew I was making it too complex...

Programming would be so simple if it wasn't for syntax...

Thanks!
Ray
 
Sounds like that would be work nicely - unfortunately - I'm still on
Access 2k.

Thanks

One approach:

DoCmd.OpenForm sFormName
Set objMainForm = Forms(sFormName)

The Forms collection consists only of Open Forms, so you have to open the
form before you can set the Form object reference.
By the same token, objMainForm will remain valid only while the form remains
open.

Another approach:
More recent versions of Access (starting with 2000? 2002?) have an AllForms
collection that doesn't have that particular limitation. (I've barely used
AllForms myself, so the following might be *very* wrong :-). If you go that
route it might look something like:

Dim objMainForm as AccessObject

'(retrieve sFormName)

Set objMainForm = CurrentProject.AllForms(sFormName)
DoCmd.OpenForm objMainForm.Name

HTH,



Ray said:
I would like to be able to store a form name in a field in a "Config"
table - then when the application initializes, it gets the name of the
form, sets a public variable declared as a Form type, and use this
form object throughout the code.

What I haven't latched onto yet is how to take the entry from the
database and use it to create the form variable...

for instance...

Dim objMainForm as Form
Dim sFormName as String

... Read Database Info and retrieve form name

sFormName = rs![Name of Form To use]


Here's where I believe I'm missing it... After retrieving the name of
the form as a string...how do you use it to set the objMainForm
variable? Even if I retrieve it as a Variant - would that help?

How do I transfer the value of sFormName to Forms!Form! ???


Suggestions and instructions are appreciated..
A slapping of the hands for stupidity is also accepted.

Thanks
Ray
 
Back
Top