sql string error

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » SQL String Error

SQL String Error

resolvedResolved · High Priority · Version 2010

Adrian has attended:
Excel VBA Introduction course

SQL String Error

Hello,

I am having issues writing from a table in excel to an SQL database. I keep getting the error "Incorrect syntax near ','.". I have checked thoroughly and I can't see where the issue could come from. I have checked the output of the first loop in the intermediate window and can see that the values are all as expected, so I don't see where the cell values could be throwing the query? Code is below (N.B. I have removed the server name), any thoughts are very much welcome.

Thanks,

Adrian

-----

Sub UpdateCPLT()

Dim conn As ADODB.Connection
Dim sConnString As String
Dim iRowNo As Integer
Dim strcplt_id, strclass_id, strinitial_payout, strinitial_agg_attachment, strinitial_trigger_events, strnum_periods, strcurrency_curve_set_id, strclass_terms_revision, strmiu_data_version, strmiu_engine_version, strmiu_database_version, strdefault_currency_curve_set_id As Integer
Dim strstatus, strcurrency, struser_name, strstart_date, strend_date, strclass_start_date, strclass_end_date As String

With Sheets("Analysis_Import_Prep")

' Create the connection string.
sConnString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Adrian_Prep;Data Source=######\######"

' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection

' Open the connection and execute.
conn.Open sConnString

'Skip the header row
iRowNo = 2

'Loop until empty cell in ClassId
Do Until .Cells(iRowNo, 27) = ""

strcplt_id = .Cells(iRowNo, 27)
strclass_id = .Cells(iRowNo, 28)
strinitial_payout = .Cells(iRowNo, 29)
strinitial_agg_attachment = .Cells(iRowNo, 30)
strinitial_trigger_events = .Cells(iRowNo, 31)
strstart_date = Format(.Cells(iRowNo, 32), "yyyy/mm/dd") & " 00:00:00.000"
strend_date = Format(.Cells(iRowNo, 33), "yyyy/mm/dd") & " 00:00:00.000"
strstatus = .Cells(iRowNo, 34)
strnum_periods = .Cells(iRowNo, 37)
strcurrency_curve_set_id = .Cells(iRowNo, 39)
strcpltclass_terms_revision_id = .Cells(iRowNo, 40)
strcurrency = .Cells(iRowNo, 41)
strclass_start_date = Format(.Cells(iRowNo, 42), "yyyy/mm/dd") & " 00:00:00.000"
strclass_end_date = Format(.Cells(iRowNo, 43), "yyyy/mm/dd") & " 00:00:00.000"
struser_name = .Cells(iRowNo, 44)
strmiu_data_version = .Cells(iRowNo, 45)
strmiu_engine_version = .Cells(iRowNo, 46)
strmiu_database_version = .Cells(iRowNo, 47)
strdefault_currency_curve_set_id = .Cells(iRowNo, 48)

'Generate and execute sql statement to import the excel rows to SQL Server table
conn.Execute "set identity_insert cplt ON " & _
"Insert into cplt (cplt_id, class_id, initial_payout, initial_agg_attachment, initial_trigger_events, start_date, " & _
"end_date, status, pct_complete, message, num_periods, parent_cplt_id, currency_curve_set_id, class_terms_revision, " & _
"[currency], class_start_date, class_end_date, user_name, miu_data_version, miu_engine_version, miu_database_version, " & _
"default_currency_curve_set_id, run_queue_date, run_start_date, run_end_date) " & _
"values(" & strcplt_id & ", " & _
strclass_id & ", " & _
strinitial_payout & ", " & _
strinitial_agg_attachment & ", " & _
strinitial_trigger_events & ", " & _
"cast('" & strstart_date & "' as datetime), " & _
"cast('" & strend_date & "' as datetime), '" & _
strstatus & "', NULL, NULL, " & _
strnum_periods & ", NULL, " & _
strcurrency_curve_set_id & ", " & _
strclass_terms_revision & ", '" & _
strcurrency & "', " & _
"cast('" & strclass_start_date & "' as datetime), " & _
"cast('" & strclass_end_date & "' as datetime), '" & _
struser_name & "', " & _
strmiu_data_version & ", " & _
strmiu_engine_version & ", " & _
strmiu_database_version & ", " & _
strdefault_currency_curve_set_id & ", NULL, NULL, NULL)"

iRowNo = iRowNo + 1

Loop

MsgBox "CPLT imported"

conn.Close
Set conn = Nothing

End With

End Sub

RE: SQL String Error

Hi Adrian,

Thank you for the forum question.

I cannot see what is wrong with the SQL syntax, but I have some suggestions.

Type:

Option Explicit

Before your code on the top of your module. Press F8 and you will find that you have a variable which are not declared.

You should also change your Dim lines. You have a lot of Variant variables, which are not a good idea.

It is only the variable strdefault_currency_curve_set_id which are declared as Integer in the below declaration

Dim strcplt_id, strclass_id, strinitial_payout, strinitial_agg_attachment, strinitial_trigger_events, strnum_periods, strcurrency_curve_set_id, strclass_terms_revision, strmiu_data_version, strmiu_engine_version, strmiu_database_version, strdefault_currency_curve_set_id As Integer


It is only the variable strclass_end_date which are declared as String in the below declaration

Dim strstatus, strcurrency, struser_name, strstart_date, strend_date, strclass_start_date, strclass_end_date As String

The rest of them is declared as Variants.

You have to type As Integer or As String after the name of each variable.

If you open the Locals window and press F8 you can see how your variables are declared to the right.

This is probably not the reason for your syntax error, but it could be.

The next step after changing the declarations could be to check the source.

If you execute the code below, you will find out if you may have an error in the source data (If you have a wrong data type in one ore more cells excel will break the code and give you the error "Run Time Error 13" "Type Mismatch").

Sub TestSource()

With Sheets("Analysis_Import_Prep")


Do Until .Cells(iRowNo, 27) = ""

strcplt_id = .Cells(iRowNo, 27)
strclass_id = .Cells(iRowNo, 28)
strinitial_payout = .Cells(iRowNo, 29)
strinitial_agg_attachment = .Cells(iRowNo, 30)
strinitial_trigger_events = .Cells(iRowNo, 31)
strstart_date = Format(.Cells(iRowNo, 32), "yyyy/mm/dd") & " 00:00:00.000"
strend_date = Format(.Cells(iRowNo, 33), "yyyy/mm/dd") & " 00:00:00.000"
strstatus = .Cells(iRowNo, 34)
strnum_periods = .Cells(iRowNo, 37)
strcurrency_curve_set_id = .Cells(iRowNo, 39)
strcpltclass_terms_revision_id = .Cells(iRowNo, 40)
strcurrency = .Cells(iRowNo, 41)
strclass_start_date = Format(.Cells(iRowNo, 42), "yyyy/mm/dd") & " 00:00:00.000"
strclass_end_date = Format(.Cells(iRowNo, 43), "yyyy/mm/dd") & " 00:00:00.000"
struser_name = .Cells(iRowNo, 44)
strmiu_data_version = .Cells(iRowNo, 45)
strmiu_engine_version = .Cells(iRowNo, 46)
strmiu_database_version = .Cells(iRowNo, 47)
strdefault_currency_curve_set_id = .Cells(iRowNo, 48)



iRowNo = iRowNo + 1

Loop

End With
End Sub

If you get a mismatch error it can be the reason of the SQL syntax error ('"TextVariable"','NumberVariables' syntax). A variant variable can both store text and numbers so please change the variable declarations to find out if the SQL syntax is correct.

If you still have a problem, I would rewrite the SQL code. It is so easy to miss something and by typing it again you will do it with "fresh" eyes.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector


RE: SQL String Error

Hi Jens,

Thanks ever so much for your very detailed response. It transpires that the first thing you suggested was the source of my issue. It was purely the Dim lines that needed listing out and then it seemed to work. Thanks also for the F8 suggestion, I forgot that from the course the other day and it was helpful; today stepping through another tricky section of the module I'm working on.

Thanks again for the help and for the course the other day. Hope you have a great weekend.

Best,

Adrian

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Move to edge of data block

When data is held in a block, however large, use the Ctrl key with your cursors to move quickly to the far edge of the block. Works with all 4 direction keys.

View all Excel hints and tips


Server loaded in 0.08 secs.