I am trying to get Excel VBA to update values in a SQL Database using a VBA User form.
The code that I have so far is:
Private Sub Btn_UpdateRepository_Click()
On Error GoTo ErrorHandler
' Get the RowID from the textbox
Dim rowID As Long
rowID = CLng(Txt_RowID.Value)
' Construct the SQL query to update the contact details
Dim strSQL As String
strSQL = "UPDATE [LLC].[LA_Contacts] SET " & _
"LocalAuthority = '" & Cbo_LocalAuth_Update.Value & "', " & _
"Department = '" & Cbo_Deparment_Update.Value & "', " & _
"JobTitle = '" & Cbo_JobTitle_Update.Value & "', " & _
"RolesAndResponsibilities = '" & Cbo_RolesAndResponsibilities_Update.Value & "', " & _
"HMLRProgrammeRole = '" & Cbo_HMLRRole_Update.Value & "', " & _
"ContactName = '" & Txt_FullName_Update.Value & "', " & _
"Email = '" & Txt_EmailAddress_Update.Value & "', " & _
"TelephoneNumber = '" & Txt_ContactNumber_Update.Value & "', " & _
"MainContact = '" & IIf(Tgl_MainContactYes_Update.Value, "Yes", "No") & "', " & _
"EngagedWithHMLR = '" & IIf(Tgl_EngagedYes_Update.Value, "Yes", "No") & "', " & _
"GeneralComments = '" & Txt_GeneralComments_Update.Value & "', " & _
"LastUpdated = '" & Format(Txt_InputDate.Value, "yyyy-mm-dd") & "', " & _
"Archived = 'No' " & _
"WHERE ID = " & rowID
' Debugging: Print the SQL query to Immediate Window
Debug.Print "SQL Query: " & strSQL
' Open the connection using the openConnection subroutine
openConnection LLCServerLocation
' Execute the SQL query
cn.Execute strSQL
' Close the connection
closeConnection False
' Inform the user that contact details have been updated successfully
MsgBox "Contact details updated successfully.", vbInformation
' Close the form
Unload Me
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbCritical
closeConnection False
End Sub
I get a run-time error ‘424;: Object required error code, and the Deggbuer highlights the strSQL part of the code.
strSQL = "UPDATE [LLC].[LA_Contacts] SET " & _
"LocalAuthority = '" & Cbo_LocalAuth_Update.Value & "', " & _
"Department = '" & Cbo_Deparment_Update.Value & "', " & _
"JobTitle = '" & Cbo_JobTitle_Update.Value & "', " & _
"RolesAndResponsibilities = '" & Cbo_RolesAndResponsibilities_Update.Value & "', " & _
"HMLRProgrammeRole = '" & Cbo_HMLRRole_Update.Value & "', " & _
"ContactName = '" & Txt_FullName_Update.Value & "', " & _
"Email = '" & Txt_EmailAddress_Update.Value & "', " & _
"TelephoneNumber = '" & Txt_ContactNumber_Update.Value & "', " & _
"MainContact = '" & IIf(Tgl_MainContactYes_Update.Value, "Yes", "No") & "', " & _
"EngagedWithHMLR = '" & IIf(Tgl_EngagedYes_Update.Value, "Yes", "No") & "', " & _
"GeneralComments = '" & Txt_GeneralComments_Update.Value & "', " & _
"LastUpdated = '" & Format(Txt_InputDate.Value, "yyyy-mm-dd") & "', " & _
"Archived = 'No' " & _
"WHERE ID = " & rowID
I’m not sure where I’m going wrong with this one, I have all the necessary dB Connection Queries set up in another module but they are easily referenced and work when querying and pasting data from the db.
Any thoughts where I’m going wrong on this one?