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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass parameter to sub from Command button On_Click? 2

Status
Not open for further replies.

Poduska

Technical User
Dec 3, 2002
108
US
I am attempting to not duplicate a long Sub by passing a parameter from the "On Click" event of a command button.

The command button wizard created a sub called
"public Sub cmdRunReport_Click()" which has a lot of code which I do not want to duplicate for maintenance reasons.

Now I need to make two versions of the report, one for OPEN records and one DUE records and call these from two command buttons, one called "cmdOPEN" the other "cmdDue".

How can I have two command buttons call the same VB sub routine but pass a parameter?

For example
cmdRunReport_Click("Open")or
cmdRunReport_Click("Due")

If I paste either of the two examples above in the properties tab instead of [Event Procedure] Access just thinks I am attempting to run a macro.

I know how to pass parameters to Sub or Functions from within the VBA module but never in this particular way.
 
Sorry, but I don't see where is the problem.
Have a Public procedure accepting a string parameter for launching the relevant report and then call this Sub with appropriate parameter in the Click event procedures of your buttons.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV

But how do I call a SUB with a parameter from the On Click event?

Quote
Have a Public procedure accepting a string parameter I have this for launching the relevant report and then call this Sub with appropriate parameter in the Click event procedures of your buttons.How?
 
Two ways:
In the Click event procedure of first button:
Code:
[i]nameOfYourPublicSub[/i] "Open"
In the Click event procedure of second button:
Code:
Call [i]nameOfYourPublicSub[/i]("Due")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Like PHV said

public myPublicSub(strRecordType as string)
select case strRecordType
case "Open"
do some code
case "Due"
do some different code
end select
do some generic code
end sub

Private cmdRunReportOpen_Click
call myPublicSub("Open")
end sub

Private cmdRunReportDue_Click
call myPublicSub("Due")
end sub
 
Ok thanks,

I had "wishfull thinking" that there may be a way to call
Code:
myPublicSub("Open") or myPublicSub("Due")
directly from the individual On Click event. As it appears Access has predetermined the Sub to be run from a specific event then this solution is great.

If I have to throw in an additional Sub to make the Due or Open split then so be it and it will accomplish my purposes.

Many thanks for your time and effort, I REALLY appreciate it.

 
Can you post your sub? it would be a lot easier to give a suggestion.
 
I have removed most non essential code such as the error checking, form modifications etc. to demonstrate why I wanted to pass a parameter.

The idea was to call this SUB from two distinct command buttons but passing "OPEN" or "Due".

Code:
'########################################
'RUNS DIRECTORS SUMMARY REPORT
'########################################
Private Sub PrintDirSummaryReport_Click(Status As String)

Dim stDocName As String
 DoCmd.Hourglass True

    If Status = "Open" Then
        stDocName = "Executive_Summary_by_Director_Open"
        DoCmd.OpenReport stDocName, acPreview
        DoCmd.RunCommand acCmdZoom150
    Else    'Assumes "Due"
        stDocName = "Executive_Summary_by_Director"
        DoCmd.OpenReport stDocName, acPreview
        DoCmd.RunCommand acCmdZoom150

End Sub

It SUB name is the name provided by Access using the Command button name and adding _Click to the end. Here I show how I tried to add a parameter but could never pass the parameter directly from the Command button event window. I saw the () after the name and though that passing a value would be possible, perhaps not.

MajP, your solution will work, I was just trying to avoid having multiple, or additional, SUB's that basicly do the same thing to reduce the probabiliy of errors.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top