Hi all,
I have a function in order to amend some data in a table.
Basically I have a list of names and a state column with T or F in it. I have sorted by name and the last state for each name should be F. The function groups the names first in a recordset, and then if the state for the record above is the same (as in 2 consecutive T's) it should delete it. Otherwise, it should exit the function.
Here is the function so far
I have tried a number of different things however none seem to be working. A few different sites have similar problems and enclosing the WHERE statement in ' ' helps however it was already done.
Any ideas anyone?
Thanks in advance.
Gareth
I have a function in order to amend some data in a table.
Basically I have a list of names and a state column with T or F in it. I have sorted by name and the last state for each name should be F. The function groups the names first in a recordset, and then if the state for the record above is the same (as in 2 consecutive T's) it should delete it. Otherwise, it should exit the function.
Here is the function so far
Code:
Public Function UpdateTable()
On Error GoTo EH:
Dim db As Database
Dim rstNames As Recordset
Dim rstCallOuts As Recordset
Dim strSQL As String
Dim dtmFirstCallout As Date
Set db = CurrentDb()
strSQL = "SELECT name1 " & _
"FROM table1 " & _
"GROUP BY name1 " & _
"HAVING name1 <> 'N/A';"
Set rstNames = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rstNames.RecordCount = 0 Then
rstNames.MoveFirst
Do While Not rstNames.EOF
strSQL = "SELECT ID, name1, state1, time1 " & _
"FROM table1 " & _
"WHERE name1 = '" & rstNames!Name1 & "' " & _
"ORDER BY TimeStamp;"
Set rstCallOuts = db.OpenRecordset(strSQL, dbOpenDynaset)
If Not rstCallOuts.RecordCount = 0 Then
rstCallOuts.MoveFirst
dtmFirstCallout = rstCallOuts!TimeStamp
rstCallOuts.MoveNext
Do While Not rstCallOuts.EOF
If (DLookup("[state1]", "table2", "[ID]=" & "[ID]" - 1) <> "[state1]") Then
Exit Function
Else
With rstCallOuts
.Delete
End With
End If
rstCallOuts.MoveNext
Loop
End If
rstNames.MoveNext
Loop
End If
Exit Function
EH:
MsgBox Err.Number & ": " & Err.Description
Exit Function
End Function
Any ideas anyone?
Thanks in advance.
Gareth
Comment