Insert variable into SQL string

T

Tawreq

Sorry for cross-post, think I put it in wrong discussion group first
time around...

This is so frustrating - all I want to do is pass the variable of a
combobox into a sql query and run it - does anyone have any ideas? I've

looked around on the groups and found that "& variable &" seems to work

but I'm getting a 'runsql requires an argument that consists of SQL'
error on the following
___


Private Sub Combo0_AfterUpdate()
Dim val As String
val = Combo0.Value


DoCmd.RunSQL ("SELECT Together.Source.[Application Name], ['" & val &
"'] AS Expr1, Together.Target.[Application Name], Together.[Feed
Name/Ref] FROM Together WHERE (((Together.Target.[Application Name])='"

& val & "')) OR (((Together.Source.[Application Name])='" & val &
"'));")


End Sub


__


Thanks a million!
 
B

Baz

You can't use RunSQL to run a row-returning query, it is for action queries
i.e. those that change ot create data. What are you trying to achieve? If
you want to see the query results, create the query in the query designer as
a parameter query and save it. Then you can use DoCmd.OpenQuery to show
it's results.
 
D

Douglas J. Steele

There are a few problems with what you have.

First, you cannot use RunSQL with a SELECT query: it can only be used with
Action queries (INSERT INTO, UPDATE, DELETE or SELECT...INTO)

However, even if you create a recordset with your query, your SQL is
incorrect.

Since val is supposed to be some text you want included as a constant in
your query, get rid of the square brackets around it in the SQL. Access sees
square brackets, and assumes it's supposed to be a field name.

Also, I have no idea what you're trying to do with stuff like
Together.Source.[Application Name] and Together.Target.[Application Name].
In Jet SQL (which Access uses), you can only use 2 levels:
TableName.FieldName (and TableName is optional)
 

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

Top