VBA Excel SQL Server INSERT查询

时间:2014-05-20 14:25:10

标签: sql sql-server excel vba excel-vba

我遇到了SQL Server INSERT查询问题。

首先,这是我想要实现的目标:

我在excel工作表中有一个产品列表,我想将其导出到MS SQL Server。

这是我尝试过的代码:

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

我收到错误消息:

Execution error '-2147217900 (80040e14)':
The Scalar variable "@P1Date" must be declare.

任何人都可以帮助我吗?

PS我知道VBA的评论不是用" //"但是"'"使代码更容易在这里阅读,所以我把它们切换出来

1 个答案:

答案 0 :(得分:2)

我认为命名参数不起作用。尝试使用:

cmd.CommandText = "INSERT INTO NewTable(Dte, NbTr, NbClient, NbTr2, NbLine, NbProd, 
                     Desc, Qty, Cost, Prc, FlImp, FLDone) 
                    VALUES(?, ?, ?, ?, ?, ?, ?, 
                     ?, ?, ?, ?, 0)"

并确保稍后追加的参数顺序正确。 ;)