主题:[讨论]本人新手,照书写了个系统,连数据库出现问题,求高手解决!!不甚感激
登录窗口代码:
Public intJobId As Integer
Public intUserId As Integer
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOk_Click()
Dim sql As String
Dim rs As ADODB.Recordset
Dim strUserName As String
Dim intUserKey As Integer
On Error GoTo ErrHandler:
If Trim(txtUserName.Text = "") Then
MsgBox "输入用户名称!", vbExclamation
txtUserName.SetFocus
Else
sql = "select * from Users where UserName='" & txtUserName.Text & " '"
Set rs = ExeSQL(sql)
If rs.EOF = True Then
MsgBox "没有此用户!", vbExclamation
txtUserName.SetFocus
Else
If Trim(rs!UserPassword) = Trim(txtUserKey.Text) Then
intUserId = rs!UserID
intJobId = rs!JobId
rs.Close
Me.Hide
frmMain.Show
Unload Me
Else
MsgBox "密码不正确!", vbExclamation
txtUserKey.SetFocus
txtUserKey.Text = ""
End If
End If
End If
Exit Sub
ErrHandler:
MsgBox "错误号:" & Err.Number & "错误描述:" & Err.Description
End Sub
定义了一个访问数据库的模块,代码:
Public Function ExeSQL(ByVal sql As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnection As String
Dim strArray() As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo ErrHandler:
strConnection = "Provider=SQLOLEDB.1;Intergrated Security=SSPI;Persist Security Info=False;Initial Catalog=pos;Data Source=WWW-0AE1AB8E4A1"
strArray = Split(sql)
cn.Open strConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), cn, adOpenKeyset, adLockOptimistic
Set ExeSQL = rs
Else
cn.Execute sql
End If
ExeSQL_Exit:
Set rs = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
MsgBox "错误号:" & Err.Number & "错误信息:" & Err.Description, vbExclamation
End Function
Public Sub ChangeFormPos(frmMain As MDIForm, frm As Form)
Dim Top As Integer
Top = (frmMain.ScaleHeight - frm.Height) / 2 - 600
If Top > 0 Then
frm.Top = Top
Else
frm.Top = 0
End If
frm.Left = (frmMain.ScaleWidth - frm.Width) / 2
End Sub
Sub main()
frmLogin.Show
End Sub
编译时出现错误号:-2147467259:无效的授权声明
错误号:91 错误描述:对象变量或with变量未设置
将登录窗口的Dim rs As ADODB.Recordset
改为Dim rs As New ADODB.Recordset
又出现错误号:-2147467259:无效的授权声明
对象关闭时不允许操作!
什么原因???
调式时运行到cn.Open strConnection后就直接提示出错了!!!
Public intJobId As Integer
Public intUserId As Integer
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdOk_Click()
Dim sql As String
Dim rs As ADODB.Recordset
Dim strUserName As String
Dim intUserKey As Integer
On Error GoTo ErrHandler:
If Trim(txtUserName.Text = "") Then
MsgBox "输入用户名称!", vbExclamation
txtUserName.SetFocus
Else
sql = "select * from Users where UserName='" & txtUserName.Text & " '"
Set rs = ExeSQL(sql)
If rs.EOF = True Then
MsgBox "没有此用户!", vbExclamation
txtUserName.SetFocus
Else
If Trim(rs!UserPassword) = Trim(txtUserKey.Text) Then
intUserId = rs!UserID
intJobId = rs!JobId
rs.Close
Me.Hide
frmMain.Show
Unload Me
Else
MsgBox "密码不正确!", vbExclamation
txtUserKey.SetFocus
txtUserKey.Text = ""
End If
End If
End If
Exit Sub
ErrHandler:
MsgBox "错误号:" & Err.Number & "错误描述:" & Err.Description
End Sub
定义了一个访问数据库的模块,代码:
Public Function ExeSQL(ByVal sql As String) As ADODB.Recordset
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConnection As String
Dim strArray() As String
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
On Error GoTo ErrHandler:
strConnection = "Provider=SQLOLEDB.1;Intergrated Security=SSPI;Persist Security Info=False;Initial Catalog=pos;Data Source=WWW-0AE1AB8E4A1"
strArray = Split(sql)
cn.Open strConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), cn, adOpenKeyset, adLockOptimistic
Set ExeSQL = rs
Else
cn.Execute sql
End If
ExeSQL_Exit:
Set rs = Nothing
Set cn = Nothing
Exit Function
ErrHandler:
MsgBox "错误号:" & Err.Number & "错误信息:" & Err.Description, vbExclamation
End Function
Public Sub ChangeFormPos(frmMain As MDIForm, frm As Form)
Dim Top As Integer
Top = (frmMain.ScaleHeight - frm.Height) / 2 - 600
If Top > 0 Then
frm.Top = Top
Else
frm.Top = 0
End If
frm.Left = (frmMain.ScaleWidth - frm.Width) / 2
End Sub
Sub main()
frmLogin.Show
End Sub
编译时出现错误号:-2147467259:无效的授权声明
错误号:91 错误描述:对象变量或with变量未设置
将登录窗口的Dim rs As ADODB.Recordset
改为Dim rs As New ADODB.Recordset
又出现错误号:-2147467259:无效的授权声明
对象关闭时不允许操作!
什么原因???
调式时运行到cn.Open strConnection后就直接提示出错了!!!