I am having trouble using SQL UPDATE statement in VBA.
I have:
Forms:
-"frmClaim": Main form. Data is based on table "claim." It has ClaimID, and other fields. ClaimID is the auto number and unique identifier.
-"frmClaimProduc ts":Subform attatched to frmClaim. This form is based on table "claim_product. " It has ClaimID, ProductID, TotalCost, and other fields.
-"frmClaimTransa ction": Subform attatched to frmClaim. This form is based on table "claim_transact ion." It has ClaimID, Date, Transaction Type, Amount, and other fields.
What I have done so far:
When someone create new record in "frmClaim," claim_id, transaction_id, date, and amount are inserted into table claim_transacti on. Below is the code. This works perfectly.
What I want to do now:
When someone changes value in the frmClaimProduct s, I want to update the changes into table claim_transacti on automatically, so I wrote VBA code "On After Update" of form frmClaimProduct s. But it keeps giving me error, "Syntax error in UPDATE statement." I looked into the code and I couldn't find anything wrong by myself. Below is the code... Please help!!
I have:
Forms:
-"frmClaim": Main form. Data is based on table "claim." It has ClaimID, and other fields. ClaimID is the auto number and unique identifier.
-"frmClaimProduc ts":Subform attatched to frmClaim. This form is based on table "claim_product. " It has ClaimID, ProductID, TotalCost, and other fields.
-"frmClaimTransa ction": Subform attatched to frmClaim. This form is based on table "claim_transact ion." It has ClaimID, Date, Transaction Type, Amount, and other fields.
What I have done so far:
When someone create new record in "frmClaim," claim_id, transaction_id, date, and amount are inserted into table claim_transacti on. Below is the code. This works perfectly.
Code:
Public Function get_claimId() As String get_claimId = CLng(Nz(claim_id.value)) 'lblClaimID.Caption End Function Private Sub Form_Current() Dim SQLtext As String passID = get_claimId DoCmd.SetWarnings False Dim CheckCost As String Dim strTotalCost As String If Nz(DLookup("claim_id", "claim_product", "claim_id=" & passID)) = vbNullString Then SQLtext = "INSERT INTO claim_transaction (claim_id, transaction_id, amount, [date])" SQLtext = SQLtext & "VALUES('" & passID & "', '15', '0', Date())" DoCmd.RunSQL SQLtext SQLtext = vbNullString Else If Nz(DLookup("claim_id", "claim_transaction", "transaction_id=15 And claim_id=" & passID)) = vbNullString Then SQLtext = "INSERT INTO claim_transaction (claim_id, transaction_id, amount, [date])" ' ***************************************************************** 'Description of transaction_id=15 is "tortal product cost." '****************************************************************** SQLtext = SQLtext & "VALUES('" & passID & "', '15', '" & Me!frmClaimProductsTotal!SumOfTotalCost & "',Date())" DoCmd.RunSQL SQLtext SQLtext = vbNullString End If End If End Sub
What I want to do now:
When someone changes value in the frmClaimProduct s, I want to update the changes into table claim_transacti on automatically, so I wrote VBA code "On After Update" of form frmClaimProduct s. But it keeps giving me error, "Syntax error in UPDATE statement." I looked into the code and I couldn't find anything wrong by myself. Below is the code... Please help!!
Code:
Public Function get_claimId() As String get_claimId = CLng(Nz(claim_id.value)) 'lblClaimID.Caption End Function Private Sub Form_AfterUpdate() passID = get_claimId DoCmd.SetWarnings False Dim PrdctTrnsc As String Dim SQLtext As String PrdctTrnsc = DLookup("amount", "claim_transaction", "transaction_id=15 And claim_id=" & passID) DoCmd.SetWarnings False If PrdctTrnsc <> Me!TotalCost Then SQLtext = "Update claim_transaction" SQLtext = SQLtext & "Set claim_id = '" & passID & "', transaction_id = '15', amount= '" & Me.TotalCost & "', date= Date()" SQLtext = SQLtext & "WHERE (((claim_id)='" & passID & "'))" DoCmd.RunSQL SQLtext SQLtext = vbNullString End If DoCmd.SetWarnings True End Sub
Comment