|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Count Query
I can build a query with Access Query builder and get a number/count of the number of records that meet specific criteria...I then try and write that in VBA and get confused and it dosent work...
critStr is a string built by user input... Basically like the select below... Any thoughts? ERROR: Run time error 2342 A RunSQL action requires an argument consisting of an SQL Statement TRYING THIS: Dim MySQL3 As String MySQL3 = "SELECT Count(*) as numRecords FROM dbo_Core2 WHERE " & critStr & ";" DoCmd.RunSQL MySQL3 MsgBox MySQL3 THIS WORKS: 'SELECT Count(dbo_Core2.CASE_NO) AS CountOfCASE_NO, dbo_Core2.ARTICLE, dbo_Core2.CATEG, dbo_Core2.DISPO 'FROM dbo_Core2 'GROUP BY dbo_Core2.ARTICLE, dbo_Core2.CATEG, dbo_Core2.DISPO 'HAVING (((dbo_Core2.ARTICLE)="SHIRT") AND ((dbo_Core2.CATEG)="STORE") AND ((dbo_Core2.DISPO)="HOLD")); Last edited by Jaykappy : May 13th, 2008 at 11:59 AM. |
|
#2
|
|||
|
|||
|
This is what the message box returns...
SELECT Count(*) numRecords FROM db_Core2 WHERE ARTICLE=SHIRT AND CATEG=STORE AND DISPO=HOLD; BUT it errors out... Thoughts |
|
#3
|
|||
|
|||
|
Just looking to get the query to work and assign it to a varriable so I can populate a text box...
|
|
#4
|
|||
|
|||
|
This is what I am trying but I keep getting 1 for a return value...I have way more than that...
Code:
Dim RCountDB As Database
Dim RCountRS As Recordset
Dim SQLstmt As String
'Dim Count As Integer
Dim MyRecordCount As Integer
SQLstmt = "SELECT Count(*) As CNT FROM dbo_Core2"
Set RCountDB = CurrentDb
Set RCountRS = RCountDB.OpenRecordset(SQLstmt)
'Count = RCountRS.RecordCount
'rst.MoveLast
MyRecordCount = 0
If RCountRS.EOF <> True Then
RCountRS.MoveLast
RCountRS.MoveFirst
MyRecordCount = RCountRS.RecordCount
End If
MsgBox "Ending"
MsgBox MyRecordCount
MsgBox "END"
|
|
#5
|
|||
|
|||
|
You need to specify a field for the count function. For best results you want to specify a field that is ALWAYS populated. I typically use the primary key field.
|
|
#6
|
|||
|
|||
|
Code:
Dim RCountDB As Database Dim RCountRS As Recordset Dim SQLstmt As String Dim MyRecordCount As Integer SQLstmt = "SELECT Count(*) As CNT FROM dbo_Core2" Set RCountDB = CurrentDb Set RCountRS = RCountDB.OpenRecordset(SQLstmt) rst.MoveLast myrecordcount = RCountRs.Recordcount MsgBox "Ending" MsgBox MyRecordCount MsgBox "END" The problem is that you were moving to the last record then moving back to the first record before you got the count and when you moved back to the first record you're always going to get a count of 1. EDIT: one other note, when you loop through a data set you will also have to increment the record as in RCountRS.movenext otherwise it will just loop through the same record forever.
__________________
---------------- If we've helped you and you have solved your problem please post that it's been resolved so we know! The suspense kills me! |
|
#7
|
|||
|
|||
|
Got it......Thanks to you all and developer barn
Code:
Dim RCountDB As Database
Dim RCountRS As Recordset
Dim SQLstmt As String
Dim MyRecordCount As Integer
'SQLstmt = "SELECT Count(*) As numRecords FROM dbo_Core2 WHERE ARTICLE='MARIJUANA' AND CATEG='NARCOTICS' AND DISPO='HOLD';"
SQLstmt = "SELECT Count(*) As numRecords FROM dbo_Core2 WHERE " & critStr & ";"
Set RCountDB = CurrentDb()
Set RCountRS = RCountDB.OpenRecordset(SQLstmt)
MyRecordCount = RCountRS("numRecords")
RCountRS.Close
If MyRecordCount > 0 Then
'MsgBox MyRecordCount
MsgBox " There were " & MyRecordCount & " Records that match your criteria."
Text141 = MyRecordCount
Else
MsgBox " There were no Records that matched your query"
End If
|
![]() |
| Viewing: ASP Free Forums > Database > Microsoft Access Help > Count Query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|