Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 从ADODB.Recordset设置窗体的VBA记录集时出错_Ms Access_Vba_Ms Access 2010 - Fatal编程技术网

Ms access 从ADODB.Recordset设置窗体的VBA记录集时出错

Ms access 从ADODB.Recordset设置窗体的VBA记录集时出错,ms-access,vba,ms-access-2010,Ms Access,Vba,Ms Access 2010,我在MS Access 2010中使用VBA创建了一个函数,以执行SQL server存储过程并在ADODB.Recordset对象中返回值。但是,我无法使用ADODB连接返回的记录集设置MS Access窗体记录源或记录集 下面是代码摘录: Dim objRs As ADODB.Recordset Set objRs = call_proc("mySQLProc", "param") Set Forms("form1").Recordset = objRs call_proc的函数头: Pu

我在MS Access 2010中使用VBA创建了一个函数,以执行SQL server存储过程并在ADODB.Recordset对象中返回值。但是,我无法使用ADODB连接返回的记录集设置MS Access窗体记录源或记录集

下面是代码摘录:

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
如果我遍历OBJR并执行Debug.Print,我就能看到所有记录。所以我知道数据就在那里。只是不知道如何修复将数据绑定到表单的错误。 下面的代码行返回错误:

Set Forms("form1").Recordset = objRs

请接受任何建议。
先谢谢你

修复了它。问题出在我的call_proc函数中。当我打开ADODB.Recordset时,我没有设置光标的位置。请参阅下面我添加的代码“Hi@hansUp,它返回“Nothing”,只是一个仅供参考的调试。打印类型名(objRS)确实返回记录集请参见将此绑定到ADO记录集的表单。它可能涉及OLEDB/ODBC驱动程序的连接和记录集的类型(即AdLockOptimization、adOpenDynamic)。请显示完整的连接代码。
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