Private Sub Command1_Click()
If x = name And y = passa Then
Me.Hide
main.Show 'main为主窗口名称
End Sub
Private Sub Command2_Click()
End
End SubPrivate Sub Form_Load()
Dim name As String, passa As String
Dim mess1 As Integer, mess2 As Integername = UCase(Trim(Text1.Text))
passa = UCase(Trim(Text2.Text))
select user.ID from user as x where ID=name 'user为数据库中的表名
select user.pass from user as y where pass=passaIf x = name Then
If passa <> y Then
mess1 = msg("密码不正确,请重新输入!", vbOKCancel, "注意")
If mess1 = 1 Then Text2.SetFocus
Else: End
End If
Else: Command1.SetFocus
End If
Else
mess2 = msg("该用户尚未注册!", vbOKCancel, "提示")
If mess2 = 1 Then Text1.SetFocus
Else: End
End If
End If
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) '当按下回车键时就触发的是不是这个事件 还有KeyAscii应该是多少?Text2.SetFocusEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)Command1.SetFocusEnd Sub
If x = name And y = passa Then
Me.Hide
main.Show 'main为主窗口名称
End Sub
Private Sub Command2_Click()
End
End SubPrivate Sub Form_Load()
Dim name As String, passa As String
Dim mess1 As Integer, mess2 As Integername = UCase(Trim(Text1.Text))
passa = UCase(Trim(Text2.Text))
select user.ID from user as x where ID=name 'user为数据库中的表名
select user.pass from user as y where pass=passaIf x = name Then
If passa <> y Then
mess1 = msg("密码不正确,请重新输入!", vbOKCancel, "注意")
If mess1 = 1 Then Text2.SetFocus
Else: End
End If
Else: Command1.SetFocus
End If
Else
mess2 = msg("该用户尚未注册!", vbOKCancel, "提示")
If mess2 = 1 Then Text1.SetFocus
Else: End
End If
End If
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) '当按下回车键时就触发的是不是这个事件 还有KeyAscii应该是多少?Text2.SetFocusEnd SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)Command1.SetFocusEnd Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Text2.SetFocus
End If
End Sub
-----------------------
keypress是在控件有按键事件时就发生的,并不只是按回车键!
所以,要对用户按下回车键的这个动作做出判断的话,就要对用户的按键进行处理。。这个是必要的。正确的是这样的:-------------------------------------------------------------------
Private Sub Text1_KeyPress(KeyAscii As Integer)
if keyascii=13 then
Text2.SetFocus
end if
End Sub
-------------------------------------------------------------------
条件判断必不可少。否则在用户按下任意键后,text2就得到焦点了。
Private Sub Command1_Click() '登陆
On Error GoTo Err
If Text1.Text = "" Then
MsgBox "请输入操作员!", vbInformation, "提示"
Exit Sub
End If
Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
'连接C:\test.mdb数据库
'cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"
'连接SQL
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=登录名;Password=密码;Initial Catalog=数据库;Data Source=Sql服务器别名"
cn.Open
rs.CursorLocation = adUseClient
rs.Open "select * from user where ID='" & name & "'", cn, 3, 2
If rs.EOF Then
MsgBox "该用户尚未注册!", vbOKCancel, "提示"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
Else
If passa <> Trim(rs!pass) Then
MsgBox "密码不正确,请重输!!!", vbQuestion, "提示"
Text2.Text = ""
Text2.SetFocus
Exit Sub
End If
Me.Hide
main.Show 'main为主窗口名称
Exit Sub
Err:
MsgBox Err.Description
End Sub
Private Sub Command1_Click() '登陆
On Error GoTo Err
If Text1.Text = "" Then
MsgBox "请输入操作员!", vbInformation, "提示"
Exit Sub
End If
Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
'连接C:\test.mdb数据库
'cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb;Persist Security Info=False"
'连接SQL
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=登录名;Password=密码;Initial Catalog=数据库;Data Source=Sql服务器别名"
cn.Open
rs.CursorLocation = adUseClient
rs.Open "select * from user where ID='" & name & "'", cn, 3, 2
Dim name As String, passa As String
name = UCase(Trim(Text1.Text))
passa = UCase(Trim(Text2.Text))
If rs.EOF Then
MsgBox "该用户尚未注册!", vbOKCancel, "提示"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
Else
If passa <> Trim(rs!pass) Then
MsgBox "密码不正确,请重输!!!", vbQuestion, "提示"
Text2.Text = ""
Text2.SetFocus
Exit Sub
End If
Me.Hide
main.Show 'main为主窗口名称
Exit Sub
Err:
MsgBox Err.Description
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Text2.SetFocus
End If
End Sub'在Text2中按回车,实现登陆:
Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Call Command1_Click
End If
End Sub
谢谢dong127(冬雪)!!