S
swarfmaker
I'm developing a database (after a 3 year layoff, so I'm a bit rusty) to
book training resources out. I've got 3 tables, tblBooking, tblResources,
tblResourceType and have built the necessary form. If I only have the one
resource I have code that will check if the resource is free between the
requested start and end dates. This code was from Allen Browne many years
ago. I am stumped as to how I can modify this code to cope with multiple
resources of different types, e.g. I may have 3 Digital Projectors, 5
laptops, 2 cameras, etc.
As I said, I'm a bit rusty, its amazing how much you forget when you no
longer do this stuff every day.
The code I need to modify is below.
TIA
Iain, in Pudsey, UK
********Code Start***********
Private Sub EndDate_LostFocus()
'dimension variables used
Dim sdate As Date, edate As Date
Dim MySQL As String, X As Date
Dim db As Database, rst As DAO.Recordset
Dim ctlText As Control
'Me!ctlStartDate and Me!ctlEndDate are the fields from your FORM
sdate = Me![StartDate]
edate = Me![EndDate]
'this creates the query, tblBookings is your TABLE name, id is the idnumber
'of the field your searching for
'Me!cboItem is the field you choose the item from on your FORM, this assumes
'that your bookings table
'has an item name and an id for that item, also that the combo box shows the
'item name but is bound
'to the item id field.....let me know if that confused you
MySQL = "SELECT * FROM tblBooking WHERE [Cancelled]Is Null "
Set db = CurrentDb()
'open recordset based on query
Set rst = db.OpenRecordset(MySQL, dbOpenDynaset)
'this code loop will walk through the recordset for each time the item is
booked
With rst
rst.MoveFirst
Do While Not rst.EOF
'!startdate and !enddate are the field names from your TABLE
' variable x stores the current date being compared
For X = ![StartDate] To ![EndDate]
If X >= sdate And X <= edate Then
'item is booked so do something, in this case display message box and
exit
'note: put msgbox statement all on one line
MsgBox "Date Conflict, Booking for dates " & sdate & " to " & edate
& " conflicts with previous booking scheduled between " & !StartDate & " and
" & !EndDate
Me.Undo 'Cancels entry
Set ctlText = Forms![frmBookings]!StartDate 'Returns focus to Start Date
ctlText.SetFocus
Exit Do
End If
Next X
.MoveNext
Loop
End With
End Sub
*********Code End************
book training resources out. I've got 3 tables, tblBooking, tblResources,
tblResourceType and have built the necessary form. If I only have the one
resource I have code that will check if the resource is free between the
requested start and end dates. This code was from Allen Browne many years
ago. I am stumped as to how I can modify this code to cope with multiple
resources of different types, e.g. I may have 3 Digital Projectors, 5
laptops, 2 cameras, etc.
As I said, I'm a bit rusty, its amazing how much you forget when you no
longer do this stuff every day.
The code I need to modify is below.
TIA
Iain, in Pudsey, UK
********Code Start***********
Private Sub EndDate_LostFocus()
'dimension variables used
Dim sdate As Date, edate As Date
Dim MySQL As String, X As Date
Dim db As Database, rst As DAO.Recordset
Dim ctlText As Control
'Me!ctlStartDate and Me!ctlEndDate are the fields from your FORM
sdate = Me![StartDate]
edate = Me![EndDate]
'this creates the query, tblBookings is your TABLE name, id is the idnumber
'of the field your searching for
'Me!cboItem is the field you choose the item from on your FORM, this assumes
'that your bookings table
'has an item name and an id for that item, also that the combo box shows the
'item name but is bound
'to the item id field.....let me know if that confused you
MySQL = "SELECT * FROM tblBooking WHERE [Cancelled]Is Null "
Set db = CurrentDb()
'open recordset based on query
Set rst = db.OpenRecordset(MySQL, dbOpenDynaset)
'this code loop will walk through the recordset for each time the item is
booked
With rst
rst.MoveFirst
Do While Not rst.EOF
'!startdate and !enddate are the field names from your TABLE
' variable x stores the current date being compared
For X = ![StartDate] To ![EndDate]
If X >= sdate And X <= edate Then
'item is booked so do something, in this case display message box and
exit
'note: put msgbox statement all on one line
MsgBox "Date Conflict, Booking for dates " & sdate & " to " & edate
& " conflicts with previous booking scheduled between " & !StartDate & " and
" & !EndDate
Me.Undo 'Cancels entry
Set ctlText = Forms![frmBookings]!StartDate 'Returns focus to Start Date
ctlText.SetFocus
Exit Do
End If
Next X
.MoveNext
Loop
End With
End Sub
*********Code End************