Using SELECT COUNT problems

mattpfc

Active Member
Joined
Nov 21, 2002
Messages
299
Hi all,

I am using the following lines of code to count the number fo rows in a table but each time I get a error message saying " A RunSQL statement requires an argument consisting of a SQL statement"


Any ideas where I'm going wrong?

Code:
    sql_count_id = "SELECT COUNT (*) FROM Stalls WHERE event_id = " & Me.cbo_event_choice.Column(0) & " AND booked = true"
    
    DoCmd.RunSQL sql_count_id
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Can't execute select queries.
You *can* open them.

Code:
strSQL = "SELECT * FROM tblmainfile"
Set qdf = dbs.CreateQueryDef("sample", strSQL)
DoCmd.OpenQuery "sample", , acEdit

Why don't you try something like this:

Code:
Private Sub countit()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim intCnt As Integer

Set dbs = CurrentDb()
strSQL = "SELECT * FROM tblmainfile"
Set rs = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
  .MoveLast
  intCnt = .RecordCount
End With
MsgBox intCnt

End Sub

Mike
 
Upvote 0

Forum statistics

Threads
1,221,668
Messages
6,161,163
Members
451,687
Latest member
KENNETH ROGERS

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