There is no need for a recordset here. What you pass to the report as a
Where condition is just that, an SQL Where clause without the word Where.
Note that the report's record source must have fields in it that match the
field names used in the Where condition.
I Changed the initiation of the EndWeek date to it is Sunday to Saturday.
This seems more logical. If Sunday to Sunday was correct, change it back.
I also added checking for a valid Sunday date and allow a way for the user
to cancel.
Note the variable name changes. It is always best to use naming
conventions. It makes it obvious what data type you are working with and
avoids naming conflicts with reserved words.
Public Function Occupancy()
Dim dtmStartWeek As Date
Dim dtmEndWeek As Date
Dim strWhere As String
Do While True
dtmStartWeek = InputBox("Select and enter a Sunday Date")
If Len(Trim(NZ(dtmStartWeek,""))) = 0 Then
If MsgBox("Cancel Report", vbQuestion + vbYesNo) = vbNo Then
Exit Sub
End If
ElseIf Not IsDate(dtmStartWeek) Then
MsgBox dtmStartWeek & " Is No A Valid Date"
ElseIf WeekDay(dtmStartWeek) <> 7 Then
MsgBox dtmStartWeek & " Is No A Sunday"
Else
Exit Do
End If
Loop
dtmEndWeek = DateAdd(6, "d", dtmStartWeek)
strWhere = "[arrDate] Between #" & dtmStartWeek & "# And #" & _
dtmEndWeek & "# OR [DepartDate] Between "# & dtmStartWeek & _
"# And #" & dtmEndWeek & "# ORDER BY [CabinNum]"
DoCmd.OpenReport "rptCabinOccupancy", acViewPreview, , strWhere
End Function
George R said:
Perhaps I could get some help to revise the following code:
Public Function Occupancy()
Dim db As database, rst As DAO.Recordset
Dim StartWeek As Date
Dim EndWeek As Date
StartWeek = CDate(InputBox("Select and enter a Sunday Date"))
EndWeek = StartWeek + 7
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * FROM tblReservations " & _
"WHERE (ArrDate < StartWeek AND DepartDate > EndWeek) " & _
"OR (ArrDate Between StartWeek And EndWeek) " & _
"OR (DepartDate Between StartWeek And EndWeek) " & _
"ORDER BY CabinNum")
Dim MyRpt As String
MyRpt = "rptCabinOccupancy"
DoCmd.OpenReport MyRpt, acViewPreview, , rst
End Function
THis code stops at the first line. Thank you for your consideration.
Klatuu said:
You can't directly reference a table like that. You will have to use SQL, a
Domain Aggragate function, or a recordset to establish a reference to the
table.
MyDepartDate = CDate(Tables!tblReservations.DepartDate)
Is not enough information in any case. I am sure there are more than one
record in your reservations table. Which one to you want? This could be
done with a Domain Aggragate function if you have a way to match to some
unique value in the the table. For demonstration purposes, I will pretend
you have a reservation number on your form:
MyDepartDate = CDate(DLookup("[DepartDate]", "tblReservations",
[ReservationID] = " & Me.txtReservationID))
First, the syntax above assumes ReservationID is a numeric field.
Do you really need the CDate function? It is not already stored as a date
in the table?
Line 4 needs to be fixed, also.
George R said:
I would appreciate any help on the followng:
The third line of this segment of code give me an error message "Object
Required." How can modify this to get the proper results?
Dim MyDepartDate As Date
Dim MyArrDate As Date
MyDepartDate = CDate(Tables!tblReservations.DepartDate)
MyArrDate = CDate(tblReservations.ArrDate)
Thank you for your consideration.