magmike said:
How would I write an If Statement to first determine whether or not
the record already exists by ID field, and then UPDATE if so, or
APPEND if not? I know how to write the SQL, but not certain how to use
an IF Statement to determine if the record already exist or not.
There are a couple of different ways you might do this. I usually do it via
a recordset, like this:
'---- start of example code 1 ----
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset( _
"SELECT * FROM MyTable WHERE ID=" & lngID)
With rs
If .EOF Then
.AddNew
' New record, so set the ID field.
!ID = lngID
Else
.Edit
End If
!Field1 = strField1
!Field2 = dblField2
' ... and so on ...
.Update
.Close
End With
'---- end of example code 1 ----
You could also do it by using DLookup to check if the record exists, and
then choosing/building SQL statements accordingly; e.g.:
'---- start of example code 2 ----
Dim strSQL As String
If IsNull(DLookup("ID", "MyTable", "ID=" & lngID)) Then
strSQL = _
"INSERT INTO MyTable(ID, Field1, Field2) " & _
"VALUES(" & lngID & "," & Chr(34) & strField1 & Chr(34) & _
"," & dblField2 & ")"
Else
strSQL = _
"UPDATE MyTable SET Field1 = " & _
Chr(34) & strField1 & Chr(34) & _
", Field2 = " & dblField2 & _
" WHERE ID=" & lngID
End If
CurrentDb.Execute strSQL, dbFailOnError
'---- end of example code 2 ----