Home > Software engineering >  MS Access Turn textbox invisible if button clicked
MS Access Turn textbox invisible if button clicked

Time:05-24

I have two different forms, one has 5 buttons and the other one has 5 textboxes By clicking one of the five buttons from the first form, I want the user to be sent to the 2nd form but only see one textbox, the other 4 are supposed to be invisible. Whenever I try to run my code, I get 2 separate error codes: the first error I get is runtime error 424 (error appears on the first form), the second error I get is runtime error 94 (2nd form)

Here is the code I am trying to execute

----first form----

Public Sub Button_Click()
           DoCmd.OpenForm "All in one ", acNormal, OpenArgs:="Associate"
End Sub

----2nd form----

Private Sub Form_Open(Cancel As Integer)
Dim Source As String
Source = Me.OpenArgs

If Source = "Associate" Then
           Associate.Visible = True
           Professional.Visible = False
           Senior_Pofessional.Visible = False
           Expert.Visible = False
           Senior_Expert.Visible = False
End Sub

I haven't written the code for the other 4 buttons since I first want to make it work for the first one. I am very new to both MS Access and coding so any sort of help would be highly appreciated :)

CodePudding user response:

Try this - removing the space from the form name:

Public Sub Button_Click()
    DoCmd.OpenForm "All in one", acNormal, , , , , "Associate"
End Sub

----2nd form----

Private Sub Form_Open(Cancel As Integer)

    Dim Source As String

    Source = Nz(Me.OpenArgs)

    MsgBox "Source: '" & Source & "'"

    If Source = "Associate" Then
        Me!Associate.Visible = True
        Me!Professional.Visible = False
        Me!Senior_Pofessional.Visible = False
        Me!Expert.Visible = False
        Me!Senior_Expert.Visible = False
    End If

End Sub

CodePudding user response:

Another way to do it would be to compare the name of all textboxes with your OpenArgs string and change the visibility dependant on that.

Private Sub Form_Open(Cancel As Integer)

    Dim Source As String
    Source = Replace(Me.OpenArgs, " ", "_") 'Replace spaces with an underscore.

    Dim ctl As Control
    For Each ctl In Me.Controls
        With ctl
            If .ControlType = acTextBox Then
                .Visible = (Source = .Name) 'Is Source the same as the control name. Returns TRUE/FALSE.
            End If
        End With
    Next ctl
End Sub  

On a side note - your code is missing an End If and Senior_Pofessional should be Senior_Professional?

  • Related