Select Query using Access VBA

Trying to write a select query to count a recordset.

I include my test code.
Dim strSQL As String

strSQL = "SELECT Count(*) AS [Count] " & _
"FROM Table1;"
DoCmd.RunSQL strSQL

Open in new window

ca1358Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gemartiCommented:
queryName = "Select * From Table1"

strSQL = DCount("*", queryName)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
What is the problem?
0
ca1358Author Commented:
My version  
Runtime error 2342 a RunSQL action requires an argument consisting of SQL statement.

using this this
queryName = "Select * From Table1"

strSQL = DCount("*", queryName)
 
I am getting an MS Access database engine cannot find the input table or query "Select * From Table1" Make sure it exist and spelled correctly.

If I run the query by using Access query design, it works.

This is the SQL view

SELECT Count(*) AS [Count]
FROM Table1;

But I need to put this into VBA code.
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You sure that the table name is really TABLE1? Maybe you need to change it to the correct table name.
0
Rey Obrero (Capricorn1)Commented:
just use dcount

dim xCount
xCount=dcount("*","table1")

0
Rey Obrero (Capricorn1)Commented:
if you want to use the select statement, you can use it to open recordset

dim rs as dao.recordset
Dim strSQL As String

strSQL = "SELECT Count(*) AS [Count] " & _
"FROM Table1;"

set rs=currentdb.openrecordset(strSql)
msgbox rs("[Count]")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
gemarti is mostly right about using DCOUNT().  I would normally assign the result of DCOUNT() to a Long Integer variable, not a string variable.  Also, note that strSQL implies that it contains a query string, when it is actually being used to contain the results of the DCOUNT() function.  This is probably the second-most simple answer.

If you are only looking for a count of records in a table (your posted example), then you can get that directly from the tabledef object.
Example:
msgbox "the number of table1 records is " & currentdb.tabledefs("table1").RecordCount

Instead of DoCmd.RunSQL, you should instantiate a recordset variable to receive the results of the Select Count query.
Example:
Dim rs As Recordset

Set rs = CurrentDb.Openrecordset("SELECT Count(*) AS [Count] FROM Table1;" 
)

msgbox "the number of table1 records is " & rs![Count]

Open in new window

0
ca1358Author Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.