View SQL Count Records

Posted on 2017-05-19
Medium Priority
Last Modified: 2017-05-19

How can I view the count from the code below?

??  msgbox "My Count is " & mycount

Dim mycount As String
mycount = "SELECT Count(*) AS MyCount " & vbCrLf & _
"FROM BackgroundCheck " & vbCrLf & _
"WHERE (((BackgroundCheck.Background) Not Like ""Access Denied"" And (BackgroundCheck.Background)=""Waiver Denied"" And (BackgroundCheck.Background)=""Waiver Granted"") AND ((BackgroundCheck.DateNeededCheck)<DateAdd(""yyyy"",-1,Date()-1)));"

Open in new window

Question by:Ernest Grogg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 1000 total points
ID: 42142769
try this
sub MyCount()
dim xCount as integer
xCount=Dcount("*","BackgroundCheck","[Background] In('Waiver Denied','Waiver Granted') And [DateNeededCheck] <DateAdd('yyyy',-1,Date()-1)")

msgbox "My count is " & xCount

end sub
LVL 35

Assisted Solution

ste5an earned 1000 total points
ID: 42142780

Dim Sql As String  
Dim rs As DAO.Recordset
Dim myCount As Long
Dim Condition As String

Sql = _
  "SELECT Count(*) " & _
  "FROM BackgroundCheck " & _
  "WHERE Background IN ( 'Waiver Denied', 'Waiver Granted' ) " & _
  "AND DateNeededCheck <DateAdd('yyyy',-1,Date()-1);" 

Set rs = CurrentDb.OpenRecordset(Sql)
myCount = rs.Fields(1).Value
Condition = _
  "Background IN ( 'Waiver Denied', 'Waiver Granted' ) " & _
  "AND DateNeededCheck <DateAdd('yyyy',-1,Date()-1);" 
myCount = DCount("*", "BackgroundCheck", Condition)

Open in new window


Author Comment

by:Ernest Grogg
ID: 42142925
Great Solutions!


I had to change ste5an's just a bit to get it to work:  (the condition had a syntax error, so I removed and changed the fields to 0)

I also had the string slightly wrong, so this below I had to have it looking just for the 1 .  


Rey Obrero (Capricorn1) was good to go also...granting both as great solutions.

Dim Sql As String
Dim rs As DAO.Recordset
Dim mycount As Long

Sql = _
  "SELECT Count(*) " & _
  "FROM BackgroundCheck " & _
  "WHERE Background IN ( 'Access Granted' ) " & _
  "AND DateNeededCheck <DateAdd('yyyy',-1,Date()-1);"

Set rs = CurrentDb.OpenRecordset(Sql)
mycount = rs.Fields(0)

MsgBox "Total Record needing moved: " & mycount

Open in new window


Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…
Suggested Courses

764 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