Solved

Get record count using SQL statement in Access

Posted on 2008-09-30
6
979 Views
Last Modified: 2013-11-28
I would like to find the 'count' of the field 'QCReportNo' and save it in a variable.
I've attached my code but can't even get it to compile.  I get the error...
    Wrong number of arguments or invalid property assignment.
strSQL = "SELECT Count(tblDaily.QCReportNo) "

strSQL = strSQL & "FROM tblDaily INNER JOIN (tblInstrumentsUsed INNER JOIN tblInstrumentsDesc ON tblInstrumentsUsed.InstrumentDescId = tblInstrumentsDesc.InstrumentDescId) ON tblDaily.QCReportNo = tblInstrumentsUsed.QCReportNo "

strSQL = strSQL & "WHERE (((tblDaily.QCReportNo)=[Forms]![frmPrevDailyDFT]![cboQCRptNo]))"

DoCmd.RunSQL (strSQL)

dblCount = Count(QCReportNo)

Open in new window

0
Comment
Question by:BobRosas
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:aaronakin
ID: 22607625
Try giving the counted value an alias.  i.e. "SELECT Count(tblDaily.QCReportNo) AS QCReportNoCount "

Then, try assigning dblCount = QCReportNoCount.
0
 

Author Comment

by:BobRosas
ID: 22607718
Thank you so much for your quick response.  I've changed my code and reposted.  Now I get the message 'Variable not defined" for the alias value.  However it could be related to my statement.
I tried running the code with the last line commented out and it compiles but on the
Do.Cmd.RunSQL (strSQL)
I get the error...
A RunSQL action requires an argument consisting of an SQL statement.
If I just want a count do I even need the do.cmd line?
Thanks in advance.
    strSQL = "SELECT Count(tblDaily.QCReportNo) As RptNoCount "

    strSQL = strSQL & "FROM tblDaily INNER JOIN (tblInstrumentsUsed INNER JOIN tblInstrumentsDesc ON tblInstrumentsUsed.InstrumentDescId = tblInstrumentsDesc.InstrumentDescId) ON tblDaily.QCReportNo = tblInstrumentsUsed.QCReportNo "

    strSQL = strSQL & "WHERE (((tblDaily.QCReportNo)=[Forms]![frmPrevDailyDFT]![cboQCRptNo]))"

    DoCmd.RunSQL (strSQL)

    dblCount = RptNoCount  

Open in new window

0
 

Author Comment

by:BobRosas
ID: 22607828
I changed my SQL code and added a 'group by' because I think it needs it to give me the right count.  But the errors are the same as above.

strSQL = "SELECT Count(tblDaily.QCReportNo) AS RptNoCnt "

     strSQL = strSQL & "FROM tblDaily INNER JOIN (tblInstrumentsUsed INNER JOIN tblInstrumentsDesc ON tblInstrumentsUsed.InstrumentDescId = tblInstrumentsDesc.InstrumentDescId) ON tblDaily.QCReportNo = tblInstrumentsUsed.QCReportNo "

     strSQL = strSQL & "GROUP BY tblDaily.QCReportNo "

     strSQL = strSQL & "HAVING (((tblDaily.QCReportNo)=[Forms]![frmPrevDailyDFT]![cboQCRptNo]))"

Open in new window

0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 11

Expert Comment

by:aaronakin
ID: 22607844
It's a while since I last used Access.  Check out this article though and see if it helps you.
0
 
LVL 11

Accepted Solution

by:
aaronakin earned 125 total points
ID: 22607845
0
 

Author Closing Comment

by:BobRosas
ID: 31501634
Yes that helped.  I got it working
Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now