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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variable to replace a long form name 2

Status
Not open for further replies.

TTThio

Programmer
May 3, 2001
185
US
Hi there,

is it possible to use variable to hold a long form name within a procedure?

For example:

formAAAAAA.text1
formAAAAAA.text2, etc.

How if just declare X = "FormAAAAA"
then do x.text1, x.text2, etc.

Is there a way to make it work? (Besides using with/end with)

Any help is appreciated.

 
I don't know if this would work:

dim frm as form

frm = "frmaaaaaaaa".

Nick
 
It won't work. To declare a form, need 'Set'
However, even with Set, it doesn't work. Because when I press frm. the property is not listed. If I force to write it, on executing procedure, error message "Method of data is not found"
 
You can use this do statement to find the index number of your form and then refer to the index number in your code:

[tt]
'Find the forms index
Dim i As Integer
i = 0

Do While i >= 0
If Forms(i).Name = "frm2" Then
Exit Do
End If
i = i + 1
Loop
[/tt]

Now in your procedure when you reference the form use this notation:

[tt]
Forms(i).ControlName = Value
[/tt]

HTH Joe Miller
joe.miller@flotech.net
 
Joe....

I've tried to apply it, but when it comes to i=1, I already got error message "The number you used to refer to a form is invalid"

I have more than one form in the project. I put the procedure in the general module. So if your above idea works, it will be really great.

Tin Tin
 
Hi!

Do you want to use some controls of separate forms? Then

dim frm as form
dim MyValue1, MyValue2, MyValue3

set frm=forms("formAAAAAA")
MyValue1=frm!text1
MyValue2=frm!text2
MyValue3=frm!text3

frm!text1="This is new value of text1"

***********************
Try following codes for checking of all contrl's names of your set form.

dim frm as form
dim ctl as control

set frm=forms("formAAAAAA")
for each ctl in frm.controls
debug.print ctl.name
next ctl

Aivars
 
I changed my code after thinking about this and hopefully this will help you. If you have problems let me know!

[tt]
Dim i As Integer
Dim FormToFind As String
Dim Match As Boolean

FormToFind = "formAAAAAA"
Match = False

For i = 0 To (Forms.Count - 1)
If Forms(i).Name = FormToFind Then
Match = True
Exit For
End If
Next i

If Match = True Then
MsgBox "Form '" & FormToFind & "' is index #" & i
Else
MsgBox "Form '" & FormToFind & "' Not Open!"
End If
[/tt]

Hope that helps! Joe Miller
joe.miller@flotech.net
 
Here's the same kind of thing as a function that will return the index number if the form is open or -1 if the form is not open.

[tt]
Function FindFormIndex(FormName As String) As Integer
Dim i As Integer

'Use For to find form index
For i = 0 To (Forms.Count - 1)
If Forms(i).Name = FormName Then
FindFormIndex = i
Exit Function
End If
Next i

'Return -1 if form is not loaded
FindFormIndex = -1
End Function
[/tt]

Now all you have to do is run the function in your code like so:

Dim i as Integer

i = FindFormIndex("formAAAAAA")

HTH!

Joe Miller
joe.miller@flotech.net
 
Thanks a lot guys!

Both ideas are applicable in my case.

First of all, Joe's idea for using Forms(i) is really helpful that I can identify the open form in the general module as using Me.Name just doesn't work.

Then by matching forms(i).name = "FormName" I create condition and set frm = "FormName" and able to use frm.property/control depending on the criteria.

Thanks again,
Tin Tin

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top