unvalid sql

J

Jacco

Hi everyone, I'm trying to run this SQL query with DoCmd.RunSQL but it keeps
giving a error

It should count the amount of aircraft with a lbk_id (a column in table
Aircraft)

Dim count_old_aircraft As String

count_old_aircraft = "SELECT Count(Aircraft.lbk_id) " & _
"AS CountOflbk_id FROM Aircraft " & _
"HAVING (((Count(Aircraft.lbk_id))<>0));"

DoCmd.RunSQL count_old_aircraft


Error:
2342 - A RunSQL action requires a argument consisting of an SQL statement.


Any help is welcome....

Jacco
(and if it works: how do I retrieve that number so I can display it in
MsgBox or something)
 
D

Dirk Goldgar

Jacco said:
Hi everyone, I'm trying to run this SQL query with DoCmd.RunSQL but
it keeps giving a error

It should count the amount of aircraft with a lbk_id (a column in
table Aircraft)

Dim count_old_aircraft As String

count_old_aircraft = "SELECT Count(Aircraft.lbk_id) " & _
"AS CountOflbk_id FROM Aircraft " & _
"HAVING
(((Count(Aircraft.lbk_id))<>0));"

DoCmd.RunSQL count_old_aircraft


Error:
2342 - A RunSQL action requires a argument consisting of an SQL
statement.


Any help is welcome....

Jacco
(and if it works: how do I retrieve that number so I can display it in
MsgBox or something)

RunSQL only works with action queries, not with SELECT queries. In
general, to get the results of a SELECT query in code, you have to open
a recordset on the query; however, in this case I believe you could get
the information you want by using the DCount function:

Dim lngCount As Long

lngCount = DCount("lbk_id", "Aircraft")

or maybe

lngCount = DCount("lbk_id", "Aircraft", "lbk_id <> 0")
 
J

Jacco

Dirk Goldgar said:
RunSQL only works with action queries, not with SELECT queries. In
general, to get the results of a SELECT query in code, you have to open
a recordset on the query; however, in this case I believe you could get
the information you want by using the DCount function:

Dim lngCount As Long

lngCount = DCount("lbk_id", "Aircraft")

or maybe

lngCount = DCount("lbk_id", "Aircraft", "lbk_id <> 0")

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
Thank you, it's the second option.
 

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

Run-time error '2342' 5
Access Form Coding 7
A SQL statement in VBA 7
VBA -- SQL 9
help on simple SQL VB 1
running an SQL query 3
Using Date fields in SQL 2
DoCmd.RunSQL 9

Top