Search code examples
stringms-accessvariablesvbasubroutine

Can I use a string variable as part of a command in Access VBA?


I have a form in Access which has some VBA automation in it to prevent junk data from being entered into the database. Part of that automation is to clear steps downstream of a step the user unchecks. It's tracking a linear process, so if steps 1, 2, 3, and 4 are complete, and the user unchecks 2, the code unchecks 3 and 4.

These steps are spread out over a multitude of possible testing forms. I want to code one subroutine that the program can call for the automatic unchecking, and pass a variable to the sub based on which test form the user clicked.

Here's my code, which isn't working. ("Dry" and "Wet" are steps in the process, in that order.):

Private Sub t2Dry_AfterUpdate()
    Dim TFNum As String
    TFNum = "t2"
    'Autodate and expose wet check fields once t2dry box is checked
    If t2Dry Then
    t2DDate.Enabled = True
    t2DDate.SetFocus
    t2DDate = Date
    t2Wet_Label.Visible = True
    t2Wet.Visible = True
    t2Wdate_Label.Visible = True
    t2Wdate.Visible = True
    End If

    'If dry box unchecked after date entered, undo all subsequent steps
    If Not t2Dry And t2date = "" Then
    Dim t2DUnchk As Byte
    t2DUnchk = msgbox("Unchecking this box will clear all subsequent data. Are you sure?", vbYesNo + vbQuestion, "Undo this step?")
    If t2DUnchk = vbYes Then
    t2DDate.SetFocus
    t2DDate = ""
    t2DDate.Enabled = False
    clearWetData (TFNum)
    Else: t2Dry = True
    t2DDate.SetFocus
    End If
    End If

End Sub

Note that this is only for Test Form 2, which is defined in my database and code as t2. This is the reason for declaring the TFNum variable. 7 lines from the bottom, you'll see me calling the subroutine I've written to clear the wet data after a user unchecks the dry box and wet data exists. Here is that subroutine:

Private Sub clearWetData(TFNum)

        'Call this subroutine to clear wet data from test forms on uncheck of dry comm step.
        'This subroutine relies on the TFNum argument to decide which test form's wet data to clear.
        (TFNum)Wet = False
        t2Wdate.Visible = True
        t2Wdate.Enabled = True
        t2Wdate.SetFocus
        t2Wdate = ""
        t2Wdate.Enabled = False
        t2Wet_Label.Visible = False
        t2Wet.Visible = False
        t2Wdate_Label.Visible = False
        t2Wdate.Visible = False

End Sub

That first line after the comments is an example of what I'm trying to do: use the passed variable TFNum (which is a string containing the text "t2") as part of an object name. "t2Wet" is the name of one of my checkbox objects I'm trying to clear, so I want it to parse that way.

All the remaining code, which begins with t2, is already working; the subroutine clears everything but the checkbox when called.

Sorry for the short novel, I wanted to be a detailed as I could. Thanks for your help!


Solution

  • As I understand it, your (TFNum)Wet is a control on a form. If that's the case, you can just use the controls collection:

    Me.Controls(TFNum & "Wet") = False