While I'd be inclined to do all this behind a form module, collect dynamic criteria, generate the recordset and then open the report, the ADO is the same. Here is how it might look in your report module:
Private Sub Report_Open(Cancel As Integer)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim strCriteria As String
Set cnn = CurrentProject.Connection
strSQL = "SELECT * FROM qryTest"
Set rst = New ADODB.Recordset
rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
'DELETE******************************************* *******
strCriteria = "Test2"
' Filter records to be deleted.
rst.Filter = "TestText = '" & strCriteria & "'"
' Delete the records
If rst.RecordCount > 1 Then
Do While Not rst.EOF
rst.Delete
rst.MoveNext
Loop
rst.UpdateBatch
End If
rst.Filter = adFilterNone
rst.Requery
'UPDATE******************************************* ******
strCriteria = "Test3"
' Updaet records that meet criteria.
If rst.RecordCount > 1 Then
Do While Not rst.EOF
If rst!TestText = strCriteria Then
With rst
!TestText = "Test4"
.MoveNext
End With
Else
rst.MoveNext
End If
Loop
rst.UpdateBatch
End If
' Set reports recordsource.
Reports!rptTest.RecordSource = "qryTest"
' clean up
rst.Close
Set rst = Nothing
Exit Sub
ErrorHandler:
' clean up
If Not rst Is Nothing Then
If rst.State = adStateOpen Then rst.Close
End If
Set rst = Nothing
If Err <> 0 Then
MsgBox Err.Source & "-->" & Err.Description, , "Error"
End If
End Sub
HTH,
Bob
|