0

I am using EXCEL macros/vba to insert data from the xls sheet into a SQL database.

Can anyone explain why I am getting this error message...

INCORRECT SYNTAX NEAR 'OPEN'

The code I run is an insert statement

 Dim insert As String

 insert = "INSERT INTO dbo.HEADER 
    (plant, 
    taskno, 
    tskstatus, 
    category, 
    opened, 
    openedby, 
    title) 
 VALUES 
    ( 'UK', 
    (select Max(taskno) AS count from HEADER)+ 1), 
    'open', 
    'RFSA', 
    CONVERT(datetime,'" & strdate & "',103), 
    '" & UserId & "', 
    '" & strtitle & "')"

The code inserts the following into a SQL database.

'OPEN' is just a value I am trying to insert into a collumn..

2 Answers 2

2

Your parenthesis don't match.

insert = "INSERT INTO dbo.HEADER 
    (plant, 
    taskno, 
    tskstatus, 
    category, 
    opened, 
    openedby, 
    title) 
 VALUES 
    ( 'UK', 
    (select Max(taskno) AS count from HEADER)+ 1, 
    'open', 
    'RFSA', 
    CONVERT(datetime,'" & strdate & "',103), 
    '" & UserId & "', 
    '" & strtitle & "')"
Sign up to request clarification or add additional context in comments.

Comments

1

You want to use insert . . . select syntax:

INSERT INTO dbo.HEADER(plant, taskno, tskstatus, category, opened, openedby, title) 
    SELECT 'UK', Max(taskno) + 1, 'open', 'RFSA', 
           CONVERT(datetime,'" & strdate & "',103), 
           '" & UserId & "', 
           '" & strtitle & "'
    from Header;

However, because you are using SQL Server, you want taskno to be an identity column. This will automatically increment it on each insert.

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.