We help IT Professionals succeed at work.

Refering to a form using a variable.

peispud
peispud asked
on
Hi
 
See the code below.  I want to enter into the sub with the name of the form as a string.  I will need to refer to that form directly later.   I wish to do this by using syntax similar to  what the code below.

I would appreciate help in fixing that code.

Thanks


Public Sub CheckDuplicates(ByVal FormName As String, ByRef lblCommunicate As Label)
    Dim TheForm As Form: Set TheForm = Forms(FormName)

Open in new window

Comment
Watch Question

Kyle AbrahamsSenior .Net Developer

Commented:
Dim TheForm as Form = Forms(FormName)
peispudTech

Author

Commented:
Thanks for your reply, but it did not work.   The syntax failed.

I used the  following  but  got "Invalid use of property"....  

Public
 Sub CheckDuplicates(ByVal Const_FormTable As String, ByRef lblCommunicate As Label)
Dim TheForm As Form:  TheForm = Forms(Const_FormTable)

Open in new window

Top Expert 2016
Commented:
try

Dim TheForm As Form: Set  TheForm = Forms(Const_FormTable)
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
First, your form must be open to do this.
Second, if so, you could simply do:

Public Sub CheckDuplicates(ByRef TheForm As Form, ByRef lblCommunicate As Label)
    Debug.Print TheForm.Name
    ' Other stuff.
End Sub
/gustav
peispudTech

Author

Commented:
Thank you for your replies, but I have not found resolution to my original question.
I have re-read it.  I believe that I have phrased the question correctly.

I am using Microsoft Access 2013.
Top Expert 2016

Commented:
are you getting an error? what is the error?

the form must be open, and this should work

Dim TheForm As Form: Set  TheForm = Forms(Const_FormTable)

debug.print TheForm.Recordsource
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
>   I believe that I have phrased the question correctly.

Yes, and you've got the answer.

/gustav