473,405 Members | 2,444 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,405 software developers and data experts.

Counting records to diplay an error message.

16
Hi all,

There are three tables in my database user, job and booking. It’s a job booking system so users login and make a booking against a job. I’m trying to get a button to display an error message if they try to make a booking but already have made 15 or more.

Below is the code I’ve got so far can anyone help me to get it working or point me in the right direction.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnEngCreateBooking_Click()
  2.  
  3. Dim username As String
  4. Dim strSQL As String
  5.  
  6. strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE userId = username AND jobStatus = open "
  7. DoCmd.RunSQL (strSQL)
  8.  
  9.  
  10. If numberOfBookings <= 15 Then
  11.    MsgBox "You have 15 or more open bookings, please close bookings before proceeding"
  12.  
  13.  
  14. DoCmd.OpenForm "frmEngCreateBooking"
  15.  
  16.  
  17. End Sub

Cheers Jonny
Jan 7 '07 #1
15 1696
pks00
280 Expert 100+
u cannot use RunSQL as this is for action queries, instead use a recordset or a domain function

eg domain function - DCOUNT

DCOUNT("*","tblBooking","userid = username AND jobStatus = open")

example above takes your filter, I assume u modify it accordingly. It returns the number of bookings

the recordset example is like this


Dim username As String
Dim strSQL As String
Dim rs as dao.recordset
Dim lCount as long


strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE userId = username AND jobStatus = open "
set rs=currentdb.openrecordset (strSQL)
lCount = 0
if rs.eof = false then
rs.movelast
lCount = rs.RecordCount
endif
rs.close
set rs=nothing


If lCount <= 15 Then
MsgBox "You have 15 or more open bookings, please close bookings before proceeding"
Jan 7 '07 #2
hariharanmca
1,977 1GB
u cannot use RunSQL as this is for action queries, instead use a recordset or a domain function

eg domain function - DCOUNT

DCOUNT("*","tblBooking","userid = username AND jobStatus = open")

example above takes your filter, I assume u modify it accordingly. It returns the number of bookings

the recordset example is like this


Dim username As String
Dim strSQL As String
Dim rs as dao.recordset
Dim lCount as long


strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE userId = username AND jobStatus = open "
set rs=currentdb.openrecordset (strSQL)
lCount = 0
if rs.eof = false then
rs.movelast
lCount = rs.RecordCount
endif
rs.close
set rs=nothing


If lCount <= 15 Then
MsgBox "You have 15 or more open bookings, please close bookings before proceeding"


i thinh in the above Qry there is no use of count(*)
Jan 7 '07 #3
hariharanmca
1,977 1GB
u cannot use RunSQL as this is for action queries, instead use a recordset or a domain function

eg domain function - DCOUNT

DCOUNT("*","tblBooking","userid = username AND jobStatus = open")

example above takes your filter, I assume u modify it accordingly. It returns the number of bookings

the recordset example is like this


Dim username As String
Dim strSQL As String
Dim rs as dao.recordset
Dim lCount as long


strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE userId = username AND jobStatus = open "
set rs=currentdb.openrecordset (strSQL)
lCount = 0
if rs.eof = false then
rs.movelast
lCount = rs.RecordCount
endif
rs.close
set rs=nothing


If lCount <= 15 Then
MsgBox "You have 15 or more open bookings, please close bookings before proceeding"

it will select only one record. if you give

lCount = rs.RecordCount

then the value will be 1

beter you can use like this
lCount = rs.field("numberOfBookings")
Jan 7 '07 #4
JonnyB
16
Thanks pks00.

I tried the recordset option but got a compile error fo this line

set rs=currentdb.openrecordset (strSQL)

saying "Too few parameters. Expected three"

Any ideas
Jan 7 '07 #5
hariharanmca
1,977 1GB
Thanks pks00.

I tried the recordset option but got a compile error fo this line

set rs=currentdb.openrecordset (strSQL)

saying "Too few parameters. Expected three"

Any ideas
just reply the qry you used....
Jan 7 '07 #6
JonnyB
16
Hi hariharanmca,

No to sure what you were meaning by that i'm guessing you ment us my sql statement not the one done by pks00.

I tried that but i'm still having problems.

I've uploaded the access file if that would help http://www.celestepoker.com/Temp/jr_system.mdb

username = u1
password = 00

Then click create booking.

Thanks.
Jan 7 '07 #7
GRDev
3
I think you need to check query

strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE (((tblBooking.userID)='u1') AND ((tblBooking.bookingStatus)='Active')) "
Jan 7 '07 #8
JonnyB
16
Cheers GRDev,

Thats stopped the errors but its not quite working.

The error message seems to appear even if I have less than 15 records in the booking table.

I'm sure its nearly there, I think its the line in bold i've tried the other methods for this line above but they don't seem to fix it either. Any ideas, my codes:-

Private Sub btnEngCreateBooking_Click()


Dim username As String
username = Nz(Forms!frmLogin!txtMembername, "")

Dim strSQL As String
Dim rs As DAO.Recordset
Dim lCount As Long


strSQL = "SELECT Count(*) AS numberOfBookings FROM tblBooking WHERE (((tblBooking.userID)='username') AND ((tblBooking.bookingStatus)='Active')) "
Set rs = CurrentDb.OpenRecordset(strSQL)
lCount = 0
If rs.EOF = False Then
rs.MoveLast
lCount = rs.RecordCount
End If
rs.Close
Set rs = Nothing


If lCount <= 15 Then
MsgBox "You have 15 or more open bookings, please close bookings before proceeding"

Else

DoCmd.OpenForm "frmEngCreateBooking"

End If


End Sub
Jan 7 '07 #9
pks00
280 Expert 100+
Hi

Right, apologies, I mixed up the usage with the sql regarding recordset

Firstly, I didnt change the sql, I left it as u initially did it

secondly, the dcount option returns the number of records based on your filter, given now that I know your table and how the data should be, u can use DCOUNT

here is the code again which gets the count
Note the use of recordset, I used a column alias (Cnt), then referenced that


Expand|Select|Wrap|Line Numbers
  1.     Dim lCnt As Long
  2.     Dim sUser As String
  3.  
  4.  
  5.     sUser = "u1"
  6.  
  7.  
  8. 'METHOD 1
  9.     lCnt = DCount("*", "tblBooking", "userID = '" & sUser & "' AND bookingStatus = 'Active'")
  10.  
  11.     Debug.Print "METHOD 1 - DCOUNT"
  12.     Debug.Print lCnt
  13.  
  14.  
  15. 'METHOD 2
  16.     Dim rs As DAO.Recordset
  17.     Dim sSql As String
  18.  
  19.     lCnt = 0
  20.     sSql = "SELECT Count(*) AS Cnt FROM tblBooking WHERE userID = '" & sUser & "' AND bookingStatus = 'Active'"
  21.     Set rs = CurrentDb.OpenRecordset(sSql)
  22.     If rs.EOF = False Then
  23.         lCnt = rs!Cnt
  24.     End If
  25.     rs.Close
  26.     Set rs = Nothing
  27.  
  28.     Debug.Print "METHOD 2 - RECORDSET"
  29.     Debug.Print lCnt
  30.  

The value of the count is in lCnt
Jan 7 '07 #10
JonnyB
16
Worked perfectly, thankyou for all your help with this.
Jan 7 '07 #11
NeoPa
32,556 Expert Mod 16PB
Nice work PKS.
Long time no see.
Jan 8 '07 #12
pks00
280 Expert 100+
Howdo NeoPa

How are you
Yea, been busy with other things, cant spend as much time as I would like. Im occasionally popping in now when I get emails from Mary asking for help.
I must try to find more time to spend here!!!
Jan 8 '07 #13
pks00
280 Expert 100+
Worked perfectly, thankyou for all your help with this.
Hi JonnyB, Im glad its worked fine for you, main thing is, do u understand the two solutions provided? DCOUNT is quick and easy to use but u cant do it where u have multiple tables/queries to deal with, hence the 2nd example and use of recordsets.
Jan 8 '07 #14
JonnyB
16
Yeah thanks, I was only querying the one table so got away with using DCount.
Jan 8 '07 #15
NeoPa
32,556 Expert Mod 16PB
Strictly speaking (I should have been a school-marm - picture stern old lady wagging admonishing finger at young child) it is possible to do it on complex queries with multiple tables as long as you use a QueryDef as the record source. SQL code won't work.

Domain Aggregate functions (of which DCount() is just one) are fine for simgle requests, but can add a very heavy overhead if called from within SQL or QueryDefs. In this case you needn't worry though :)
Jan 8 '07 #16

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

Similar topics

2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
16
by: walexand | last post by:
I use the database mysql v.4. My problem is... I have a select like: select * from user where language = "de"; the result are then: id name =================== 1 max
8
by: mgm | last post by:
hello, I have a query that is supposed to return only 1 record, however I recently found that because of an error in the database it can return more than 1. So what I need to do is capture if it...
1
by: mjobrien | last post by:
Thanks for the hint Allen (see below). But I am already doing that count as total records read (5)in the report footer. That count is unduplicated for the record not for the field - student ID as...
5
by: chrisc | last post by:
Hello, Hope this is the right place for this... I am creating a testing database for components as they come off a production line. My reports need to select faults that are found, as well...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
13
by: Regnab | last post by:
The problem I'm having is the rsa.RecordCount always returns a '1' when I know there should be about 1600 records returned. I read somewhere that one should use '.MoveLast' before recordcount but...
0
by: Chia Lee Lee | last post by:
Hello… I have problem when counting the number of records, which is based on the start date and end date. I have tried to use message box to prompt the result, but the result given is...
1
by: RussCRM | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.