[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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
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 36

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

834 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