Getting an argument from one subroutine into another ??

pfiroz

Registered User.
Local time
Today, 08:33
Joined
Jan 21, 2009
Messages
27
Hello, :)What is the best way for me to get the value of a variable from a subroutine into another subroutine ?
The code I'm running is below, it calls the btnSearch_Click subroutine (a very long complex piece of code) in the same module. I just need to get the value of 'strFilter' from this in order for Print_Click() to work. How do I do that??
Appreciate any help!!
Code:
Private Sub Print_Click()
    Dim qryDef As QueryDef
    Call btnSearch_Click
    qrycriteria = "select * from tblmain where person_id in (" & strFilter & ")"
    Set qryDef = CurrentDb.QueryDefs("qrytesting")
    qryDef.SQL = qrycriteria
End Sub
 
That variable appears to be external to this process, so can you not just refer to it from the other routine just as you do here?
 
Hi,
Thanks for the reply. Yes I can but the btnSearch_Click routine does a complex query and then diplays the results based on the search string it pieces together called 'strFilter' . In Print_Click routine, I was trying to build a query that I could based a form off of to print the results from the initial search.
If I could somehow get the value of 'strFilter' to the Print_Click routine I think it would work. I guess I could go get the long complex piece of code that builds the 'strFilter' from btnSearch_Click but I thought there might be a easy way to just call btnSearch_Click and pass the value of 'strFilter' .
Whats do you think is the best way to do this. Basically, I just want to print the results of the initial query to the printer.
Appreciate any help!!
 
In the btnSearch_Click event, store the results in a form level variable (m_sPersonIDs) so that you can use that variable through out your form.
Then replace your strFilter with m_sPersonIDs

Or do something like this

Code:
Private Sub btnSearch_Click()
    GetPersonIds
End Sub

Private Sub btnPrint_Click()
    
    qrycriteria = "select * from tblmain where person_id in (" & GetPersonIds& ")"
    Set qryDef = CurrentDb.QueryDefs("qrytesting")
    qryDef.SQL = qrycriteria

End Sub

Private Function GetPersonIds() as string

    Dim IDs as string

    'do whatever the Search button suppose to do here....
    'Store the result in IDs

    GetPersonIds = IDs

End Function

This will allow you to use the Search Button without returning anything or use it for other purpose.

When print, the GetPersonIds function will be called and get whatever the IDs are and return straight to your query.

Hope this help.
 
Last edited:
Hey Thanks!!
I used your suggestion of copying the value on my form and making the text box invisible. Then I just called the value of the text box when I run the print subroutine.
Thanks! :)
 

Users who are viewing this thread

Back
Top Bottom