Reputation: 19
I have written a function to run from a macro using RunCode
.
I am trying to call the function from the OnClick
event of a command button.
I want to print a different page of my report depending upon the value in the text control Text28
.
When I press the button, the function is not running.
Public Function PrintOut()
If Text28 = "aaa" Then
DoCmd.PrintOut acPages, 2, 2, , 1
ElseIf Text28 = "bbb" Then
DoCmd.PrintOut acPages, 3, 3, , 1
ElseIf Text28 = "ccc" Then
DoCmd.PrintOut acPages, 4, 4, , 1
ElseIf Text28 = "ddd" Then
DoCmd.PrintOut acPages, 5, 5, , 1
End If
End Function
Upvotes: 0
Views: 240
Reputation: 16025
Rather than using a Macro with a RunCode
action to evaluate your function, I would suggest evaluating the code directly from a VBA event handler associated with the OnClick
event for your button, e.g. (changing the Command0
to suit your button name):
Private Sub Command0_Click()
If Text28 = "aaa" Then
DoCmd.PrintOut acPages, 2, 2, , 1
ElseIf Text28 = "bbb" Then
DoCmd.PrintOut acPages, 3, 3, , 1
ElseIf Text28 = "ccc" Then
DoCmd.PrintOut acPages, 4, 4, , 1
ElseIf Text28 = "ddd" Then
DoCmd.PrintOut acPages, 5, 5, , 1
End If
End Sub
A Select Case
statement may also be more appropriate than sequential if
statements, e.g.:
Private Sub Command0_Click()
Select Case Text28
Case "aaa": DoCmd.PrintOut acPages, 2, 2, , 1
Case "bbb": DoCmd.PrintOut acPages, 3, 3, , 1
Case "ccc": DoCmd.PrintOut acPages, 4, 4, , 1
Case "ddd": DoCmd.PrintOut acPages, 5, 5, , 1
Case Else: MsgBox "Text28 has an invalid value."
End Select
End Sub
Upvotes: 1