运行后出现byref参数类型不符Public FilePath As String
Public Cnn As New ADODB.Connection
Public Rcdst As New ADODB.Recordset
Public strSql As StringFunction Getrs(strSql As String) As ADODB.Recordset
    With Cnn
        FilePath = App.Path & "\realty.mdb"
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
        .ConnectionString = .ConnectionString & "Data   Source=" & FilePath & ";"
        .ConnectionString = .ConnectionString & "Jet   OLEDB:"
        .Open
    End With
    With Rcdst
        .ActiveConnection = Cnn
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .Source = strSql
        .Open
    End With
 End Function
.................
Private Sub Command8_Click()    '第一条记录
Dim rs As Recordset    If Rcdst.State = 1 Then
        Rcdst.MoveFirst
        ShowData
        Exit Sub
    Else
        Set rs = Getrs(strSql1)
        rs.MoveFirst
        ShowData
        Exit Sub
    End If

解决方案 »

  1.   

    这样试试:
    Dim   rs   As   New Recordset 
      

  2.   

    Function Getrs(Byval strSql As String) As ADODB.Recordset 
      

  3.   

    有点看不明白。“strSql1”是哪来的?没声明啊。还有Getrs是用来打开Rcdst的连接的,而且也没有赋值,为什么要把它赋值给rs呢??
      

  4.   

    谢谢楼上诸位,改成如下后,出现“未能连接到数据库,对象关闭时,不允许操作”,请帮忙。
    Public FilePath As String
    Public Cnn As New ADODB.Connection
    Public Rcdst As New ADODB.Recordset
    Public strSql As String
    Public SqlWhere As String
    Public Myt As Single
    Function Getrs(ByVal strSql As String) As ADODB.Recordset
            With Cnn
                    FilePath = App.Path & "\realty.mdb"
                    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                    .ConnectionString = .ConnectionString & "Data Source=" & FilePath & ";"
                    .ConnectionString = .ConnectionString & "Jet OLEDB:"
                    .Open
            End With
            With Rcdst
                    .ActiveConnection = Cnn
                    .CursorLocation = adUseClient
                    .CursorType = adOpenStatic
                    .LockType = adLockReadOnly
                    .Source = strSql
                    .Open
            End With
      End Function
    ........................
    Private Sub Command8_Click()    '第一条记录
        On Error GoTo ConnectionERR
        Dim strSql1 As String
        Dim rs As New Recordset
        strSql1 = "select * from rltdata"        If Rcdst.State = 1 Then
                    Rcdst.MoveFirst
                    ShowData
                    Exit Sub
            Else
                   Set rs = Getrs(strSql1)
                    rs.MoveFirst
                    ShowData
                    Exit Sub
            End If
            
    ConnectionERR:
        MsgBox "未能连接到案例库," & Err.Description & "将关闭!", vbCritical, "出错"
        Unload Me
        Exit Sub
        Cnn.Close
        Set Cnn = Nothing
    End Sub
      

  5.   


    .ConnectionString   =   "Provider=Microsoft.Jet.OLEDB.4.0;" 
                                    .ConnectionString   =   .ConnectionString   &   "Data   Source="   &   FilePath   &   ";" 
                                    .ConnectionString   =   .ConnectionString   &   "Jet   OLEDB:" 多次直接设置连接字符串,当然出错啦
    先用一个临时变量连接好字符串后再一次性给.ConnectionString赋值