Solved SQL Statement

Eljefegeneo

Still trying to learn
Local time
Today, 07:32
Joined
Jan 10, 2011
Messages
902
What am I doing wrong? This is my sql statement from the query:
Code:
 SELECT TOP 25 tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList
FROM tblMain
WHERE ((Not (tblMain.Email) Is Null));
And this is my SQL statement in my vba code:
Code:
 Dim sSql As String
sSql = "SELECT TOP 25 tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList" & _
" FROM tblMain" & _
" WHERE ((Not (tblMain.Email) Is Null));"
DoCmd.RunSQL sSql
The former works of course. Running the latter says it is an inconsistent SQL statement.

Where am I going wrong?
 
Hi. I could be wrong but maybe, I think, RunSQL is for actions queries only - not for select queries.
 
So, what do I use for the run statement?
 
Yes. I want to modify the query (Sql statement) with VBA using a parameter from a form. The use depends on whether or not I can run the SQL statement with VBA.
 
Can you share the screenshot that shows "inconsistent SQL statement" ?
I'm curious as that error doesn't sound familiar to me.

You should consider displaying the results of the query on a form or report, or exporting them, rather than directly opening what I assume is an updateable query. Opening it directly puts you in the same risk category as if you Opened a Table for a user - which then opens you up to people manually editing/damaging data in a completely uncontrolled interface environment.

See this for a method that I find very convenient in most 'parameter' needs cases.
 
My current code:
Code:
 Dim sSQL As String
sSQL = "SELECT TOP 25 tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList" & _
" FROM tblMain" & _
" WHERE ((Not (tblMain.Email) Is Null));"
DoCmd.RunSQL sSQL[Code]
 

Attachments

  • ErrorStatement.jpg
    ErrorStatement.jpg
    33.6 KB · Views: 247
what is the parameter you need to add?
you can add it to your saved Query, eg:

Dim sSQL As String
Dim qd As DAO.Querydef
Dim db As DAO.Database

sSQL = "SELECT TOP " & Me!txtNumber & " tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList" & _
" FROM tblMain" & _
" WHERE ((Not (tblMain.Email) Is Null));"

Set db = Currentdb
Set qd = db.Querydefs("yourQueryName")
qd.SQL = sSQL
Set qd=Nothing
Set db=Nothing

DoCmd.OpenQuery "yourQueryName"
 
My current code:
Code:
 Dim sSQL As String
sSQL = "SELECT TOP 25 tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList" & _
" FROM tblMain" & _
" WHERE ((Not (tblMain.Email) Is Null));"
DoCmd.RunSQL sSQL[Code]
Hi. Actually, I agree with @Isaac that you should consider using a Form to display the result of the SQL statement. Check out this article on RunSQL.

Specifically, this portion.
1600994438722.png
 
what is the parameter you need to add?
you can add it to your saved Query, eg:

Dim sSQL As String
Dim qd As DAO.Querydef
Dim db As DAO.Database

sSQL = "SELECT TOP " & Me!txtNumber & " tblMain.ClientID, tblMain.FirstName, tblMain.Organization, tblMain.Email, tblMain.OnSendList" & _
" FROM tblMain" & _
" WHERE ((Not (tblMain.Email) Is Null));"

Set db = Currentdb
Set qd = db.Querydefs("yourQueryName")
qd.SQL = sSQL
Set qd=Nothing
Set db=Nothing

DoCmd.OpenQuery "yourQueryName"
 
Thank you arnelgp. I see what you are saying now. You can only modify an existing select query this way. by the way, you have solved two problems for me today. Guess I can now go and make dinner for my wife.
 
enjoy your evening and observe distancing.
 
She is involved with local politics - on the city council as vice mayor - so I definitely keep my distance.
 

Users who are viewing this thread

Back
Top Bottom