Aidan Hoffman
Aidan Hoffman

Reputation: 5

Button_Click event in Access VBA code doesn't open queries as expected

I've written the following VBA code to execute some queries off of a form with two text boxes and a button to run the appropriate query. When I perform the click event though, nothing happens... my queries don't open regardless of input, and even with no input the error msgbox doesn't display. I'm still pretty new to VBA and coding in general so I might be missing something obvious, but I can't see any glaring issues. On button click the debug.print does print, so the code is being accessed, it just isn't doing anything. I imagine it has to do with the nested If statements and specifically the opening of the queries, but I can't figure it out despite multiple hours of googling.

This is the button click event.

Private Sub RunQ_Click()

Dim Frm As Form
Dim MdlF As TextBox
Dim PartF As TextBox

Set Frm = Forms("SearchFormParts")
Set MdlF = Forms!SearchFormParts.Controls("ModelNoF")
Set PartF = Forms!SearchFormParts.Controls("PartNoF")
Debug.Print "this code is working2"
With Forms!SearchFormParts.Controls("RunQ")
    If .OnClick = "" Then
        If MdlF = vbNullString Then
            If PartF = vbNullString Then
                DoCmd.OpenQuery ("CCModel")
            Else
                DoCmd.OpenQuery ("CCBoth")
            End If
        ElseIf PartF = vbNullString Then
            If MdlF = vbNullString Then
                DoCmd.OpenQuery ("CCPart")
            Else
                DoCmd.OpenQuery ("CCBoth")
            End If
        Else: MsgBox ("Please enter a value in either or both fields!")
        End If
    End If
End With

End Sub

Thank you :)

Upvotes: 0

Views: 560

Answers (1)

Erik A
Erik A

Reputation: 32632

If .OnClick = "" Then results to false. It is set to [Event Procedure].

This causes none of your query code to be run. Remove it, and the corresponding End If

Upvotes: 1

Related Questions