I need some help again, please.
I want to insert a record into tblDogCheckList . If a record for the DogID does not exist do an Insert. If a record with the same DogID already exists then do an Update only. MCTrfLodged is a date field.
When I run it and I check the table, nothing happened.
I know I must have something wrong.
Thank you, everyone!!
I want to insert a record into tblDogCheckList . If a record for the DogID does not exist do an Insert. If a record with the same DogID already exists then do an Update only. MCTrfLodged is a date field.
When I run it and I check the table, nothing happened.
I know I must have something wrong.
Thank you, everyone!!
Code:
Private Sub cmdMCTrfLodged_Click()
If Me.Dirty Then Me.Dirty = False 'This line Saves the Data just entered
Dim rnSQL As String
Dim varItem As Variant
Dim db As DAO.Database
Set db = CurrentDb
varItem = Me.lstPuppies.Column(0, varItem)
If DCount("DogID", "tblDogsCheckList", "DogID=" & varItem) = 0 Then
rnSQL = "INSERT INTO tblDogsChecklist (DogID, MCTrfLodged)" _
& " Values (p1, p2);"
For Each varItem In Me.lstPuppies.ItemsSelected
With db.CreateQueryDef("", rnSQL)
.Parameters("p1") = varItem
.Parameters("p2") = Me!TxtMCTrfLodged
.Execute dbFailOnError
End With
Next varItem
Else
rnSQL = "UPDATE tblDogsChecklist SET MCTrfLodged = Me!TxtMctrflodged" _
& " WHERE DogID = " & varItem & ";"
For Each varItem In Me.lstPuppies.ItemsSelected
varItem = Me.lstPuppies.Column(0, varItem)
Next varItem
End If
Set db = Nothing
End Sub
Last edited: