A
Aino
Hi
I am really not sure to get this done.
Based on input from a form, I process a query, ending up assigning my
desired results to variables inside a loop.
Now I would like to display the results in a report.
At first I thought I could just add textboxes as desired to the report
inside the loop before opening it: Nope.
Then I thougth of using listboxes and adding the results as new lines
to those from the loop: No can do.
Instead I thougth I would create a new recordset, fill it with my
results, and then use this as the recordsouce: Can't be done.
Could someone please point me in the rigth direction?
Here is the code:
===============================
Private Sub btnOK_Click()
Dim dbs As DAO.Database
Dim rstAvailTimes As DAO.Recordset
Dim rstCars As DAO.Recordset
Dim rstAvailCar As DAO.Recordset
Dim strCriteria As String
Dim strSQL As String
Dim strCar As String
Dim strDay As String
Dim datFrom As Date
Dim datTo As Date
Dim datMaxForCar As Date
Dim datMinForCar As Date
Dim datTimeMax As Date
Dim datTimeMin As Date
Dim strTimeMax As String
Dim strTimeMin As String
Dim strAdrMax As String
Dim strAdrMin As String
On Error GoTo Err_btnOK_Click
strDay = cmbDay
datFrom = txtFrom
datTo = txtTo
Set dbs = CurrentDb
strCriteria = "[Day] = '" & strDay & "' AND NOT [Car] IN " _
& "(SELECT [Car] FROM [qryTimes] WHERE [Day] = '" & strDay _
& "' AND [TimeMax] > #" & datFrom & "# AND [TimeMin] < #" &
datTo & "#)"
strSQL = "SELECT * FROM [qryTimes] WHERE " & strCriteria
Set rstAvailTimes = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
If rstAvailTimes.EOF Then
MsgBox "There are no available cars between kl. " & datFrom &
" og kl. " & datTo & " on a " & strDay
GoTo Exit_btnOK_Click
End If
strSQL = "SELECT DISTINCT [Car] FROM [qryTimes] WHERE " &
strCriteria
Set rstCars = dbs.OpenRecordset(strSQL)
rstCars.MoveFirst
Do While Not rstCars.EOF
datTimeMax = #12:00:00 AM#
datTimeMin = #11:59:00 PM#
strTimeMax = ""
strTimeMin = ""
strAdrMax = ""
strAdrMin = ""
strCar = rstCars![Car]
rstAvailTimes.Filter = "[Car]= " & strCar
Set rstAvailCar = rstAvailTimes.OpenRecordset
With rstAvailCar
.MoveFirst
Do While Not .EOF
datMaxForCar = rstAvailCar![TimeMax]
datMinForCar = rstAvailCar![TimeMin]
If datMaxForCar <= datFrom Then
If datMaxForCar > datTimeMax Then
datTimeMax = datMaxForCar
strAdrMax = rstAvailCar![AdrMax]
End If
ElseIf datMinForCar >= datTo Then
If datMinForCar < datTimeMin Then
datTimeMin = datMinForCar
strAdrMin = rstAvailCar![AdrMin]
End If
End If
.MoveNext
Loop
End With 'rstAvailCar
If datTimeMax <> #12:00:00 AM# Then strTimeMax = datTimeMax
If datTimeMin <> #11:59:00 PM# Then strTimeMin = datTimeMin
' Debug.Print strCar & vbTab & strTimeMax & vbtab &
strAdrMax & vbTab & strTimeMin & vbTab & strAdrMin
rstCars.MoveNext
Loop
GoTo Exit_btnOK_Click
Err_btnOK_Click:
MsgBox Err.Description
Exit_btnOK_Click:
If Not rstAvailCar Is Nothing Then
rstAvailCar.Close
Set rstAvailCar = Nothing
End If
If Not rstAvailTimes Is Nothing Then
rstAvailTimes.Close
Set rstAvailTimes = Nothing
End If
If Not rstCars Is Nothing Then
rstCars.Close
Set rstCars = Nothing
End If
If Not dbs Is Nothing Then
dbs.Close
Set dbs = Nothing
End If
End Sub
===================
Best regards
Aino
I am really not sure to get this done.
Based on input from a form, I process a query, ending up assigning my
desired results to variables inside a loop.
Now I would like to display the results in a report.
At first I thought I could just add textboxes as desired to the report
inside the loop before opening it: Nope.
Then I thougth of using listboxes and adding the results as new lines
to those from the loop: No can do.
Instead I thougth I would create a new recordset, fill it with my
results, and then use this as the recordsouce: Can't be done.
Could someone please point me in the rigth direction?
Here is the code:
===============================
Private Sub btnOK_Click()
Dim dbs As DAO.Database
Dim rstAvailTimes As DAO.Recordset
Dim rstCars As DAO.Recordset
Dim rstAvailCar As DAO.Recordset
Dim strCriteria As String
Dim strSQL As String
Dim strCar As String
Dim strDay As String
Dim datFrom As Date
Dim datTo As Date
Dim datMaxForCar As Date
Dim datMinForCar As Date
Dim datTimeMax As Date
Dim datTimeMin As Date
Dim strTimeMax As String
Dim strTimeMin As String
Dim strAdrMax As String
Dim strAdrMin As String
On Error GoTo Err_btnOK_Click
strDay = cmbDay
datFrom = txtFrom
datTo = txtTo
Set dbs = CurrentDb
strCriteria = "[Day] = '" & strDay & "' AND NOT [Car] IN " _
& "(SELECT [Car] FROM [qryTimes] WHERE [Day] = '" & strDay _
& "' AND [TimeMax] > #" & datFrom & "# AND [TimeMin] < #" &
datTo & "#)"
strSQL = "SELECT * FROM [qryTimes] WHERE " & strCriteria
Set rstAvailTimes = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
If rstAvailTimes.EOF Then
MsgBox "There are no available cars between kl. " & datFrom &
" og kl. " & datTo & " on a " & strDay
GoTo Exit_btnOK_Click
End If
strSQL = "SELECT DISTINCT [Car] FROM [qryTimes] WHERE " &
strCriteria
Set rstCars = dbs.OpenRecordset(strSQL)
rstCars.MoveFirst
Do While Not rstCars.EOF
datTimeMax = #12:00:00 AM#
datTimeMin = #11:59:00 PM#
strTimeMax = ""
strTimeMin = ""
strAdrMax = ""
strAdrMin = ""
strCar = rstCars![Car]
rstAvailTimes.Filter = "[Car]= " & strCar
Set rstAvailCar = rstAvailTimes.OpenRecordset
With rstAvailCar
.MoveFirst
Do While Not .EOF
datMaxForCar = rstAvailCar![TimeMax]
datMinForCar = rstAvailCar![TimeMin]
If datMaxForCar <= datFrom Then
If datMaxForCar > datTimeMax Then
datTimeMax = datMaxForCar
strAdrMax = rstAvailCar![AdrMax]
End If
ElseIf datMinForCar >= datTo Then
If datMinForCar < datTimeMin Then
datTimeMin = datMinForCar
strAdrMin = rstAvailCar![AdrMin]
End If
End If
.MoveNext
Loop
End With 'rstAvailCar
If datTimeMax <> #12:00:00 AM# Then strTimeMax = datTimeMax
If datTimeMin <> #11:59:00 PM# Then strTimeMin = datTimeMin
' Debug.Print strCar & vbTab & strTimeMax & vbtab &
strAdrMax & vbTab & strTimeMin & vbTab & strAdrMin
rstCars.MoveNext
Loop
GoTo Exit_btnOK_Click
Err_btnOK_Click:
MsgBox Err.Description
Exit_btnOK_Click:
If Not rstAvailCar Is Nothing Then
rstAvailCar.Close
Set rstAvailCar = Nothing
End If
If Not rstAvailTimes Is Nothing Then
rstAvailTimes.Close
Set rstAvailTimes = Nothing
End If
If Not rstCars Is Nothing Then
rstCars.Close
Set rstCars = Nothing
End If
If Not dbs Is Nothing Then
dbs.Close
Set dbs = Nothing
End If
End Sub
===================
Best regards
Aino