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

Passing parameters from forms to queries in VBA

Status
Not open for further replies.

shanedavid1981

Programmer
Feb 3, 2005
54
US
Hey,

I'm a bit stuck as to how I would pass a parameter from a form to a query in VBA... look at this:
Code:
Private Sub Command5_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim acQuery As QueryDef
Dim objRST As Recordset
Dim strQueryName As String
Dim strquerytext As String
Combo2.SetFocus
strquerytext = Combo2.Text
strQueryName = "OpsAttSummarySearch"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Add
Set objRST = Application.CurrentDb.OpenRecordset(strQueryName)
Set xlSheet = xlWorkbook.Sheets(1)
With xlSheet
    .Cells.CopyFromRecordset objRST
    .Name = strquerytext
End With
Set objRST = Nothing
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing

    
End Sub

Now when I run this Excel opens, but no data is delievered.. an error message in the background tells me a parameter is required... the query requires one parameter from the form this code is triggered from... what do I need to do to get this parameter passed correctly?

Thanks :eek:)
 
First, acQuery is a global constant.
You have to play with the QueryDef.Parameters collection and the Eval function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Okay, how would I go about that?

I've done direct SQL thru VBA,a nd outputting simple queries to e-mail with VBA, I didn't think there would be much difficulty in passing a parameter from a control that is already open on the form...
 
Another way is to open the query prior to play with the recordset:
DoCmd.OpenQuery strQueryName

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top