rs.open("select count(*) from table where username='" & user & "' And password='"& pwd &"'"),Conn 直接查找符合用户名和密码的记录的数量,结果大于0就可以了。
我刚好有一段代码,给你参考一下If Trim(Me.Text1.Text) + "" = "" Or Trim(Me.Text2.Text) + "" = "" Then MsgBox "姓名和密码输入不完整", vbInformation, "错误提示" Me.Text1.SetFocus Exit Sub End IfcNstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\accsee\hr"Dim strsql, password As String strsql = "select * from pass where name='" & Trim(Me.Text1.Text) & "'" Dim rsTable As New ADODB.Recordset cNstr.Open rsTable.CursorLocation = adUseClient rsTable.Open strsql, cNstrIf rsTable.RecordCount < 1 Then If Me.Text3.Text = "3" Then MsgBox "第3次输入错误" + Chr(13) + Chr(10) + "程序即将关闭" + Chr(13) + Chr(10) + "请与程序员联系", vbInformation, "提示" Unload Me Else Dim nt As String nt = 3 - Me.Text3.Text MsgBox "第" + Text3.Text + "次输入错误" + Chr(13) + Chr(10) + "您还有" + nt + "次机会", vbInformation, "提示" Text3.Text = Text3.Text + 1 Text1.Text = "" Me.Text1.SetFocus End If cNstr.Close Exit Sub Else If Text2.Text = rsTable.Fields("password") Then Form6.Show Me.Label7.Caption = rsTable.Fields("xm") Form6.Label4.Caption = "当前用户:" + Form1.Label7.Caption Form6.Label5.Caption = Form1.Label6.Caption Unload Me Else If Me.Text3.Text = "3" Then MsgBox "第3次输入错误" + Chr(13) + Chr(10) + "程序即将关闭" + Chr(13) + Chr(10) + "请与程序员联系", vbInformation, "提示" Unload Me Else Dim strInt As String strInt = 3 - Me.Text3.Text MsgBox "第" + Text3.Text + "次输入错误" + Chr(13) + Chr(10) + "您还有" + strInt + "次机会", vbInformation, "提示" Text3.Text = Text3.Text + 1 Text2.Text = "" cNstr.Close End If End If End If
我的数据库里的表里的用户名和密码有4个,不同的用户登录验证不同的用户名和密码。 现在我要把text里的文本跟表里的第二行的用户名和密码 验证,这条语句怎么写? 我写的 这条只能把text里的文本跟表里的第以行的用户名和密码 验证。 If Trim(txtUserName.Text) = Trim(Data1.Recordset.Fields("用户名")) Then LoginSucceeded = True Me.Hide wuye_main.Show
rs.open "select * from table where 用户名= " & txtUserName.Text & " AND 密码= " & txtPass.Text,mConnectstring, adOpenKeyset, adLockOptimistic, adCmdText if rs.eof and rs.bof then msgbox"用户名或密码错误" else Me.Hide wuye_main.Show end if rs.close set rs=nothing
这个问题怎么到处有人问? Form代码:Private Sub cmdOK_Click() Dim Sex As String Dim sqlStr As String sqlStr = "Select * from user where 用户名='" & txtName.Text & "' and 密码='" & txtPwd.Text & "'" rs.Open sqlStr, cn, adOpenKeyset, adLockPessimistic If rs.BOF = True And rs.EOF = True Then '登录不成功 MsgBox "用户名或密码错!为保证系统安全!程序将终止!", vbInformation + vbOKOnly, "系统提示" End Else '登录成功 MsgBox txtName.Text & " 欢迎您的到来!!", vbInformation + vbOKOnly, "管理系统" CurrentUser = txtName.Text Unload Me frmMain.Show End If End Sub Moudle代码:Option Explicit Public cn As New ADODB.Connection Public rs As New ADODB.RecordsetPublic Function ConnectToServer() As Boolean '连接数据库的函数,在sub main过程中被调用 On Error GoTo connecterr '连接到ACCESS cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & App.Path & "\user.MDB" cn.ConnectionTimeout = 30 '连接数据库的超时时限为30秒 cn.Open '打开连接 ConnectToServer = True '已经顺利地打开连接则让函数返回真值 Exit Function '退出函数调用
'以下为错误处理
connecterr: ConnectToServer = False MsgBox "错误代码:" & Err.Number & vbCrLf & _ "错误描述:" & Err.Description, vbCritical + vbOKOnly, "连接数据库过程中发生错误!" End FunctionSub main() '程序启动时,首先检验数据库连接的正确性,如果通过则显示登录窗口 If ConnectToServer = False Then End '出现错误,则结束程序的执行 frmLogin.Show 'show方法被调用时如果窗体还没加载,则会自动加载窗体再显示出来 End Sub
问题解决了,谢谢诸位的指点...!!! Private Sub cmdOK_Click() '检查正确的密码
Data1.Recordset.MoveNext If Trim(txtUserName.Text) = Trim(Data1.Recordset.Fields("用户名")) And Trim(txtPassword.Text) = Trim(Data1.Recordset.Fields("密码")) Then LoginSucceeded = True Me.Hide baoan_main.Show
Else MsgBox "用户名或密码错误,请重试!", , "登录" txtUserName.SetFocus SendKeys "{Home}+{End}" Data1.Recordset.MoveFirst End If End Sub
直接查找符合用户名和密码的记录的数量,结果大于0就可以了。
MsgBox "姓名和密码输入不完整", vbInformation, "错误提示"
Me.Text1.SetFocus
Exit Sub
End IfcNstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\accsee\hr"Dim strsql, password As String
strsql = "select * from pass where name='" & Trim(Me.Text1.Text) & "'"
Dim rsTable As New ADODB.Recordset
cNstr.Open
rsTable.CursorLocation = adUseClient
rsTable.Open strsql, cNstrIf rsTable.RecordCount < 1 Then
If Me.Text3.Text = "3" Then
MsgBox "第3次输入错误" + Chr(13) + Chr(10) + "程序即将关闭" + Chr(13) + Chr(10) + "请与程序员联系", vbInformation, "提示"
Unload Me
Else
Dim nt As String
nt = 3 - Me.Text3.Text
MsgBox "第" + Text3.Text + "次输入错误" + Chr(13) + Chr(10) + "您还有" + nt + "次机会", vbInformation, "提示"
Text3.Text = Text3.Text + 1
Text1.Text = ""
Me.Text1.SetFocus
End If
cNstr.Close
Exit Sub
Else
If Text2.Text = rsTable.Fields("password") Then
Form6.Show
Me.Label7.Caption = rsTable.Fields("xm")
Form6.Label4.Caption = "当前用户:" + Form1.Label7.Caption
Form6.Label5.Caption = Form1.Label6.Caption
Unload Me
Else
If Me.Text3.Text = "3" Then
MsgBox "第3次输入错误" + Chr(13) + Chr(10) + "程序即将关闭" + Chr(13) + Chr(10) + "请与程序员联系", vbInformation, "提示"
Unload Me
Else
Dim strInt As String
strInt = 3 - Me.Text3.Text
MsgBox "第" + Text3.Text + "次输入错误" + Chr(13) + Chr(10) + "您还有" + strInt + "次机会", vbInformation, "提示"
Text3.Text = Text3.Text + 1
Text2.Text = ""
cNstr.Close
End If
End If
End If
现在我要把text里的文本跟表里的第二行的用户名和密码 验证,这条语句怎么写?
我写的 这条只能把text里的文本跟表里的第以行的用户名和密码 验证。
If Trim(txtUserName.Text) = Trim(Data1.Recordset.Fields("用户名")) Then
LoginSucceeded = True
Me.Hide
wuye_main.Show
if rs.eof and rs.bof then
msgbox"用户名或密码错误"
else
Me.Hide
wuye_main.Show
end if
rs.close
set rs=nothing
Form代码:Private Sub cmdOK_Click()
Dim Sex As String
Dim sqlStr As String
sqlStr = "Select * from user where 用户名='" & txtName.Text & "' and 密码='" & txtPwd.Text & "'"
rs.Open sqlStr, cn, adOpenKeyset, adLockPessimistic
If rs.BOF = True And rs.EOF = True Then
'登录不成功
MsgBox "用户名或密码错!为保证系统安全!程序将终止!", vbInformation + vbOKOnly, "系统提示"
End
Else
'登录成功
MsgBox txtName.Text & " 欢迎您的到来!!", vbInformation + vbOKOnly, "管理系统"
CurrentUser = txtName.Text
Unload Me
frmMain.Show
End If
End Sub
Moudle代码:Option Explicit
Public cn As New ADODB.Connection
Public rs As New ADODB.RecordsetPublic Function ConnectToServer() As Boolean '连接数据库的函数,在sub main过程中被调用
On Error GoTo connecterr
'连接到ACCESS
cn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & App.Path & "\user.MDB"
cn.ConnectionTimeout = 30 '连接数据库的超时时限为30秒
cn.Open '打开连接
ConnectToServer = True '已经顺利地打开连接则让函数返回真值
Exit Function '退出函数调用
'以下为错误处理
connecterr:
ConnectToServer = False
MsgBox "错误代码:" & Err.Number & vbCrLf & _
"错误描述:" & Err.Description, vbCritical + vbOKOnly, "连接数据库过程中发生错误!"
End FunctionSub main()
'程序启动时,首先检验数据库连接的正确性,如果通过则显示登录窗口
If ConnectToServer = False Then End '出现错误,则结束程序的执行
frmLogin.Show
'show方法被调用时如果窗体还没加载,则会自动加载窗体再显示出来
End Sub
Private Sub cmdOK_Click()
'检查正确的密码
Data1.Recordset.MoveNext
If Trim(txtUserName.Text) = Trim(Data1.Recordset.Fields("用户名")) And Trim(txtPassword.Text) = Trim(Data1.Recordset.Fields("密码")) Then
LoginSucceeded = True
Me.Hide
baoan_main.Show
Else
MsgBox "用户名或密码错误,请重试!", , "登录"
txtUserName.SetFocus
SendKeys "{Home}+{End}"
Data1.Recordset.MoveFirst
End If
End Sub