Forum Discussion
sf49ers19238597
Jun 17, 2024Iron Contributor
I have 3 option to pick for print I pick which option I need that the time for printing
I have codes to pick which on to print. I have one button
Private Sub PrintButton_Click()
Dim selectedOption As Integer
' Get user selection from option buttons
selectedOption = GetSelectedOption()
Select Case selectedOption
Case 1
PrintLast3Months
Case 2
PrintLastMonthToCurrent
Case 3
PrintCurrentMonth
Case Else
MsgBox "Please select an option.", vbExclamation
End Select
End Sub
Thanks You
3 Replies
Sort By
- sf49ers19238597Iron Contributor
I try to configure our code and how to get it to work.
I added a copy of the updated file.
- NikolinoDEGold Contributor
The error message "Compile error" usually indicates there is an issue with the code syntax or structure. In your case, it might be related to the function GetSelectedOption, which is not defined in your snippet. Also, the subroutines PrintLast3Months, PrintLastMonthToCurrent, and PrintCurrentMonth need to be defined.
Here is a complete example of how you can implement this in VBA:
- Define the function GetSelectedOption to determine which option is selected.
- Define the subroutines PrintLast3Months, PrintLastMonthToCurrent, and PrintCurrentMonth.
Complete Example
Vba Code is untested backup your file
Private Sub PrintButton_Click() Dim selectedOption As Integer ' Get user selection from option buttons selectedOption = GetSelectedOption() Select Case selectedOption Case 1 PrintLast3Months Case 2 PrintLastMonthToCurrent Case 3 PrintCurrentMonth Case Else MsgBox "Please select an option.", vbExclamation End Select End Sub ' Function to get the selected option Private Function GetSelectedOption() As Integer ' Assuming you have OptionButtons named OptionButton1, OptionButton2, and OptionButton3 If OptionButton1.Value = True Then GetSelectedOption = 1 ElseIf OptionButton2.Value = True Then GetSelectedOption = 2 ElseIf OptionButton3.Value = True Then GetSelectedOption = 3 Else GetSelectedOption = 0 ' No option selected End If End Function ' Subroutine to print the last 3 months Private Sub PrintLast3Months() ' Your code to print the last 3 months goes here MsgBox "Printing the last 3 months." End Sub ' Subroutine to print from last month to current Private Sub PrintLastMonthToCurrent() ' Your code to print from last month to current goes here MsgBox "Printing from last month to current." End Sub ' Subroutine to print the current month Private Sub PrintCurrentMonth() ' Your code to print the current month goes here MsgBox "Printing the current month." End Sub
Explanation:
- PrintButton_Click Subroutine: This subroutine handles the click event for the Print button. It gets the selected option using GetSelectedOption and calls the appropriate print subroutine based on the selected option.
- GetSelectedOption Function: This function checks which option button is selected and returns the corresponding value (1, 2, or 3). If no option is selected, it returns 0.
- Print Subroutines: The PrintLast3Months, PrintLastMonthToCurrent, and PrintCurrentMonth subroutines contain the code to handle the printing for each specific option. Currently, they just show a message box, but you can replace that with your actual printing logic.
Important Notes:
- Make sure the option buttons (OptionButton1, OptionButton2, OptionButton3) are named correctly in your form.
- You can replace the message boxes in the print subroutines with your actual print logic.
By following this complete example, you should be able to resolve the compile error and have a functioning print selection system in your VBA project. The text, steps and the code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.
- sf49ers19238597Iron Contributor
is possible can make a sample file. I copy code not working
Thanks You