我用VB做了个数据库程序,为后台数据库SQL2000中的一个表做了一个类模块,并在程序中声明了类的实例,但在程序运行时却出现错误:无效的对象.请赐教,谢谢.
解决方案 »
- 谷歌还在2009年特别发布了针对初学者的Android Simple语言,该语言类似Basic语言
- print 打印中的定位问题
- VC++语法结构写的动态库在VB中怎么调用
- 如何获取picturebox控件的当前x,y的坐标值
- 请问如何在datagrid中实现"插入--删除--修改"三个功能?急!!!!!
- davidlv (断刀客)大侠请接分:我要那个c/s系统的代码。
- VB用利用窗口句柄传送数据问题。。。
- 请问各位大侠,怎样用VB编写DLL?生成的DLL在DELPHI里任何调用它的函数?!!急!!!
- 请大家看段程序,检查一下,到底哪里错了
- datagrid的另一个小问题!!!
- 求教VBA小程序,有兴趣的高手进来指点下哦
- 运行程序时,老是提示"应用程序发生异常 unknown software exception(0xc0000094),位置为0x76b1594d."!请问各位高手是怎么回事?
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)
居然在每次读取字段时都重新查询数据库这样写好得多: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
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
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
后插入MsgBox "Good!"观察,所有语句均得到执行,
我想是不是语句str = rs.Fields(0)有问题