473,471 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using SQL statement in VBA to do a count then use IF statement on result!!

4 New Member
Hi Guys,
Im trying to get this VBA code to work so if the SQL returns a count of 1 or more then the msgbox will appear and the user cannot add the date and time. This is to stop conflicts of booking 2 interviews on the same date and time:

Expand|Select|Wrap|Line Numbers
  1. Public Sub Interview_Time_AfterUpdate()
  2. Dim strSQL As String
  3. strSQL = "SELECT Count(*) from tblIntCandInterview WHERE [Interview Date] = '" & Me.Interview_Date & "' and [Interview Time] = '" & Me.Interview_Time & "'" 
  4.  
  5. If strSQL > 0 Then 
  6. MsgBox "Interview Date & Time Conflicts With Another, Please Change Date or Time!" 
  7. Else 
  8. MsgBox "Date & Time of Interview OK"
  9. End Sub
Please help me, what am I doing wrong? Its not erroring when I enter a duplicate date and time??"

Thanks, Charlie
May 6 '10 #1

✓ answered by Jim Doherty

@CharlieUK
Hi Charlie

Look at your code again...

1) You are only defining the variable strSQL as a string and then setting a string value to that variable and not opening any recordset based on that SQL in order to do any counting test.

2) The strSQL>0 makes no sense in this context (ie if you believe you are testing a recordset at this point then you are mistaken)
.
3) The use of concatenation when referring to dates should use # not the apostrophe when wrapping a date field

4) When creating table fields name them without spaces.

Have a look at the DCount function in Access it is documented to return a count of the records that satisfy any of your criteria

If you wish to proceed in the context of opening a recordset and counting on that, you need to look at the OpenRecordSet method. There are plenty of references to it on site........ seek and you shall find :)

If you get really stuck then come back to me. I would rather you fully understood each piece in sequence rather than just the solution straight away

3 2060
Jim Doherty
897 Recognized Expert Contributor
@CharlieUK
Hi Charlie

Look at your code again...

1) You are only defining the variable strSQL as a string and then setting a string value to that variable and not opening any recordset based on that SQL in order to do any counting test.

2) The strSQL>0 makes no sense in this context (ie if you believe you are testing a recordset at this point then you are mistaken)
.
3) The use of concatenation when referring to dates should use # not the apostrophe when wrapping a date field

4) When creating table fields name them without spaces.

Have a look at the DCount function in Access it is documented to return a count of the records that satisfy any of your criteria

If you wish to proceed in the context of opening a recordset and counting on that, you need to look at the OpenRecordSet method. There are plenty of references to it on site........ seek and you shall find :)

If you get really stuck then come back to me. I would rather you fully understood each piece in sequence rather than just the solution straight away
May 6 '10 #2
CharlieUK
4 New Member
@Jim Doherty
thanks Jim, ive sorted it with DCount method using a query!

great answer and thanks again :-)
May 6 '10 #3
Jim Doherty
897 Recognized Expert Contributor
@CharlieUK
You,re welcome glad you got it sorted :-)
May 6 '10 #4

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

Similar topics

2
by: bsder | last post by:
Hi, If I want to select a result that based on the value of count (eg. count(*) > 5), how can I write a sql to do that? eg. select count(*)>5 from Flight where OperationType = "Departure"...
4
by: Jimmy V | last post by:
Hi all, I am a VB programmer who is moving out of the shadows and starting to code in C#. I would like to know how to determine a control's type using a swtich statement. In VB i would do...
2
by: dhakate123 | last post by:
Hi Friends.. I want delete repeated entries which comes twice in a table. How to delete that extra entry and keep each single entry using T-SQL statement(SQL server 2000). Please give me the...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
1
by: sandeep.damodar | last post by:
Hello, I need help.. I am just trying to insert data from the datagrid to data source, using insert sql statement.
10
by: Nkhosinathie | last post by:
hello everyone may someone please help me with this c++ program. i'm developing a program that will print numbers from 0 to 10 which must appear like a table but i;m only allowed to use if...
1
by: =?Utf-8?B?bGlhbnF0bGl0?= | last post by:
Is using a jump statement more faster than using if statement with a jump statement example for(int i=0; i < 10; i++) { if(a == null) {
1
vikysaran
by: vikysaran | last post by:
Hi, i want to delete rows from two tables using one sql statement. One table is tblEmployee and other is tblUser and i want to delete all record in both of table whose UserID is EE00001
4
by: whatsuppussycat | last post by:
I want to be able to SUM the values returned using the TOP 5 and COUNt statements. So far I've tried from many angles and can't get it to work, very frustrating! Here is my code: SELECT TOP 5...
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...
1
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.