我正在尝试将SQL Server连接到Access。我在模块中具有连接功能。我在另一个模块中调用此函数。
这是我模块中的代码:
'Variabel voor SQL Server Connectie
Public SQLConnectie As ADODB.Connection
'Connecten met SQL Server
Public Function DBConn() As ADODB.Connection
If Not (SQLConnectie Is Nothing) Then
Set SQLConnectie = New ADODB.Connection
With SQLConnectie
.CommandTimeout = 15
.Mode = adModeReadWrite
.ConnectionString = "Provider=SQLNCLI11;Server=dafehvmvdsql3;Database=PROVOMotorenfabriek;Integrated Security=SSPI; Persist Security Info=False"
.Open
End With
End If
Set DBConn = SQLConnectie
Set SQLConnectie = Nothing
End Function
在执行SQL Server中存储过程的模块中的代码下方:
Call DBConn.Execute("EXEC spStoringToevoegen " & productielijnMW & ", " & Forms(Formnaam)!cbLijngedeelte & ".............etc
我得到错误:未设置对象变量或With块变量。我找到的每个答案都说我需要将set放在一些变量的前面,但是我找不到应该是哪个变量。
预先感谢
答案 0 :(得分:1)
将其添加到样本参数中。
正如我所说,我的VBA非常生锈(而且我发现VBA尤其适用于奇怪的语言,请添加Access使其对我来说听起来很痛苦)。如果您使用其他一些后端(以及语言),将会容易得多。无论如何,这是在VBA(Excel)中具有参数的示例:
Sub Macro1()
Dim oRecordset1 As ADODB.Recordset
Dim oConnection As ADODB.Connection
Dim oCommand As ADODB.Command
Dim oParameter1 As ADODB.Parameter
Dim oParameter2 As ADODB.Parameter
Set oConnection = New ADODB.Connection
Set oCommand = New ADODB.Command
oConnection.ConnectionString = "Provider=SQLNCLI11.0;Data Source=.\SQLExpress;Trusted_connection=Yes"
oConnection.Open
oCommand.ActiveConnection = oConnection
oCommand.CommandType = 4
oCommand.CommandText = "Northwind.dbo.[CustomersSelectLike]"
Set oParameter1 = oCommand.CreateParameter("@country", 130, 1, -1) ' adWChar
oCommand.Parameters.Append oParameter1
oCommand.Parameters("@country").Value = "USA"
Set oParameter2 = oCommand.CreateParameter("@customer", 130, 1, -1)
oCommand.Parameters.Append oParameter2
oCommand.Parameters("@customer").Value = "%"
Set oRecordset = oCommand.Execute()
Sheet1.Range("A1").CopyFromRecordset (oRecordset)
End Sub