By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Referencing a form using a variable for the form name

P: 9
I have a Function in a Module that takes in a parameter. From my form, I call that function and pass my form name. I'm trying to use forms!formname.Controls in the function and I get the error:

Access can't find the form 'FormName' reffered to in macro express of vb code

The code in the form that calls the function is:
Check_Req_Fields "tbAddDocument"
And the code in the function:

Function Check_Req_Fields(FormName As String)

For Each ctrl In Forms!FormName.Controls
If TypeName(ctrl) = "TextBox" Then
If ((IsNull(ctrl.Value)) Or ctrl.Value = "") And (ctrl.Tag = "req") Then
err = err + 1
MsgBox "Please enter a value for " & ctrl.Name
Exit For
End If
End If
Next ctrl


Thank You
Nov 4 '08 #1
Share this Question
Share on Google+
2 Replies


Expert Mod 2.5K+
P: 2,545
There is an error in the way you are accessing the forms collection - it should be Forms(formname) to use the value of your formname variable, not
forms!formname, which will try to access a (nonexistent) form called formname. The variable's value cannot be substituted for the name of the variable when referring to forms using the ! notation.

-Stewart
Nov 4 '08 #2

P: 9
Thank you for pointing that out Steve. I do appreciate it.

Elesh
Nov 5 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.