【问题标题】:Error setting VBA Recordset of form from ADODB.recordset从 ADODB.recordset 设置表单的 VBA 记录集时出错
【发布时间】:2023-03-05 11:16:01
【问题描述】:

我在 MS Access 2010 中使用 VBA 创建了一个函数来执行 SQL 服务器存储过程并在 ADODB.Recordset 对象中返回值。但是,我无法使用从 ADODB 连接返回的记录集设置 MS Access 表单 RecordSource 或 Recordset。

下面是代码摘录:

Dim objRs As ADODB.Recordset
Set objRs = call_proc("mySQLProc", "param")
Set Forms("form1").Recordset = objRs

call_proc的函数头:

Public Function call_proc(procName As String, procVal As String) As ADODB.Recordset

如果我遍历 objRS 并执行 Debug.Print,我可以看到所有记录。所以我知道数据在那里。只是不知道如何修复将数据绑定到表单的错误。 下面这行代码返回错误:

Set Forms("form1").Recordset = objRs

欢迎接受任何建议。 先感谢您。

【问题讨论】:

  • 嗨@hansUp,它返回“Nothing”
  • 仅供参考,Debug.Print TypeName(objRS) 确实返回 Recordset
  • 请参阅resource 将表单绑定到 ADO 记录集。它可能涉及连接 OLEDB/ODBC 驱动程序和记录集类型(即 adLockOptimistic、adOpenDynamic)。请出示您的完整连接代码。

标签: ms-access vba ms-access-2010


【解决方案1】:

修复它。问题出在我的 call_proc 函数中。当我打开 ADODB.Recordset 时,我没有设置光标位置。请参阅下面我添加 "'

的代码
Public Function call_proc(procName As String, procVal As String) As ADODB.Recordset

    ' Initialize variables.
    Dim cn As New ADODB.Connection
    Dim objCmd As New ADODB.Command
    Dim objParm1 As New ADODB.Parameter
    Dim objRs As New ADODB.Recordset
    Dim ServerName As String, DatabaseName As String


   ServerName = "YourServerName"
   DatabaseName = "YourDatabaseName"

   ' Specify the OLE DB provider.
   cn.Provider = "sqloledb"

   ' Set SQLOLEDB connection properties.
   cn.Properties("Data Source").Value = ServerName
   cn.Properties("Initial Catalog").Value = DatabaseName
   cn.CursorLocation = adUseClient ' <---#####ADD THIS

   ' Windows authentication.
   cn.Properties("Integrated Security").Value = "SSPI"


  ' Set CommandText equal to the stored procedure name.
   objCmd.CommandText = procName 
   objCmd.CommandType = adCmdStoredProc

  ' Open the database.
  cn.Open

  objCmd.ActiveConnection = cn

  ' Automatically fill in parameter info from stored procedure.
  objCmd.Parameters.Refresh



  ' Set the param value.
   objCmd(1) = procVal


  Set call_proc = objCmd.Execute
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多