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? - Private Sub Condo_Exit(Cancel As Integer)
-
-
Dim ThisStartDate As String
-
Dim ThisEndDate As String
-
Dim Criteria As String
-
Dim Cancel As Boolean
-
-
ThisStartDate = "#" & Format(Me!Arrival, "mm/dd/yyyy") & "#"
-
ThisEndDate = "#" & Format(Me!Departure, "mm/dd/yyyy") & "#"
-
Criteria = "[Condo] = '" & Me!Condo & "' And " & _
-
"[Arrival] < " & ThisEndDate & " And [Departure] > " & ThisStartDate & ""
-
Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
-
If Cancel Then _
-
Call MsgBox("Double Booking", _
-
vbOKOnly)
-
MsgBox "Looks good."
-
-
End Sub
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 : - Private Sub Condo_Exit(Cancel As Integer)
-
Dim ThisStartDate As String, ThisEndDate As String, Criteria As String
-
Dim blnCancel As Boolean
-
Dim datVar As Date
-
-
With Me
-
If .Arrival > .Departure Then
-
datVar = .Arrival
-
.Arrival = .Departure
-
.Departure = datVar
-
End If
-
ThisStartDate = "#" & Format(.Arrival, "mm/dd/yyyy") & "#"
-
ThisEndDate = "#" & Format(.Departure, "mm/dd/yyyy") & "#"
-
Criteria = "[Condo] = '" & .Condo & "' And " _
-
& "[Arrival] < " & ThisEndDate & " And " _
-
& "[Departure] > " & ThisStartDate
-
Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
-
If Cancel Then
-
Call MsgBox("Double Booking", vbOKOnly)
-
Else
-
Call MsgBox ("Looks good.", vbOKOnly)
-
End If
-
End With
-
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 : - If Cancel Then
-
Call MsgBox("Double Booking", vbOKOnly)
-
Else
-
MsgBox "Looks good."
-
End If
-
-
End Sub
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 : - Dim datVar As Date
-
-
With Me
-
If .Arrival > .Departure Then
-
datVar = .Arrival
-
.Arrival = .Departure
-
.Departure = datVar
-
End If
-
End With
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.
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 : - Private Sub Condo_Exit(Cancel As Integer)
-
Dim ThisStartDate As String, ThisEndDate As String, Criteria As String
-
Dim blnCancel As Boolean
-
Dim datVar As Date
-
-
With Me
-
If .Arrival > .Departure Then
-
datVar = .Arrival
-
.Arrival = .Departure
-
.Departure = datVar
-
End If
-
ThisStartDate = "#" & Format(.Arrival, "mm/dd/yyyy") & "#"
-
ThisEndDate = "#" & Format(.Departure, "mm/dd/yyyy") & "#"
-
Criteria = "[Condo] = '" & .Condo & "' And " _
-
& "[Arrival] < " & ThisEndDate & " And " _
-
& "[Departure] > " & ThisStartDate
-
Cancel = Not IsNull(DLookup("[Condo]", "[tblBookings]", Criteria))
-
If Cancel Then
-
Call MsgBox("Double Booking", vbOKOnly)
-
Else
-
Call MsgBox ("Looks good.", vbOKOnly)
-
End If
-
End With
-
End Sub
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!
NeoPa 32,556
Expert Mod 16PB
Always a joy :-)
I'll PM about a chat soon.
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”.
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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:...
|
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
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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...
| |