473,394 Members | 1,843 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,394 software developers and data experts.

Double Booking Code needs a little extra jazz

269 256MB
Hi everybody!
I snagged a piece of code from Bytes (written by the one and only NeoPa to whom my Access success is somewhat owed!). The code works great but I desire it to do a few more things that I am not able to put together. I did TRY and you experts may laugh at my message box sequence. I laugh at myself too, and then return to Bytes. LOL!

Below, then the code runs and it IS a double booking, i get a message box that says "Double Booking" and THEN I get another message box that says "Looks good." Well if it is a double booking, I don't want the box telling me it "looks good." All that really tells me is I don't know how to code very well. LOL. I added the second message box as an indicator to me that the code did fire, because usually it's not a double booking, so nothing would happen when I ran the code. So "Looks Good" let's me know the code ran its check. Need help to exit the code after the "double booking" message box when that's true.

Two other things I would really like:
ONE
I want it to check that the departure date > arrival date. Typos sometimes get me here and that check would help, especially during year changes when I type the wrong year sometimes.

TWO
Right now this code fires when I click a button. But I would really like it to run when I exit the field (by tab key, or by mouse click somewhere else). I tried to put the [Event Procedure] On Exit but it gives me an error in the code here: Dim Cancel As Boolean and won't fire. But it works fine on the button click....however, sometimes I could forget to click the button. Trying to eliminate more of my errors!

Here is the wonderful code. Can anyone help me repair either of these two things above?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Condo_Exit(Cancel As Integer)
  2.  
  3. Dim ThisStartDate   As String
  4. Dim ThisEndDate     As String
  5. Dim Criteria        As String
  6. Dim Cancel          As Boolean
  7.  
  8. ThisStartDate = "#" & Format(Me!Arrival, "mm/dd/yyyy") & "#"
  9. ThisEndDate = "#" & Format(Me!Departure, "mm/dd/yyyy") & "#"
  10. Criteria = "[Condo] = '" & Me!Condo & "' And " & _
  11.     "[Arrival] < " & ThisEndDate & " And [Departure] > " & ThisStartDate & ""
  12. Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
  13. If Cancel Then _
  14. Call MsgBox("Double Booking", _
  15. vbOKOnly)
  16. MsgBox "Looks good."
  17.  
  18. End Sub
Nov 18 '22 #1

✓ answered by NeoPa

Just in case all those changes become a little complicated to manage I've copied below a version that should work past all your problems :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Condo_Exit(Cancel As Integer)
  2.     Dim ThisStartDate As String, ThisEndDate As String, Criteria As String
  3.     Dim blnCancel As Boolean
  4.     Dim datVar As Date
  5.  
  6.     With Me
  7.         If .Arrival > .Departure Then
  8.             datVar = .Arrival
  9.             .Arrival = .Departure
  10.             .Departure = datVar
  11.         End If
  12.         ThisStartDate = "#" & Format(.Arrival, "mm/dd/yyyy") & "#"
  13.         ThisEndDate = "#" & Format(.Departure, "mm/dd/yyyy") & "#"
  14.         Criteria = "[Condo] = '" & .Condo & "' And " _
  15.                  & "[Arrival] < " & ThisEndDate & " And " _
  16.                  & "[Departure] > " & ThisStartDate
  17.         Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
  18.         If Cancel Then
  19.             Call MsgBox("Double Booking", vbOKOnly)
  20.         Else
  21.             Call MsgBox ("Looks good.", vbOKOnly)
  22.         End If
  23.     End With
  24. End Sub

8 9870
NeoPa
32,556 Expert Mod 16PB
Hi Danica Dearest.

To start with the easiest bit replace your lines #13 onwards with :
Expand|Select|Wrap|Line Numbers
  1. If Cancel Then
  2.     Call MsgBox("Double Booking", vbOKOnly)
  3. Else
  4.     MsgBox "Looks good."
  5. End If
  6.  
  7. End Sub
Nov 20 '22 #2
NeoPa
32,556 Expert Mod 16PB
For your problem 'ONE'.
Add an extra (Date) variable after line #6 and then check that the [Arrival] date is earlier than the [Departure] one. If it isn't then switch them.
This code should replace your blank line #7 :
Expand|Select|Wrap|Line Numbers
  1. Dim datVar As Date
  2.  
  3. With Me
  4.     If .Arrival > .Departure Then
  5.         datVar = .Arrival
  6.         .Arrival = .Departure
  7.         .Departure = datVar
  8.     End If
  9. End With
Nov 20 '22 #3
NeoPa
32,556 Expert Mod 16PB
For your problem 'TWO'.
Here you have tried to use the same (reserved) name (Cancel) for two different purposes within the same procedure. First in the procedure heading as an Integer, and then later, on line #6, as a Boolean. As your error message tried so hard to explain - that just can't work.

The predefined version of Cancel in the procedure header is not optional so you'll need to change the name of the other one so they don't work against each other.

You say this code is originally from me anyway, which I find curious as I always use 3-letter prefixes for names which ensures I never fall over such isues in the first place. However we managed to get here though, I suggest changing lines #6, #12 & #13 to use a variable called blnCancel instead of Cancel and I'm sure all will work normally.
Nov 20 '22 #4
NeoPa
32,556 Expert Mod 16PB
Just in case all those changes become a little complicated to manage I've copied below a version that should work past all your problems :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Condo_Exit(Cancel As Integer)
  2.     Dim ThisStartDate As String, ThisEndDate As String, Criteria As String
  3.     Dim blnCancel As Boolean
  4.     Dim datVar As Date
  5.  
  6.     With Me
  7.         If .Arrival > .Departure Then
  8.             datVar = .Arrival
  9.             .Arrival = .Departure
  10.             .Departure = datVar
  11.         End If
  12.         ThisStartDate = "#" & Format(.Arrival, "mm/dd/yyyy") & "#"
  13.         ThisEndDate = "#" & Format(.Departure, "mm/dd/yyyy") & "#"
  14.         Criteria = "[Condo] = '" & .Condo & "' And " _
  15.                  & "[Arrival] < " & ThisEndDate & " And " _
  16.                  & "[Departure] > " & ThisStartDate
  17.         Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
  18.         If Cancel Then
  19.             Call MsgBox("Double Booking", vbOKOnly)
  20.         Else
  21.             Call MsgBox ("Looks good.", vbOKOnly)
  22.         End If
  23.     End With
  24. End Sub
Nov 20 '22 #5
DanicaDear
269 256MB
This works beautifully NeoPa. Thank you for the explanations, which I have studied. I added an extra message box on the arrival>departure and it works beautifully also.

Sometimes the answers are so simple. Sometimes they are more complex. Either way, I super grateful for the help. Thank you so much!
Nov 21 '22 #6
NeoPa
32,556 Expert Mod 16PB
Always a joy :-)

I'll PM about a chat soon.
Nov 21 '22 #7
tumnahi232
1 Bit
This actually isn’t too hard. I built one with Dialogflow to screen any recruiters reaching out to me on LinkedIn or via email. All it takes is a database of commonly asked questions and your responses to those questions. We’re not far off from “have your bot talk to my bot to see if we’re a good fit”.
Nov 22 '22 #8
NeoPa
32,556 Expert Mod 16PB
So, without offering any actual help whatever, you wanted to post to make everyone realise how much more clever you are than those needing to ask for help? That's a bit "WOW!!!" don't you think?
Nov 22 '22 #9

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

Similar topics

4
by: Grant | last post by:
Hi I have a database which logs the usage of rooms. Some booking are entered well in advance, and some have stays of more than six months. I would like to ensure that rooms which have been...
3
by: Just Me | last post by:
In code I check to see if the Floppy drive is ready and display it's state. But if the user inserts or removes a floppy disk I need an event or a Wndproc message to cause the code to display the...
50
by: johnblack | last post by:
I am creating a tennis court booking system. I am trying to create a macro that will not allow the same court to be booked on the same date at the same time. I have the following tables and...
3
by: anotherjoe | last post by:
Question 1: I have reservation database in access 2007 I have a cabin information table with: cabinid (primarykey) cabinname(text) cabinlocation(text) I have a customer table with:...
3
by: Keyboyx | last post by:
I am creating a Dentist Booking form, for my A level project. I have the following Tables: Appointments Appointment ID Dentist ID Patient ID Appointment Date Appointment Time
3
by: Wayne | last post by:
I'm building a bookings database for trucks that among other things captures the TruckName, LoadDate, LoadTime, UnloadDate and UnloadTime. Is there a simple way to prevent double bookings for any...
1
by: Jordan Boden | last post by:
Hi, I have a problem trying to prevent a time slot and seat number being double booked. I am using PHP and MySQL for this. Currently I have no errors concerning variable names or issues with...
1
by: Lucylu | last post by:
Hi, I am creating a website for a project where you are able to book driving lessons. I have created code which (sort of) prevents double booking, however, I'm having a problem. If there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.