0
votes

I am new to Access. I have made a form to put the data in the table. I have one one combo box and one text box in the userform.

Below is my code:

Private Sub add_Click()
'Add Data to the GroupHeads Table
CurrentDb.Execute "Insert into GroupHeads(AccountHeads, GroupHeads)" & "  Values(" & Me.cboaccounthead & ", '" & Me.txtgrouphead & "')"

End Sub

Please review and advise why its giving runtime error.

Regards. Salman

1
This often happens because you've got a column name wrong. Confirm that the columns in the table match the names of the columns that you're inserting into. Also can you confirm that AccountHeads column is a numeric data type? Finally watch for Me.cboaccounthead containing quote characters as this will break the insert statement - you might need to wrap Me.cboaccounthead inside a Replace function.Skippy
Columns name are correct... AccountHeads and GroupHeads are defined as ShortText. How i will replace the combobox text using replace function..Salman Khan

1 Answers

0
votes

If both columns in the table are text then both values need to be enclosed in single quotes, so you need:

CurrentDb.Execute "Insert into GroupHeads(AccountHeads, GroupHeads) " & _
    "Values('" & Replace(Me.cboaccounthead, "'", "''") & "', " & _
    "'" & Replace(Me.txtgrouphead, "'", "''") & "')"