我用VB做了个数据库程序,为后台数据库SQL2000中的一个表做了一个类模块,并在程序中声明了类的实例,但在程序运行时却出现错误:无效的对象.请赐教,谢谢.

解决方案 »

  1.   

    其中一个模块中的代码如下:
    Private Mycnn As Connection
    Private Myrec As Recordset
    Private Mycom As Command
    Public Sub DBConnection()
    Set Mycnn = New ADODB.Connection
    Mycnn.ConnectionString = CONSTR
    Mycnn.Open
    If Mycnn.State <> adStateOpen Then
    MsgBox "The DB isn't opened!"
    Exit Sub
    Else
    IsConnection = True
    End If
    End Sub
    Public Sub Disconnection()
    If IsConnection = True Then
    IsConnection = False
    End If
    Mycnn.Close
    Set Mycnn = Nothing
    IsConnection = False
    End Sub
    Public Sub MyExecute(ByVal str As String)
    DBConnection
    Set Mycom = New ADODB.Command
    Set Mycom.ActiveConnection = Mycnn
    Mycom.CommandText = Trim(str)
    Mycom.Execute
    Disconnection
    End Sub
    Public Function MyQuery(ByVal str As String) As ADODB.Recordset
    DBConnection
    Dim QueryStr As String
    Set Myrec = New ADODB.Recordset
    Set Myrec.ActiveConnection = Mycnn
    Myrec.CursorType = adOpenStatic
    'QueryStr = Trim(str)
    QueryStr = str
    Myrec.Open QueryStr
    Set MyQuery = Myrec
    End Function
    另一个模块中的代码如下:
    Public: obj as New PasswordInfo
    类模块中代码如下:(类名:PasswordInfo)
    Public Function IsExitUser(ByVal str As String) As Boolean
    Dim SQLStr As String
    Dim count As Integer
    SQLStr = "Select * From PasswordInfo"
    MyQuery (SQLStr)
    count = MyQuery(AQLStr).RecordCount
    Do While count >= 0
    count = count - 1
    If str = MyQuery(SQLStr).Fields(0) Then
    IsExitUser = True
    Disconnection
    Exit Function
    End If
    Loop
    IsExitUser = False
    Disconnection
    End Function
    我在窗体按钮响应函数中调用:
    obj.IsExitUser(str)
      

  2.   

    到底在什么地方出错的同时你那个IsExitUser写的太差了
    居然在每次读取字段时都重新查询数据库这样写好得多:Public Function IsExitUser(ByVal str As String) As Boolean
        Dim SQLStr As String
        dim rs as ADODB.Recordset    SQLStr = "Select * From PasswordInfo"
        set rs = MyQuery(SQLStr)
        Do until rs.Eof
            If str = rs.Fields(0) Then
                IsExitUser = True
                Disconnection
                Exit Function
            End If
            rs.MoveNext '定位到下一条记录
        Loop
        IsExitUser = False
        Disconnection
    End Function
      

  3.   

    多谢指教,我的问题是:用obj.IsExitUser(str)时,显示错误:无效对象.
      

  4.   

    //多谢指教,我的问题是:用obj.IsExitUser(str)时,显示错误:无效对象.你的obj构造了没有?
      

  5.   

    刚刚找到问题所在,我把界面上的ADODC控件删除了就可以了,但不知为什么.
      

  6.   

    但又出现新问题了,我在界面上编缉框中输入了正确的用户名,却总是出现"Please input UserName again"对话框,为什么:
    Private Sub define_Click()
    Dim user As String
    Dim Flag As Boolean
    user = UserName.Text
    Flag = PasswordObj.IsExitUser(user)
    If user = "" Then
    MsgBox "Please input the username!"
    Exit Sub
    End If
    If Flag = False Then
    MsgBox "Please input UserName again!"
    Exit Sub
    End If
    Form1.Show 1
    End Sub
      

  7.   

    我的函数内容如下:
    Public Function IsExitUser(ByVal str As String) As Boolean
    Dim SQLStr As String
    Dim rs As New ADODB.Recordset
    SQLStr = "Select * From PasswordInfo"
    Set rs = MyQuery(SQLStr)
    rs.MoveFirst
    Do Until rs.EOF = True
    If str = rs.Fields(0) Then
    IsExitUser = True
    Disconnection
    Exit Function
    End If
    rs.MoveNext
    Loop
    IsExitUser = False
    Disconnection
    End Function
    Public Function MyQuery(ByVal str As String) As ADODB.Recordset
    DBConnection
    Dim QueryStr As String
    Set Myrec = New ADODB.Recordset
    Set Myrec.ActiveConnection = Mycnn
    Myrec.CursorType = adOpenStatic
    QueryStr = Trim(str)
    Myrec.Open QueryStr
    Set MyQuery = Myrec
    End Function
    Public Sub DBConnection()
    Set Mycnn = New ADODB.Connection
    Mycnn.ConnectionString = CONSTR
    Mycnn.Open
    If Mycnn.State <> adStateOpen Then
    MsgBox "The DB isn't opened!"
    Exit Sub
    Else
    IsConnection = True
    End If
    End Sub
    我在界面上编缉框中输入了正确的用户名(和数据库表PasswordInfo中Fields(0)中的数据一致),却总是出现"Please input UserName again"对话框,为什么:
    Private Sub define_Click()
    Dim user As String
    Dim Flag As Boolean
    user = UserName.Text
    Flag = PasswordObj.IsExitUser(user)
    If user = "" Then
    MsgBox "Please input the username!"
    Exit Sub
    End If
    If Flag = False Then
    MsgBox "Please input UserName again!"
    Exit Sub
    End If
    Form1.Show 1
    End Sub
      

  8.   

    在IsExitUser中下断点、单步跟踪,看有没有正确执行
      

  9.   

    我在Set rs = MyQuery(SQLStr)前后及rs.MoveFirst.Do Until rs.EOF = True
    后插入MsgBox "Good!"观察,所有语句均得到执行,
    我想是不是语句str = rs.Fields(0)有问题