excute and SQL query

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
 
C

Chris

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?
 
T

Tim Ferguson

I would like VBA to excute the query and then export it
to excel.

Have you looked at help for the TransferDatabase and TransferSpreadsheet
methods?


Tim F
 
M

Marshall Barton

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).
 
R

Ranx

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Running A query from VBA 1
Calling a Function in a Query and Form 10
Public Function Problems 8
"select case" 2
help with a VBA 4
Error 3075 Missing Operator 7
HELP NEEDED WITH REPORT 1
creating search box in vba 7

Top