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

Complex booking form using VBA code

Hi,

I have a form in Access 2000 running Windows XP with the following fields: -

Booking_ID
Date
Person_Name
Booking_Type
8-9am (y/n checkbox)
9-10am (y/n checkbox)
10-11am (y/n checkbox)
11-12am (y/n checkbox)

I'm trying to write some VBA code which for any given date only allows a max of 3 bookings each for the two timeslots 8-9am and 9-10am, and a max of 5 bookings each for the timeslots 10-11am and 11-12am, but only when the Booking_Type is set as 'holiday', if the Booking_Type is anything else then there can be unlimited bookings for each of the individual timeslots.

Lets say that on 15/10/08 there have been 3 holiday bookings for the time slot 8-9pm and only 1 holiday booking for 9-10am and someone tries to make a booking for 2 holiday slots (8-9am & 9-10am), I would want a message appearing which states that the 8-9am slot was fully booked up (as already 3 bookings had been made for that time slot on that date).

Basically I'm creating a holiday booking database where there is a holiday threshold where only 3 holidays can be booked from 8-10am and only 5 bookings can be made from 10-12am each day.

Any help would be very much appreciated,

Regards,

Dean
Feb 29 '08 #1
3 3634
sierra7
446 Expert 256MB
Hi Dean

First, don't use fieldname "Date" in your table as this is a reserved word. Use Bdate (for Booking Date) or something instead.

You will need another table to hold one record for each 'slot' booked. Before adding an extra record to book a new 'slot' on a particular day , you will need to count how many already exist and evaluate whether another is allowable.

I suggest you define SlotType as = 1 for 8-9am and SlotType = 2 for 9-10am.

The structure of the new table table: Name = tblSlots
Expand|Select|Wrap|Line Numbers
  1.  
  2. SlotID            Autonumber     'Unique, for internal indexing
  3. SlotType Integer             'Holds 1 or 2
  4. Sdate         Date                 'Date of time slot
  5. Booking_ID    Long Integer     'needed for deleting!
  6.  
On the Before_Update event of the checkbox for [8-9am] you need some code like;
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Ctl8_9am_AfterUpdate()
  3. 'first check a date has been entered
  4. If IsNull(Me.Bdate) Then
  5.     MsgBox "You must enter a date before booking the time", vbCritical, "No Date!"
  6.     Me.Ctl8_9am = False
  7.     Exit Sub
  8. End If
  9.  
  10. 'Application.SetOption "Confirm Action Queries", False
  11. If Me.Booking_Type Like "H*" Then 'only check if Holiday
  12.     If Me.Ctl8_9am = True Then
  13.         'Check current situation
  14.         If DCount("[SlotID]", "tblSlots", "[SlotType]=1 and [Sdate]=#" & Format(Me.Bdate, "mm-dd-yyyy") & "#") >= 3 Then
  15.             MsgBox "There are already 3 Holidays booked for this time on this date", vbCritical, "Fully Booked"
  16.             Me.Ctl8_9am = False
  17.             Exit Sub
  18.         Else
  19.             'Confirm booking by adding new record
  20.             DoCmd.RunSQL "INSERT INTO tblSlots ( SlotType, Sdate, Booking_ID )" & _
  21.                 " Values( 1,#" & Format(Me.Bdate, "mm-dd-yyyy") & "#, " & Me.Booking_ID & ");"
  22.         End If
  23.  
  24.     Else
  25.         'Removing a booking
  26.         DoCmd.RunSQL "DELETE * FROM tblSlots WHERE Booking_ID =" & Me.Booking_ID & " and [SlotType] =1 ;"
  27.     End If
  28. End If
  29. End Sub
  30.  
I have left warnings on while you are testing but you can set them off once you have it all working.

I don't know if I have captured exactly what you want but it must be something along these lines. You will have to repeat this code for the 9-10am slot, not forgetting to change the Count and SlotType.

S7
Feb 29 '08 #2
Hi S7,

Thanks for your quick response, the majority of it makes sense and it looks as though it covers everything needed. However when I click on the checkbox which contains the code is brings back the error message: -

MS Access can't append all the records in the append query....and it didn't add 1 record to the table due to key violations

The code it is getting stuck on is as follows: -

Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "INSERT INTO tblSlots ( SlotType, Sdate, Booking_ID )" & _
  2.                 " Values( 1,#" & Format(Me.Bdate, "mm-dd-yyyy") & "#, " & Me.Booking_ID & ");" 
Just to confirm, the main tables being used look like this: -

tblBooking
-Booking ID
-Bdate
-PersonID (links into an Person Table containing info on the Person)
-Booking Type (i.e. holiday, sick, AWOL etc)

tblSlots
-SlotID
-SlotType
-SDate
-BookingID

And the form in question contains all data in the tblBooking table along with checkboxes for the possible slot times i.e. 8-9am etc.

Quick question: What is the use of SDate in tbleSlots when there is a BDate in tblBooking, I would have thought that having two dates is duplicating data?

Thanks,

Dean
Feb 29 '08 #3
sierra7
446 Expert 256MB
Hi Dean

Last things first!; You need a date in tblSlots so that you know how may Type 1 there are for a given date. I called it [Sdate] whereas it's copied from [Bdate] so could be called [Bdate]. It's just a habit; I try and name all my dates differently so I know where they come from and I don't get mixed up (on a biggger project).

Just reading your post you have three different spellings for BookingID, Booking ID and Booking_ID. Choose one and stick with it!

As a general rule I abhor spaces in field names; it stops this sort of problem. Also, I didn't want to winge at the time but I don't like fieldnames starting with Numbers. e.g [8-9am] Out of interest I continued to follow your schema and to see if Access would allow it and I note that when the form is built Access prefixes these contols with 'Ctl' e.g [Ctl8-9am]

This does not necessarily account for your error message which implies that you have an index on one of the fields and are trying to add a duplicate value. This may be, or it could just be that Access 'has got it's knickers in a twist' because of an erroneous field name.

My test-bed seems to works althought I have not tried to break it.
I tried to use the Before_Update event for the test but got too many error messages about cancelling and the After_Update did not seem to mind the check box being re-set if the numbers where exceeded.

S7
Attached Files
File Type: zip TimeSlots.zip (16.7 KB, 206 views)
Feb 29 '08 #4

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

Similar topics

8
by: Dave Robinson | last post by:
I was wondering if anyone could help me with a problem I'm having. I've been using Dreamweaver to create a hotel booking system for a friend of mine, using MySQL (version 4.0.21) and PHP 5. The...
4
by: WiseOwl | last post by:
Hi folks I teach. At school, four IT rooms are booked using a paper based outline timetable. Completing it is easy but basic and impossible to ensure completion of all fields (name, year...
2
by: Andy | last post by:
Hi folks I teach. At school, four IT rooms are booked using a paper based outline timetable. Completing it is easy but basic and impossible to ensure completion of all fields (name, year...
0
by: thegame21 | last post by:
Hi, I am currently creating a cinema system using access where a booking can be made for a event. Each event when it is shown is categoriesd as a performance. A booking must be made for each...
1
by: simba | last post by:
Hello, I am currently doing a project which requires me to develop an online booking system for hotels and integrate both bed and room booking. I have the room booking working but I cant seem to...
2
by: ewan89 | 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...
25
by: NDayave | last post by:
How do, I have a access 2000 booking database with Personal Details, Outing Details and the Bookings in three tables: tblBookings -- -Autonumber, -Number, -Number, -Currency, -Yes/No, -Yes/No ...
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...
7
by: phill86 | last post by:
Hi, I have an equipment booking form that a user is able to book a piece of equipment on a selected date and time. I have written code to prevent the user being able to book the piece of equipment...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...

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.