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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dpot
    New Member
    • Oct 2014
    • 30

    #1

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

    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:
    Code:
    Dim str Form As String
    If Me.ComboSelect.Value = "A" Then
    strForm = "AForm"
    ElseIf Me.ComboSelect.Value = "B" Then
    strForm = "BForm"
    ElseIf Me.ComboSelect.Value = "C" Then
    strForm = "CForm"
    ElseIf Me.ComboSelect.Value = "D" Then
    strForm = "DForm"
    End If
    DoCmd.OpenForm strForm
    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:
    Code:
    If Me.ComboSelect.Value = "A" Then
    Forms!AForm.Text1.Value = Me.Text1.Value
    Forms!AForm.Text2.Value = Me.Text2.Value
    Forms!AForm.Text3.Value = Me.Text3.Value
    End If
    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:
    Code:
    DoCmd.OpenForm strForm
    Forms!strForm.Text1.Value = Me.Text1.Value
    Forms!strFormText2.Value = Me.Text2.Value
    Forms!strForm.Text3.Value = Me.Text3.Value
    But I am getting "Run-time error '2494': The action or method requires a Form Name argument.
  • jforbes
    Recognized Expert Top Contributor
    • Aug 2014
    • 1107

    #2
    You could refer to a Form like this:
    Code:
    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:
    Code:
    Function isLoaded(ByRef sFormName As String) As Boolean
        ' Determines if a Form is loaded
        Dim i As Integer
    
        isLoaded = False
        For i = 0 To Forms.Count - 1
            If Forms(i).FormName = sFormName Then
                isLoaded = True
                Exit Function
            End If
        Next
    End Function
    Last edited by jforbes; Feb 9 '15, 01:06 PM. Reason: added isLoaded()

    Comment

    • Dpot
      New Member
      • Oct 2014
      • 30

      #3
      Still getting the run-time error. Could it be because
      Code:
      Dim strForm As String
      It highlights the strForm in
      Code:
      Forms!strForm.Text1.Value = Me.Text1.Value
      or
      Code:
      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
      Code:
      Dim strForm As Form
      I get "Invalid use of property" and it'll highlight the strForm in this code

      Code:
      If Me.ComboSelect.Value = "A" Then
      strForm = "AForm"

      Comment

      • jforbes
        Recognized Expert Top Contributor
        • Aug 2014
        • 1107

        #4
        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..ELSEI F block (which you might want to convert to a SELECT CASE)

        Once the String is set,
        Code:
        Forms(strForm).Text1.Value = Me.Text1.Value
        should work for you.

        Comment

        • Dpot
          New Member
          • Oct 2014
          • 30

          #5
          Thank you so much for your help!

          Comment

          Working...