excute and SQL query

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

I have created a form for a user to select values from
multiple combo boxes. From that I want to run a query
based on the select criteria after pressing a command
button. I have built the SQL query (i think) and want to
create and display the query on the click. How do I do
this? this is
what i have. what comes next? Thanks

Private Sub Command7_Click()

dateend = Me.cbodateend.Value
datestart = Me.cboDatestart.Value
media = Me.cboMediabox.Value

If IsNull(media) = True Then
MsgBox ("Please specify the meida")
End If
If IsNull(datestart) = True Then
MsgBox ("Please specify the starting date")
End If
If IsNull(dateend) = True Then
MsgBox ("Please specify the ending date")
End If

Dim strSQL As String
strSQL = "SELECT FROM (tbldata INNER .....

End Sub
 
This displays my SWL string but doesn't run the query
I would like VBA to excute the query and then export it
to excel. I am lost on how to get it to create and open
the query. Any help?
 
I suggest that you use the QBE grid to create a query with a
trivial SQL statement:
SELECT * FROM sometable

For the sake of discussion, lets name the query MyQuery.

With that in place, you code can then modify that query's
SQL property to whatever you want:

Dim db As Database
Dim qdf As QueryDef
Set db = CurrentDb()
Set qdf = db.QueryDefs("MyQuery")
strSQL = "SELECT FROM (tbldata INNER .....
qdf.SQL = strSQL

Now you can open the query for display in sheet view:
DoCmd.OpenQuery "MyQuery"

or export it to Excel using the TansferSpreadsheet method
(see Help for details of all its options).
 
I'm not sure where you are trying to show it. If it is in
a subform on the same form as the parameters are
collected, try

Me![myFormName].Form.RecordSource = strSQL
 
Back
Top