0
votes

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 :)

1
I can't figure it out despite multiple hours of googling - yet 30 seconds of debugging would have fixed it. F9 to toggle breakpoints, F8 to step through, Shift+F8 to step over; then you have the locals toolwindow, the immediate pane (Ctrl+G). Place a breakpoint on that With instruction, run it, then press F8 once to get to the If statement. Hover the .OnClick token and you'll see why next time you press F8 execution jumps to End If and out of the procedure. - Mathieu Guindon

1 Answers

1
votes

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