Change subform name dynamically

T

th34

I have 2 sub forms SubFormA and SubFormB. I am looking to
change the name of my subform depending which one is
visible. I have the following code but it does not seem
to work for me. Any suggestions would be greatly
appreciated.

Code below sits in the module:
Public FormName as String-----the variable declare in the
module

Public Sub Test1(FormName As String)
Forms![MainForm]!FormName.Form![First_Name].BackStyle
= "1"
Forms![MainForm]!FormName.Form![First_Name].BackColor
= "255"
End Sub

Public Sub Test2(FormName As String)
Forms![MainForm]!FormName.Form![First_Name].BackStyle
= "1"
Forms![MainForm]!FormName.Form![First_Name].BackColor
= "65535"
End Sub

-calling the code in the sub form
Private Sub Form_Current()
If Len(Me!ID) = 0 Then
Test1 Me.Name
Else
Test2 Me.Name
End If
End Sub


If I use a msgbox function, it prints out the subform name
for me but I just don't know how to pass into the
reference below. I've tried the following and it does not
work. Am I passing the variable wrong???

1) "Forms![MainForm]!" & FormName & ".Form!
[First_Name].BackColor = 255"
2) Forms![MainForm]! [FormName].Form!
[First_Name].BackColor = "255"
3) I also tried break the string up into 2 different
variables and concetenating them all together
 
M

Marshall Barton

th34 said:
I have 2 sub forms SubFormA and SubFormB. I am looking to
change the name of my subform depending which one is
visible. I have the following code but it does not seem
to work for me.

Public Sub Test1(FormName As String)
Forms![MainForm]!FormName.Form![First_Name].BackStyle
= "1"
Forms![MainForm]!FormName.Form![First_Name].BackColor
= "255"
End Sub

When you have a string containing the name of an item in a
collection, you can refer to to the item by using the
collection followed by the stringname in parenthesis:

Forms![MainForm].Controls(FormName).Form![First_Name].BackStyle
= "1"
 
T

th34

-----Original Message-----
th34 said:
I have 2 sub forms SubFormA and SubFormB. I am looking to
change the name of my subform depending which one is
visible. I have the following code but it does not seem
to work for me.

Public Sub Test1(FormName As String)
Forms![MainForm]!FormName.Form![First_Name].BackStyle
= "1"
Forms![MainForm]!FormName.Form![First_Name].BackColor
= "255"
End Sub

When you have a string containing the name of an item in a
collection, you can refer to to the item by using the
collection followed by the stringname in parenthesis:

Forms![MainForm].Controls(FormName).Form! [First_Name].BackStyle
= "1"
Marsh,
You're the best. It worked!!!!!

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top