RUN SQL Statement

lovemuscle2005

Board Regular
Joined
Dec 28, 2004
Messages
99
Hi All,

I'm trying to run the following SQL statement via RUNSQL, but not getting very far, can anyone help?

Thanks


Option Compare Database

Function BLANKSSQL()
On Error GoTo BLANKSSQL_Err
DoCmd.RunSQL "SELECT [Store Lookup List].[Store No], [Store Lookup List].[Store Name], [Store Lookup List].Region, [Store Lookup List].Division FROM (([Store Lookup List] LEFT JOIN [MQ1 Test] ON [Store Lookup List].[Store No] = [MQ1 Test].[Store No]) LEFT JOIN [MQ2 Test] ON [Store Lookup List].[Store No] = [MQ2 Test].[Store No]) LEFT JOIN [MQ3 Test] ON [Store Lookup List].[Store No] = [MQ3 Test].[Store No] WHERE ((([MQ1 Test].[New Contracts]) Is Null));"
TestSQL_Exit:
Exit Function
BLANKSSQL_Err:
MsgBox Error$
Resume TestSQL_Exit
End Function
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
What problems are you actually having?

Why are you using a Function instead of a Sub?
 
Upvote 0
Got the same error myself.

Try testing your SQL string in a normal query.

I can't quite see anything wrong with it but that might help, maybe there's a space missing somewhere.

By the way I think a better way to do this sort of thing is to build the SQL string piece by piece.
Code:
strSQL = "SELECT [Store Lookup List].[Store No], [Store Lookup List].[Store Name], [Store Lookup List].Region, [Store Lookup List].Division "
strSQL = strSQL & "FROM (([Store Lookup List] LEFT JOIN [MQ1 Test] ON [Store Lookup List].[Store No] = [MQ1 Test].[Store No]) LEFT JOIN [MQ2 Test] ON [Store Lookup List].[Store No] = [MQ2 Test].[Store No]) LEFT JOIN [MQ3 Test] ON [Store Lookup List].[Store No] = [MQ3 Test].[Store No] "
strSQL = strSQL & "WHERE ((([MQ1 Test].[New Contracts]) Is Null));"

DoCmd.RunSQL strsSQL
 
Upvote 0
CORRECTION

You cannot use the DoCmd.RunSQL statement with a SELECT query. Instead, you may use
Code:
Set rs = CurrentDatabase.OpenRecordset(strSQL)
that Norie nearly had. Again, DoCmd.RunSQL will NOT work with SELECT queries.

HTH (y)
P

D'oh! Please note above corrected code line. :oops:
 
Upvote 0
Hmm, can't get that to work at all Philem.
Usually I do this when I need to create an actual query object.

Code:
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String

Set dbs = CurrentDb()

strSQL = "various valid SQL"

Set qdf = dbs.CreateQueryDef(aqryName, strSQL)

If all I need to do is open a recordset object - to walk through it, for example, I'd substitute this for the Set qdf statement

Code:
Set rs = dbs.OpenRecordset(strSQL, dbOpenDynaset)

vary the dbOpenDynaset option depending on need.

Mike
 
Upvote 0
You're right, Mike! <blush> Made an error which has been corrected.

Apologies, all!
P
 
Upvote 0
I tried that too when I was fiddling with the statement.
I think I'm missing something.

I usually do this as a two step process.

Set dbs = CurrentDb()
Set rs = dbs.OpenRecordset(sqlstring, mode)

In A2K, CurrentDb() is not equivalent to CurrentDatabase

Am I missing a reference to see that as an option or are you in a different version of Access?

Mike
 
Upvote 0
I normally don't use the second argument of OpenRecordset. Also, I'm running A97. Corporate policy. :rolleyes: Let me attach some working A97 code:
Code:
Dim db as Database
Dim rs as Recordset
Dim strSQL as String

Set db = CurrentDb
strsql = "SELECT * from Table1;"
set rs = db.OpenRecordset(strSQL)
I have a DAO3.5 reference set.

:cool:
P
 
Upvote 0
What wasn't working is the object "CurrentDatabase"
O2K seems to require "CurrentDb()"

That's unless 'CurrentDatabase' is actually a variable.
Looks like we're doing the exact same thing otherwise.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,889
Messages
6,162,624
Members
451,778
Latest member
ragananthony7911

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top