以下的函数中,我的记录集rsErp若在函数外定义一个公用的记录集,当关闭调用此函数的窗体时,会出现"当前行不可用"的警告,若点击的是接收这个数据的DB控件除前两列以外的列,当我关闭此窗体时,我的整个系系统都分死掉,不知为何,请高手指点一二,谢谢!
'查询数据库记录集并绑定到窗体
'------------------------------------
Public Function FindErp(ByVal strLot As String, ByVal strFactory As String, ByVal strBom As String, ByVal strSale As String, ByVal beginDate As Date, ByVal endDate As Date, ByVal strSafe As String, ByVal strCrt As String, ByVal strMB As String, ByVal strAuthor As String, ByVal strOdfMood As String, ByVal strWheTher As String) As Boolean
    Dim strSql As String
'    Dim rsErp As New ADODB.Recordset
'    beginDate = Format(beginDate, "yyyy-mm-dd")
'    endDate = Format(endDate, "yyyy-mm-dd")
    
    '设置连接查询字符串
    '------------------------------------
    strSql = "select lotNo as 批次,Factory as 工厂,bomNo as 机型,Account as 销售工厂,makeDate as 拟制日期, SafeList as 安全件清单,Sp as 备损,Chassis as 机芯,tOrder as 翻单,UecnNo as 截止UECN编号,Qty as 数量,CtrNo as CRT编号,MainBd as 主板编号,Author as 拟制人,pCrtList as 配管表,Client as 客户,ClientModel as 客户型号,Tablet as 品牌,odfMood as 订单状态,Re as 备注 "
    strSql = strSql & "from makeErp where lotno like '" & Trim(strLot) & Trim("%") & "' and Factory='" & Trim(strFactory) & "'and BomNo like '" & Trim(strBom) & Trim("%") & "' and Account like '" & Trim(strSale) & Trim("%") & "'and makeDate between '" & beginDate & "' and '" & endDate & "' and SafeList like '" & Trim(strSafe) & Trim("%") & "' "
    strSql = strSql & "and CtrNo like '" & Trim(strCrt) & Trim("%") & "' and MainBd like '" & Trim(strMB) & Trim("%") & "' and Author like '" & Trim(strAuthor) & Trim("%") & "' and odfMood like '" & Trim(strOdfMood) & Trim("%") & "' and WheTher like '" & Trim(strWheTher) & Trim("%") & "' order by makedate asc"
    
    '连接记录集查询
    '-------------------------------------
    On Error GoTo rsErpErr
    rsErp.Open strSql, dbConn, adOpenStatic, adLockReadOnly
    
    '将记录集移动到最末端
    '-------------------------------------
    rsErp.MoveLast
    
    '显示报告记录数
    '-------------------------------------
    lblRs.Caption = "记录总数为:" & rsErp.RecordCount
    
    '绑定记录集到数据控件
    '-------------------------------------
    Set dgErp.DataSource = rsErp
    Set txtLot.DataSource = rsErp
    Set txtBom.DataSource = rsErp
    Set txtMain.DataSource = rsErp
    Set txtQty.DataSource = rsErp
    Set txtChassis.DataSource = rsErp
    Set txtUecn.DataSource = rsErp
    Set txtSp.DataSource = rsErp
    Set txtCrt.DataSource = rsErp
    Set txtPCrt.DataSource = rsErp
    Set txtClient.DataSource = rsErp
    Set txtClientMod.DataSource = rsErp
    Set txtTOrder.DataSource = rsErp
    Set txtTablet.DataSource = rsErp
    Set txtRe.DataSource = rsErp
    Set comSales.DataSource = rsErp
    Set comSafe.DataSource = rsErp
    Set comOdfMood.DataSource = rsErp
    Set txtFactory.DataSource = rsErp
    Set txtAuthor.DataSource = rsErp
    Set txtDate.DataSource = rsErp
    txtLot.DataField = "批次"
    txtBom.DataField = "机型"
    txtMain.DataField = "主板编号"
    txtQty.DataField = "数量"
    txtChassis.DataField = "机芯"
    txtUecn.DataField = "截止UECN编号"
    txtSp.DataField = "备损"
    txtCrt.DataField = "CRT编号"
    txtPCrt.DataField = "配管表"
    txtClient.DataField = "客户"
    txtClientMod.DataField = "客户型号"
    txtTOrder.DataField = "翻单"
    txtTablet.DataField = "品牌"
    txtRe.DataField = "备注"
    comSales.DataField = "销售工厂"
    comSafe.DataField = "安全件清单"
    comOdfMood.DataField = "订单状态"
    txtFactory.DataField = "工厂"
    txtAuthor.DataField = "拟制人"
    txtDate.DataField = "拟制日期"
    Exit Function
    
'错误处理过程
'-----------------------------------
rsErpErr:
    If rsErp.RecordCount <> 0 Then
        MsgBox "错误代码:" & Err.Number & vbCrLf & "错误描述:" & Err.Description, vbCritical, "连接查询错误"
        Unload frmErp
    End If
    If rsErp.RecordCount = 0 Then
        lblRs.ForeColor = vbRed
        lblRs.Caption = "无记录,请确认"
    End If
End Function