I am using a SQL server database to hold data.
The data is entered onto the excel sheet, and a macro then runs to insert the data into a SQL database.
One of the cells on the excel sheet has a value called 'ONE HUNDRED'. When I run the macro, I get an error saying 'Incorrect syntax near ONE HUNDRED'
Could someone please advise why I am getting this error?
Code is below.
Sub server()
Dim sConnString As String
Set objMyConn = New ADODB.Connection
Set objMyCmd = New ADODB.Command
Set objMyRecordset = New ADODB.Recordset
objMyConn.ConnectionString = "Provider=sqloledb;Data Source=xx;Initial Catalog=xx;Integrated Security=SSPI;": MsgBox "Connection Successful"
objMyConn.Open
Set objMyCmd.ActiveConnection = objMyConn
Dim val1 As String, val2 As String, val3 As String, val4 As String, val5 As String
val1 = Range("D9").Value
val2 = Range("D10").Value
val3 = Range("D11").Value
val4 = Range("D12").Value
val5 = Range("D13").Value
Dim item1 As String
item1 = "INSERT INTO [xx].[dbo].[server]("
item1 = item1 & " [server_name],[ip_address], [phys_location], [phys_or_virt], [it_contact] "
item1 = item1 & " )Values("
item1 = item1 & " '" & val1 & "', '" & val2 & "', '" & val3 & "', '" & val4 & "', '" & val5 & "'"
objMyConn.Execute item1
End Sub
")".