I take it that you book machines in 1-hour blocks, so you need to create 24
x 1-hour blocks, for each machine, for each date, omitting the dates listed
on a table of holidays.
The solution below uses a table of hours, a table of dates, and your
existing table of machines. Combining them in a query without joins (a
Cartesian Product), gives you every possible combination. You then execute
this as an Append query to populate your table with all the desired records.
(I assume the target table has fields for MachineID and the date/time.) We
will use a lower-level query to skip the holidays.
Steps:
1. Build a table of hours.
Create a table with one field named CountID, Number type, Long Integer size.
Mark the field as primary key. Save the table as tblCount. Enter 24
records - values from 0 to 23. Close.
2. Build a table of dates.
Create another table, with a field named TheDate, Date/Time type, primary
key. Save the table as tblDate. Populate the table with all dates for the
range you wish to use.
3. Build the query to skip the holidays.
Create a query using tblDate and your table of holidays, joined on the date
fields. Double-click the line joining the 2 tables. Access pops up a dialog
offering 3 options. Choose:
All records from tblDate, and any matches from Holidays.
Drag TheDate into the grid form tblDate.
Drag the holiday date into the grid, and uncheck the Show box for this
field.
In the Criteria row under this field, enter:
Is Null
Save the query as qryWorkDays.
4. Build the query to generate all the records.
Create a query that uses these source "tables":
- your table of machines
- qryWorkDays
- tblCount.
There must be no line joining the tables in the upper pane of query design.
Drag the MachineID field from the Machine table into the grid.
In the next column of the field row, enter:
DateAdd("h", tblCount.CountID, qryWorkDays.TheDate)
Test the output: you should see a record for each hour of each work day for
each machine.
5. Execute the query.
Change the query to an Append query (Append on Query menu.)
Tell Access the name of the table to append to.
Run the query (Run on Query menu.)
You now have all the records in your target table.
If you have a large range of dates at step 2, you can use the code below to
programmatically populate the table of dates:
Function MakeDates(dtStart As Date, dtEnd As Date) As Long
Dim dt As Date
Dim rs As DAO.Recordset
Set rs = DBEngine(0)(0).OpenRecordset("tblDate")
With rs
For dt = dtStart To dtEnd
.AddNew
!TheDate = dt
.Update
Next
End With
rs.Close
Set rs = Nothing
End Function
My question is probably elementary but I need help. I have numerous
machines
[quoted text clipped - 10 lines]
autofill
on the days in the holiday column.