Hi,
I am trying to loop through a number of records in a query and select them based on a condition. Once the records are selected I want to create a record in another query and continue the loop until it is finished.
More simply.
1. Loop through records in query
2. Find record based on condition
3. Create new record in another query
4. Continue Loop
5. (If any more records are found, then continue to add them to the new query)
This is the code so far:
*********************************************
Dim strDate As String
Dim strStartDate As String
Dim strFinishDate As String
Dim i As Date
Dim rs As ADODB.Recordset
Dim db As Database
Dim strSQL As String
Dim qdef As QueryDef
stDocName = "DueCalibration"
strDate = DATE
strStartDate = DateAdd("m", -24, strDate)
strStartDate = Format(strStartDate, "Medium Date"
strFinishDate = DateAdd("m", -23, strDate)
strFinishDate = Format(strFinishDate, "Medium Date"
MsgBox "search range is from " & strStartDate & " to " & strFinishDate & "", vbExclamation
For i = strStartDate To strFinishDate
DoCmd.Hourglass True
Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
stLinkCriteria = "[JobDate]= #" & strStartDate & "#"
strSQL = "SELECT DISTINCT * FROM JobsComplete WHERE " & _
stLinkCriteria
Set db = CurrentDb
'Open the recordset
rs.Open strSQL
If (rs.EOF) Then
strStartDate = DateAdd("d", 1, strStartDate)
strStartDate = Format(strStartDate, "Medium Date"
Else
db.QueryDefs.Delete "TEST"
Set qdef = db.CreateQueryDef("TEST", strSQL)
Set rs = Nothing
strStartDate = Format(strStartDate, "Medium Date"
End If
Next i
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
DoCmd.Hourglass False
Set rs = Nothing
Set db = Nothing
***************************************************
Regards,
- Paul
I am trying to loop through a number of records in a query and select them based on a condition. Once the records are selected I want to create a record in another query and continue the loop until it is finished.
More simply.
1. Loop through records in query
2. Find record based on condition
3. Create new record in another query
4. Continue Loop
5. (If any more records are found, then continue to add them to the new query)
This is the code so far:
*********************************************
Dim strDate As String
Dim strStartDate As String
Dim strFinishDate As String
Dim i As Date
Dim rs As ADODB.Recordset
Dim db As Database
Dim strSQL As String
Dim qdef As QueryDef
stDocName = "DueCalibration"
strDate = DATE
strStartDate = DateAdd("m", -24, strDate)
strStartDate = Format(strStartDate, "Medium Date"
strFinishDate = DateAdd("m", -23, strDate)
strFinishDate = Format(strFinishDate, "Medium Date"
MsgBox "search range is from " & strStartDate & " to " & strFinishDate & "", vbExclamation
For i = strStartDate To strFinishDate
DoCmd.Hourglass True
Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
stLinkCriteria = "[JobDate]= #" & strStartDate & "#"
strSQL = "SELECT DISTINCT * FROM JobsComplete WHERE " & _
stLinkCriteria
Set db = CurrentDb
'Open the recordset
rs.Open strSQL
If (rs.EOF) Then
strStartDate = DateAdd("d", 1, strStartDate)
strStartDate = Format(strStartDate, "Medium Date"
Else
db.QueryDefs.Delete "TEST"
Set qdef = db.CreateQueryDef("TEST", strSQL)
Set rs = Nothing
strStartDate = Format(strStartDate, "Medium Date"
End If
Next i
DoCmd.OpenReport stDocName, acViewPreview, , stLinkCriteria
DoCmd.Hourglass False
Set rs = Nothing
Set db = Nothing
***************************************************
Regards,
- Paul