473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MS Access - Writing SQL to count for conditions

I am not very familiar with SQL.

I try to count records, with a field "Mon"
which contain "AL" or "UL"

I am not sure is my SQL statement correct.
When I execute the SQL, error message display
"A RunSQL action requires an argument consisting of
an SQL statement."

I am also thinking of using set filter instead of
SQL but not sure which is a preferable method.

Here my code
Expand|Select|Wrap|Line Numbers
  1.    Dim dbs1 As DAO.Database
  2.    Dim rst1 As DAO.Recordset
  3.    Dim sSQL As String
  4.  
  5.  
  6.    Set dbs1 = CurrentDb()
  7.    sSQL = "SELECT TblTempAttendance.EmpName, Count(TblTempAttendance.mon) AS MonLeave from TblTempAttendance where TblTempAttendance.Mon  Like '*" & "AL" & "*' OR TblTempAttendance.Mon Like '*" & "UL" & "*' GROUP BY TblTempAttendance.EmpName HAVING TblTempAttendance.empName like '*" & EmpName & "*'"
  8.    DoCmd.RunSQL sSQL
Sep 26 '11 #1
6 1810
Rabbit
12,516 Expert Mod 8TB
RunSQL is for action queries. What you have is a select query. You need to use OpenRecordset for that and assign the result to a recordset variable.
Sep 26 '11 #2
NeoPa
32,556 Expert Mod 16PB
As this requires counting of items involved within a GROUP BY clause, and DCount() doesn't handle SQL as its Domain parameter, a recordset does appear to be required. I would consider using SQL as follows though, for simplicity :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. strSQL = "SELECT   [EmpName]" & _
  4.          "       , Count(*) AS MonLeave " & _
  5.          "FROM     [TblTempAttendance] " & _
  6.          "WHERE    ([Mon] Like '*{AU}L*')" & _
  7.          "  AND    ([EmpName] Like '*" & Me.EmpName & "*') " & _
  8.          "GROUP BY [EmpName]"
Sep 26 '11 #3
Thanks for the beautiful SQL Statement, I included the OpenRecordset.

It work!!!

Expand|Select|Wrap|Line Numbers
  1.  <SQL Statement >
  2.   Set rst1 = CurrentDb.OpenRecordset(SQL)
  3.   result = rst1!MonLeave
Sep 27 '11 #4
Thank for your advise of using OpenRecordset. Now I see the dfferent
between action query and open query.
Sep 27 '11 #5
NeoPa
32,556 Expert Mod 16PB
NB. This SQL, as written (to match your original explanation), can return multiple records so you should be aware that your code will return only the first of these.
Sep 27 '11 #6
Yes, it is what I want, Noted with thanks
Sep 27 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: thomasp | last post by:
I am trying to get a record count of a PHP query on a MS Acess database using ODBC with a DSN for MS ACCESS connection. I got this code from the PHP manual user notes. It seems to return the...
4
by: bhbgroup | last post by:
I have a query on one large table. I only add one condition, i.e. a date (the SQL reads like 'where date > parameterdate'. This query is rather quick if 'parameterdate' is either explicitly...
2
by: cajan341 | last post by:
Hello I am new to access, I have a report that has a list of classes in the section that is called class header. I want to count how many classes are on my report? and then divide this by another...
1
by: deshg | last post by:
Hey everyone, I think i am being kind of stupid but i can't quite work out how to do it and wondered if anyone might be able to help!? I'm using MS Access 2002 on Windows XP FYI. I have a...
9
by: Geneman | last post by:
I am using the current code to make a button visible. Private Sub Form_Current() If Me.related = "X" Then Me.addinfo.Visible = True Else Me.addinfo.Visible = False End If End Sub
1
by: Baskee | last post by:
Hi, Normally in if conditions, the constants are placed at the end , i.e. if (Variable != CONSTANT) or if( function_call() != CONSTANT) But some people write in the opposite way, if(CONSTANT...
1
by: icenemesis | last post by:
Using Access 2000 on WinXP Alright so here's how what I have is set up, well simplified version. I have a Program Table, a Procedure Table and a Status Table. Program - Table ProgramID...
1
by: Marks | last post by:
Howdy, Need some help on this one.... need to get access to count the number of records have a value of "Completed" in field1 and repeat on with other values...
13
by: craigchalmers | last post by:
Hi I am a complete novice so hope someone can shed some light on my problem/goal. I have an access database with some records in it. i have two fields 1) ArrivalDate 2) ReturnDate
3
by: J Hall | last post by:
Access 2003. I have a query which pulls in a list if ID numbers with about 6 check box fields. In my report, I need to know how to count the number of ID numbers that have 2 or more of the fields...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.