英文故事 海洋之歌:验证密码问题,VB的

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 20:39:05
比如用户名是abc
密码是123
输完用户名和密码后回车。记得是“回车”,不是单击确定,如果用户名和密码正确则通过。否则不通过。
代码怎么写呀。晕死。这么简单的问题我都不懂。呵呵。因为我是新学的。

text1 用户名文本框
text2 密码文本框
cmd1 确定键
cmd2 退出键

text1代码:
if keyascii=13 then
text2.setfocus
end if
--------------------------
text2代码
if keyasscii=13 then
cmd1_click
end fi
--------------------------
cmd1代码
if text1.text="abc" and text2.text="123" then
form2.show '如果进入程序为form2的话
unload me
else
msgbox"用户名密码错误",48,"警告"
end if
--------------------------
cmd2代码
end
--------------------------
全部代码如上

if name.text=pass.text then
(退出的代码)
else
(输入错误的代码)
end if

我给你整个完成的实列代码作参考:
VERSION 5.00
Begin VB.Form frmLogin
BorderStyle = 3 'Fixed Dialog
Caption = "登录"
ClientHeight = 1545
ClientLeft = 2835
ClientTop = 3480
ClientWidth = 3750
Icon = "frmLogin.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 912.837
ScaleMode = 0 'User
ScaleWidth = 3521.047
ShowInTaskbar = 0 'False
Begin VB.TextBox txtUserName
Height = 345
Left = 1290
TabIndex = 1
Top = 135
Width = 2325
End
Begin VB.CommandButton cmdOK
Caption = "确定"
Default = -1 'True
Height = 390
Left = 495
TabIndex = 4
Top = 1020
Width = 1140
End
Begin VB.CommandButton cmdCancel
Cancel = -1 'True
Caption = "取消"
Height = 390
Left = 2040
TabIndex = 5
Top = 1020
Width = 1140
End
Begin VB.TextBox txtPassword
Height = 345
IMEMode = 3 'DISABLE
Left = 1290
PasswordChar = "*"
TabIndex = 3
Top = 525
Width = 2325
End
Begin VB.Label lblLabels
Caption = "用户名称(&U):"
Height = 270
Index = 0
Left = 105
TabIndex = 0
Top = 150
Width = 1080
End
Begin VB.Label lblLabels
Caption = "密码(&P):"
Height = 270
Index = 1
Left = 105
TabIndex = 2
Top = 540
Width = 1080
End
End
Attribute VB_Name = "frmLogin"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public LoginSucceeded As Boolean

Private Sub cmdCancel_Click()
Unload Me
End Sub

Private Sub cmdOK_Click()
Dim runYN As Boolean

runYN = getUNP(connstr_str, txtUserName, txtPassword)

'检查正确的密码
If runYN Then
Milk.Show
Unload frmLogin
Else
MsgBox "无效的密码,请重试!", , "登录"
txtPassword.SetFocus
SendKeys "{Home}+{End}"
End If
End Sub

Private Function getUNP(connstr_ As String, user_ As String, passw_ As String) As Boolean

Dim YesNo_ As Boolean
Dim Conns As New ADODB.Connection
Dim Recordsetz As New ADODB.Recordset
Dim querys As String

querys = "select * from 员工管理 where 姓名 = '" & user_ & "' and 密码 = '" & passw_ & "'"

Conns.Open connstr_
Recordsetz.Open querys, Conns, 1, 1

If Recordsetz.RecordCount = 1 Then
YesNo_ = True
ElseIf Recordsetz.RecordCount > 1 Then
YesNo_ = True
MsgBox "提醒用户:系统中有重复的用户名", , App.EXEName
Else
YesNo_ = False
End If

getUNP = YesNo_

Recordsetz.Close
Set Conns = Nothing
Set Recordsetz = Nothing

End Function

Private Sub Form_Load()
'Dim connstr As String
Dim File_1 As File '//文件对象
Dim txt_1 As TextStream '//文件容器
Dim FSO_1 As New FileSystemObject '//文件系统对象

Set File_1 = FSO_1.GetFile(App.Path & "\" & App.EXEName & ".txt")
Set txt_1 = File_1.OpenAsTextStream(ForReading)
connstr_str = txt_1.ReadAll

Set File_1 = Nothing
txt_1.Close
Set txt_1 = Nothing
Set FSO_1 = Nothing
End Sub

Private Sub Form_Load()
Text2.PasswordChar = "*"
Text2.Text = ""
Text1 = ""
End Sub
Private Sub Text1_LostFocus()
If Not IsNumeric(Text1) Then
MsgBox "帐号有非数字字符错误"
Text1.Text = ""
Text1.SetFocus
End If
End Sub
Private Sub Command1_Click()

Dim I As Integer
If Text2.Text <> "Gong" Then
I = MsgBox("密码错误", 5 + vbExclamation, "输入密码")
If I <> 4 Then
End
Else
Text2.Text = ""
Text2.SetFocus
End If
End If
End Sub

你自己研究一下吧。。。。

首先把CommandButton的default属性设置为true然后
Private Sub Command1_Click()
If UserName.Text = "abc" And PassWord.Text = "123" Then
MsgBox "ok"
'你下一步要执行的程序
End If
End Sub