Go Back   DeveloperBarn Forums > Databases > Microsoft Access

Sponsored Links

Discuss "Query Counts" in the Microsoft Access forum.

Microsoft Access - Microsoft Access is a database for small to medium applications. Learn tips and tricks and best database practices here.


Closed Thread « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1  
Old May 13th, 2008, 01:40 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default Query Counts

I am trying to get a record count from a Query.
I want to get this count to a variable and then place it in a textbox...

I have a dropdown box on my form that allows the user to select a Field Name from the table.
It then populates a second dropdown with the unique values from that field, which the user then selects

They can build 4 querys like this...although they might only write one...returning all the unique values from a field....

I then put them together like this....

ARTICLE=SHIRT AND CATEG=STORE AND DISPO=HOLD;

the "critStr" is a variable that holds the string above


I THEN Was trying to run this:

Code:
Dim MySQL3 As String
MySQL3 = "SELECT Count(*) as numRecords FROM dbo_Core2 WHERE " & critStr & ";"
DoCmd.RunSQL MySQL3
MsgBox MySQL3
The message box returns this....but I dont think this was right...I dont know how to get the query to run and get the value?????

SELECT Count(*) numRecords FROM db_Core2 WHERE ARTICLE=SHIRT AND CATEG=STORE AND DISPO=HOLD;




I THEN started with this....BUT the below code only returns a value of 1...I know I have more records than that...

I DONT knwo what I am doing right now.....All I am trying to do is run a query that gives me a count from a variable holding the string...

PLease help....



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"
Sponsored Links
  #2  
Old May 13th, 2008, 01:49 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default

I am also trying this...

I cant figure this out....I dotn think this is that hard to accomplish...



Code:
Dim LTotal As String

'LTotal = DCount("ARTICLE", "dbo_Core2", critStr)
'LTotal = DCount("ARTICLE", "dbo_Core2", " & critStr & ")
LTotal = DCount("ARTICLE", "dbo_Core2", "' & critStr & '")

MsgBox LTotal
  #3  
Old May 13th, 2008, 01:50 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

In your first instance, you will need to put quotes around the values in your Where clause, as in:
Code:
SELECT Count(*) numRecords FROM db_Core2 
WHERE ARTICLE='SHIRT' AND CATEG='STORE' AND DISPO='HOLD';
In you second case, you're getting 1 because of:
Code:
MyRecordCount = RCountRS.RecordCount
which is the number of records returned, not the value.
  #4  
Old May 13th, 2008, 01:51 PM
mehere's Avatar
Super Sarcasm Mistress


 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 143
Thanks: 10
Thanked 27 Times in 25 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

is this just what you placed here or is this actually how it is
Code:
WHERE ARTICLE=SHIRT AND CATEG=STORE AND DISPO=HOLD
because your values should have single quotes around them
Code:
WHERE ARTICLE='SHIRT' AND CATEG='STORE' AND DISPO='HOLD'
and are you positive that you have more than one record that has those values?

Comments on this post
Jaykappy agrees:
__________________
Quote of the Month:
Regret: It hurts to admit when you make mistakes - but when they're big enough, the pain only lasts a second.

Questions to Ponder:
Could it be that all those trick-or-treaters wearing sheets aren’t going as ghosts but as mattresses?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
  #5  
Old May 13th, 2008, 01:58 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default

FIRST EXAMPLE:

ARTICLE=SHIRT AND CATEG=STORE AND DISPO=HOLD;

This is exaclty how it is returning the string in the variable......so it seems that I am missing the ' ' ..I will get those in and test again...



SECOND EXAMPLE:
where I am getting a return value of 1...

I am looking for the number of records that meet the query...

This should return all the values in the table right?

Code:
SQLstmt = "SELECT Count(*) As CNT FROM dbo_Core2"
  #6  
Old May 13th, 2008, 02:05 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

RecordSet.RecordCount returns the number of records in the RecordSet -- which for your query will always be 1.
  #7  
Old May 13th, 2008, 02:08 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default

I changed the string variable (critStr) and now it reads with ' '

DISPO = 'HOLD' AND ...........

Although I am not sure how to run the query and get it to a variable...



I do the below and I still get an error:

RUn Time error 2342
A RunSQL action requires an argument consisting of an SQL statement



This is exacly what is being displayed int he MEssage Box:

SELECT Count(*) as numRecords FROM dbo_Core2 WHERE CATEG = 'NARCOTICS' AND DISPO = 'HOLD' AND ARTICLE = 'MARJIUANA';



Code:
Dim MySQL3 As String
MySQL3 = "SELECT Count(*) as numRecords FROM dbo_Core2 WHERE " & critStr & ";"
MsgBox MySQL3
DoCmd.RunSQL MySQL3
MsgBox MySQL3
  #8  
Old May 13th, 2008, 02:10 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default

How can I change that to get the number of records return from the query?



--------------------------------------------------------------------------------

RecordSet.RecordCount returns the number of records in the RecordSet -- which for your query will always be 1.
  #9  
Old May 13th, 2008, 02:25 PM
Wolffy's Avatar
Slaprentice of Wolves

 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 175
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

Try something like
Code:
Dim RCountDB As Database
Dim RCountRS As Recordset
Dim SQLstmt As String
Dim MyRecordCount As Integer

SQLstmt = "SELECT Count(*) numRecords FROM db_Core2 WHERE ARTICLE='SHIRT' AND CATEG='STORE' AND DISPO='HOLD'";

Set RCountDB = CurrentDb()
Set RCountRS = RCountDB.OpenRecordset(SQLstmt)
MyRecordCount = RCountRS("numRecords")
RCountRS.Close
  #10  
Old May 13th, 2008, 02:35 PM
Barn Newbie
 
Join Date: Apr 2008
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 1
Jaykappy is an unknown quantity at this point
Default

Its not liking this line..

SQLstmt = "SELECT Count(*) numRecords FROM db_Core2 WHERE ARTICLE='SHIRT' AND CATEG='STORE' AND DISPO='HOLD'";


I tried 'HOLD'";" but thats not it....
Closed Thread

  DeveloperBarn Forums > Databases > Microsoft Access

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL VBA Query Jaykappy Microsoft Access 12 May 8th, 2008 09:30 AM
[ASP/VBScript] Using Dropdown List Value in Database SQL Query richyrich Code Samples 0 April 2nd, 2008 10:05 AM


All times are GMT -4. The time now is 02:58 PM.



Content Relevant URLs by vBSEO 3.2.0