0

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
2
  • and the cell is val5, right? Commented Feb 19, 2015 at 12:21
  • 5
    You have a missing ")". Commented Feb 19, 2015 at 12:23

2 Answers 2

4

Try closing parenthesis at the end sql string

Sign up to request clarification or add additional context in comments.

Comments

1

Add a parenthesis at the end of item1

item1 = item1 & "  '" & val1 & "', '" & val2 & "', '" & val3 & "', '" & val4 & "', '" & val5 & "')"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.