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

Using a string name in Forms!____!Text.Value

Dpot
P: 30
I'm working on a database that was created by someone else. They used a code to open a specific form based on the value of a combobox. The code is:
Expand|Select|Wrap|Line Numbers
  1. Dim str Form As String
  2. If Me.ComboSelect.Value = "A" Then
  3. strForm = "AForm"
  4. ElseIf Me.ComboSelect.Value = "B" Then
  5. strForm = "BForm"
  6. ElseIf Me.ComboSelect.Value = "C" Then
  7. strForm = "CForm"
  8. ElseIf Me.ComboSelect.Value = "D" Then
  9. strForm = "DForm"
  10. End If
  11. DoCmd.OpenForm strForm
  12.  
The actual code is quite long.


The problem I'm running into is the form has some text boxes where I would like the values to carry over to the appropriate form that opens.

Right now I have each form individually listed out like this:
Expand|Select|Wrap|Line Numbers
  1. If Me.ComboSelect.Value = "A" Then
  2. Forms!AForm.Text1.Value = Me.Text1.Value
  3. Forms!AForm.Text2.Value = Me.Text2.Value
  4. Forms!AForm.Text3.Value = Me.Text3.Value
  5. End If 
  6.  
And it will open the appropriate form with the text boxes filled in which is wonderful BUT since there are so many options I just wanted to simplify the code to read something like:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm strForm
  2. Forms!strForm.Text1.Value = Me.Text1.Value
  3. Forms!strFormText2.Value = Me.Text2.Value
  4. Forms!strForm.Text3.Value = Me.Text3.Value
  5.  
But I am getting "Run-time error '2494': The action or method requires a Form Name argument.
Feb 9 '15 #1

✓ answered by jforbes

Right, you'll either need to create a Form Variable and set it to the Form Instance and then set the values of the controls or use the Forms() or AllForms() collection to get a reference to the Instance of the Form and then set the values.
...I know that sentence was as painful to write as to read.

There are a couple collections available to you. The Forms() and AllForms() collections have lists of Forms. The difference is that AllForms() contains all the Forms in your database regardless if they are opened or not. The Forms() collection contains only opened Forms. So you can refer to the Forms in the collections a few different ways. These examples aren't great, but they give an idea of the different ways to address Forms in the Forms() and AllForms() collections.

So by using the Forms("formname") syntax, you can refer to the Form by name, which could be a string variable like strForm which is set in the large IF..THEN..ELSEIF block (which you might want to convert to a SELECT CASE)

Once the String is set,
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
  2.  
should work for you.

Share this Question
Share on Google+
4 Replies


jforbes
Expert 100+
P: 1,107
You could refer to a Form like this:
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text2.Value = Me.Text2.Value
I also use this code to make sure a Form is loaded before I try something like this:
Expand|Select|Wrap|Line Numbers
  1. Function isLoaded(ByRef sFormName As String) As Boolean
  2.     ' Determines if a Form is loaded
  3.     Dim i As Integer
  4.  
  5.     isLoaded = False
  6.     For i = 0 To Forms.Count - 1
  7.         If Forms(i).FormName = sFormName Then
  8.             isLoaded = True
  9.             Exit Function
  10.         End If
  11.     Next
  12. End Function
Feb 9 '15 #2

Dpot
P: 30
Still getting the run-time error. Could it be because
Expand|Select|Wrap|Line Numbers
  1. Dim strForm As String
It highlights the strForm in
Expand|Select|Wrap|Line Numbers
  1. Forms!strForm.Text1.Value = Me.Text1.Value
or
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
Is there another way to write this so that way it would work for both codes?

If I change it to
Expand|Select|Wrap|Line Numbers
  1. Dim strForm As Form
I get "Invalid use of property" and it'll highlight the strForm in this code

Expand|Select|Wrap|Line Numbers
  1. If Me.ComboSelect.Value = "A" Then
  2. strForm = "AForm"
Feb 9 '15 #3

jforbes
Expert 100+
P: 1,107
Right, you'll either need to create a Form Variable and set it to the Form Instance and then set the values of the controls or use the Forms() or AllForms() collection to get a reference to the Instance of the Form and then set the values.
...I know that sentence was as painful to write as to read.

There are a couple collections available to you. The Forms() and AllForms() collections have lists of Forms. The difference is that AllForms() contains all the Forms in your database regardless if they are opened or not. The Forms() collection contains only opened Forms. So you can refer to the Forms in the collections a few different ways. These examples aren't great, but they give an idea of the different ways to address Forms in the Forms() and AllForms() collections.

So by using the Forms("formname") syntax, you can refer to the Form by name, which could be a string variable like strForm which is set in the large IF..THEN..ELSEIF block (which you might want to convert to a SELECT CASE)

Once the String is set,
Expand|Select|Wrap|Line Numbers
  1. Forms(strForm).Text1.Value = Me.Text1.Value
  2.  
should work for you.
Feb 9 '15 #4

Dpot
P: 30
Thank you so much for your help!
Feb 9 '15 #5

Post your reply

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