Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Variable form names 2

Status
Not open for further replies.

billheath

Technical User
Mar 17, 2000
299
US
I wrote the following code to open up a form using a variable name.

Dim stDocName, Asset_Num as String
StDocName="frm List"
DoCmd.OpenForm stDocName

The form, "frm List" opens with out a problem. However, when I try to populate a field, as in :

Forms!stDocName.[Assets] = Asset_Num

I get the error message, "Cannot find the form "stDocName"."

I need to be able to select different forms and use the variable name to open and set data. How do I get Access to recognize that stDocName is a variable?
Thanks, Bill Heath
 
To use a control on a form you must use a form object. There are two ways to reference a form object. One is directly, the other is through the forms collection. To do what you want you must use the formname if you want to assign the form object directly(you can't use a variable). It would look like this:

Dim frm as Form
Set frm = Forms![frm list]

You can then use this object to assign whatever:

frm.Assets = Asset_Num

The other way (which will allow the use of a variable indirectly) is to use the forms collection. It's kind of a round about way to assign the form object, but it works. It would look like this:

Dim frm As Form
Dim stDocName, Asset_Num as String
StDocName="frm List"
DoCmd.OpenForm stDocName

For Each frm in forms
If frm.Name = stDocName
frm.Assets = Asset_Num
End If
Next

 
Oops, I'm brain dead. Paul's use of the Forms collection is MUCH more efficient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top