Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a function on button click

Status
Not open for further replies.

Hawkide

Technical User
Oct 8, 2003
159
US
I have 10 buttons on a form. Each button needs to call a subroutine. Each subroutine is basically the same.

Do I have to write 10 separate OnClick event procedures, or can I call the same procedure from each button's OnClick event? For example:

If btn1 is clicked, On Click -> Call sbPreviewReport(1)
If btn2 is clicked, On Click -> Call sbPreviewReport(2)
If btn3 is clicked, On Click -> Call sbPreviewReport(3)
etc...

If I can write one function, can it be in the form's code module, or do I need to place it in it's own module?
 
Not enough info. If each is the same, why the need for 10? There must be some difference.

coachdan32
 
The procedure will receive an integer based on which button is clicked. For example:

If btn1 is clicked, On Click -> Call sbPreviewReport(1)
If btn2 is clicked, On Click -> Call sbPreviewReport(2)
If btn3 is clicked, On Click -> Call sbPreviewReport(3)

The flow of the procedue is based on the value of the parameter passed.
 
You can simply enter =sbPreviewReport(1) in the event for each button (changing -1- as appropriate).

I would put sbPreviewReport in a separate module Main.bas or BasGlobalRoutines.bas or whatever (Public).

Given your choice of names, I'm guessing that you have 10 buttons for 10 reports. Rather than passing an integer (number of report), I'd pass the string name =sbPreviewReport("rpt1") from the button. That way you wouldn't have to change the module code if you added a report. You could also have a combo box with your reports listed and a single command button. There are many ways to skin a cat.

Hope this was some help...

traingamer
 
I'd most certainly work on minimizing number of command buttons to 1 on your form - space and esthetic reasons, - and allow report selection from a combo/list box.
Try this if you want:
- make a table with 2 fields: autonumber repId field and text repName field, call the table tblReports, enter names of all report you have into this tbl;
- on a form make list box, call it lstReports, make it show reports names from tblReports
- on a form make command button, call it cmdPreviewReport
- make to onClick event procedures for the list and the button correspondently, they will be in form code module
- make onDoubleClick event procedure - it will allow you to double click the list and preview the selected report with out clicking the command button
- place there VB code as follows:
'========================================
Option Explicit

Dim strReportToOpen As String

Private Sub lstReports_Click()
strReportToOpen = Me.lstReports.Value
End Sub

Private Sub lstReports_DblClick(Cancel As Integer)
Call cmdPreviewReport_Click
End Sub

Private Sub cmdPreviewReport_Click()
If IsNull(strReportToOpen) Or strReportToOpen = "" Then
MsgBox "Select report from the list and try again.", vbCritical, "My application"
Exit Sub
End If

DoCmd.OpenReport strReportToOpen, acViewPreview

End Sub
'========================================

This should work. If you have default report or the one that is most commonly used you can code following event sub
(this way Report2 will be highlighted/selected when you open your form):

'========================================
Private Sub Form_Open(Cancel As Integer)
Me.lstReports.Value = "Report2"
End Sub
'========================================

I'd also suggest to implement appropriate error handling as reports in Access may and will through a monkey ranch at you.

As it has been noticed: "There are many ways to skin a cat."

Cheers.............
 
you might consider using a listbox or a combo box with your report names/options
then add some code to the On Change event so that it opens the currently selected option

hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top