I'm having trouble with an SQL Server INSERT Query.
First, here's what I am trying to achieve:
I have a list of product in an excel worksheet that I would like to export into a MS SQL Server.
Here is the code I tried:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim Client As String
Set cn = New ADODB.Connection
Set cmd = New ADODB.Command
//Worksheets("Config").cells(1, "B").Value contains the connection string
cn.Open Worksheets("Config").Cells(1, "B").Value
cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
If Worksheets("Configuration").Cells(2, "B").Value = 1 Then
cmd.CommandText = "SELECT * FROM Cardex WHERE NbCardex = ?"
ElseIf Worksheets("Configuration").Cells(2, "B").Value = 0 Then
cmd.CommandText = "SELECT * FROM Cardex WHERE Name = ?"
End If
cmd.Prepared = True
cmd.Parameters.Append cmd.CreateParameter("Client", adVarChar, adParamInput, 250,
UCase(Worksheets("Exporter").Cells(2, "B").Value))
Set rs = cmd.Execute
If Not rs.EOF Then
Client = rs.Fields("NoCardex").Value
End If
rs.Close
//The code work until it reach this point, The next lines I am not sure what is wrong
//Hell I'm not even sure what i am doing.
//Problem is, nothing is added into the table
Dim cell As Range
For Each cell In Worksheets("Exporter").Range("Liste")
If cell.Row > 4 And Not cell.Value = vbNullString Then
Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandType = adCmdText
cmd.CommandText = "INSERT INTO NewTable(Dte, NbTr, NbClient, NbTr2, NbLine, NbProd,
Desc, Qty, Cost, Prc, FlImp, FLDone)
VALUES(?Date, ?Trans, ?Client, ?Projet, ?Ligne, ?Prod, ?Desc,
?Qte, ?Cout, ?Vend, ?Imp, 0)"
cmd.Prepared = True
cmd.Parameters.Append cmd.CreateParameter("Date", adVarChar, adParamInput, 8, Worksheets("Exporter").Cells(1, "E").Value)
cmd.Parameters.Append cmd.CreateParameter("Trans", adVarChar, adParamInput, 15, Worksheets("Exporter").Cells(1, "B").Value)
cmd.Parameters.Append cmd.CreateParameter("Client", adVarChar, adParamInput, 15, Client)
cmd.Parameters.Append cmd.CreateParameter("Projet", adVarChar, adParamInput, 20, Worksheets("Exporter").Cells(2, "E").Value)
cmd.Parameters.Append cmd.CreateParameter("Ligne", adInteger, adParamInput, , (cell.Row - 4))
cmd.Parameters.Append cmd.CreateParameter("Prod", adVarChar, adParamInput, 30, cell.Value)
cmd.Parameters.Append cmd.CreateParameter("Desc", adVarChar, adParamInput, 8000, Worksheets("Exporter").Cells(cell.Row, "B").Value)
cmd.Parameters.Append cmd.CreateParameter("Qte", adCurrency, adParamInput, , Worksheets("Exporter").Cells(cell.Row, "C").Value)
cmd.Parameters.Append cmd.CreateParameter("Cout", adCurrency, adParamInput, , Worksheets("Exporter").Cells(cell.Row, "D").Value)
cmd.Parameters.Append cmd.CreateParameter("Vend", adCurrency, adParamInput, , Worksheets("Exporter").Cells(cell.Row, "E").Value)
cmd.Parameters.Append cmd.CreateParameter("Imp", adSmallInt, adParamInput, , Worksheets("Exporter").Cells(cell.Row, "F").Value)
cmd.Execute()
End If
Next
cn.Close
I get an error that says:
Execution error '-2147217900 (80040e14)':
The Scalar variable "@P1Date" must be declare.
Can anyone help me?
PS I know VBA comment aren't done with "//" but the "'" make the code less easier to read on here so I switch them out
cmd.Parameters.Append cmd.CreateParameter("Date", adVarChar, adParamInput, 8, Worksheets("Exporter").Cells(1, "E").Value)