VBA not handling end of For...Next when inserting into SQL
Hi Experts -
I have a spreadsheet that is looking at a concatenated list of values in column c and inserting them into a SQL table. It's working great until it hits the first empty cell in C. It's trying to send a null value to the table, so I'm getting this error:
Here's the code:
Private Sub CommandButton1_Click()Dim rng As RangeDim cell As RangeDim con As Object'connect and clear existing data in tableSet con = CreateObject("adodb.connection") con.connectionstring = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=BTS;Initial Catalog=vmfgtest; password=blocked" sSQL = "delete osk_release_num1" con.Open con.Execute sSQL'loop through data in column c and insert into tableSet rng = Worksheets("sheet1").Range("c2:c170")For Each cell In rng Set con = CreateObject("adodb.connection") con.connectionstring = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=BTS;Initial Catalog=vmfgtest; password=blocked" sSQL = "insert into osk_release_num1 (release_num, part_num, due_date) values ( " & cell & " )" con.Open con.Execute sSQL Next cell a = MsgBox("Success!", vbOKOnly) End Sub
Open in new window
before:Open in new window
to see whether it's a valid SQL statement or not. it seems you got problem with that.