我正在尝试在Excel中创建一个userform,允许用户向现有的SQL Server表添加新的列和描述。
我在VBA中使用ADO库。要向创建的列添加描述,我需要运行系统存储过程sp_addextendedproperty。
我无法将参数传递给它,我总是收到错误错误-2147217900:为过程'sp_addextendedproperty'指定了无效的参数或选项。
Private Sub InsertField_bt_Click()
Set con = New ADODB.Connection
con.Provider = "sqloledb"
sConnectionString = "Server=rumossql12;Database=PharmNet;UID=(login here);Pwd=(password here)"
con.Open sConnectionString
Set cmd = New ADODB.Command
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = con
cmd.CommandText = "sp_addextendedproperty"
Dim name As ADODB.Parameter
Dim value As ADODB.Parameter
Dim level0type As ADODB.Parameter
Dim level0name As ADODB.Parameter
Dim level1type As ADODB.Parameter
Dim level1name As ADODB.Parameter
Dim level2type As ADODB.Parameter
Dim level2name As ADODB.Parameter
Set name = cmd.CreateParameter("@name", adVarWChar, adParamInput, 128, "N'MS_Description'")
cmd.Parameters.Append name
Set value = cmd.CreateParameter("@value", adVariant, adParamInput, , "N'" & InsertField.TextBox2.value & "'")
cmd.Parameters.Append value
Set level0type = cmd.CreateParameter("@level0type", adVarChar, adParamInput, 128, "N'SCHEMA'")
cmd.Parameters.Append level0type
Set level0name = cmd.CreateParameter("@level0name", adVarWChar, adParamInput, 128, "N'dbo'")
cmd.Parameters.Append level0name
Set level1type = cmd.CreateParameter("@level1type", adVarChar, adParamInput, 128, "N'TABLE'")
cmd.Parameters.Append level1type
Set level1name = cmd.CreateParameter("@level1name", adVarWChar, adParamInput, 128, "N'tbConsolidator'")
cmd.Parameters.Append level1name
Set level2type = cmd.CreateParameter("@level2type", adVarChar, adParamInput, 128, "N'COLUMN'")
cmd.Parameters.Append level2type
Set level2name = cmd.CreateParameter("@level2name", adVarWChar, adParamInput, 128, "N'" & InsertField.TextBox1.value & "'")
cmd.Parameters.Append level2name
On Error GoTo ErrHandler
sSQL = "ALTER TABLE PharmNet.dbo.tbConsolidator ADD " & InsertField.TextBox1.value & " NVARCHAR(255) NULL"
'con.Execute sSQL
cmd.Execute
InsertField.Hide
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "ErrorThingy"
Sheet2.Cells(1, 1).value = "Error " & Err.Number & ": " & Err.Description
con.Close
Set con = Nothing
End Sub
为什么会发生?