I have a form that has two textboxes, txtStartDate & txtEndDate, that are used as criteria for a query. Also on the form are two buttons, btnRefresh & btnPrintReport. Both buttons need do to the same checking to make sure that the textboxes have dates in them. I would like to make that check be done in a function. The problem is that what needs done in the If False portion is different for the two buttons. btnRefresh will requery a subform and btnPrintReport will open a report. Is it possible to do something like the following?
Function
btnPrintReport On_Click
btnRefresh On_Click
strCommand is declared as a string at the top of the forms VBA (not in a function or private sub).
So basically, each button assigns its command to the variable and then runs the DateCheck function which uses the command assigned to the variable.
I know that my code doesn't work, but I would like to do something like this so that I can easily change the DateCheck function if I have to and have that change both buttons.
Function
Code:
Function DateCheck() If IsNull(Me.txtStartDate) Then MsgBox ("Please enter a start date") Else If IsNull(Me.txtEndDate) Then Me.txtEndDate = Date strCommand End If End If End Function
Code:
Private Sub btnPrintReport_Click() strCommand = DoCmd.OpenReport "rptPurchaseDateReport", acViewPreview DateCheck End Sub
Code:
Private Sub btnRefresh_Click() strCommand = Me.sfrmPurchaseDateReport.Requery DateCheck End Sub
So basically, each button assigns its command to the variable and then runs the DateCheck function which uses the command assigned to the variable.
I know that my code doesn't work, but I would like to do something like this so that I can easily change the DateCheck function if I have to and have that change both buttons.
Comment