界面设计:
1ComboBox 名为: CboName 用来输入或显示用户名
2TextBox 名为:TxtPassword 用来输入密码
3TextBox 名为:TxtPasswordSure 在注册时用来输入确认密码
4CheckBox 名为:ChkNew 用来新注册 Caption属性:新建用户
5CommandButton 名为:CmdLoad 用来登陆或注册 Caption属性:登陆
6CommandButton 名为:CmdExit 用以退出 Caption 属性:退出
7Data 不需要多设置,这个控件不用的,只是第一次加载时起作用Visible属性:False
代码如下:
Dim MyTable As TableDef
Dim MyField As Field
Dim MyDatabase As Database
Dim Myrs As Recordset
Dim StrRule As String
Private Sub CboName_Change()
If Len(CboNameText) > 8 Then
CboNameSelStart = 0
CboNameSelLength = 8
CboNameText = CboNameSelText
CboNameSelStart = 8
End If
End Sub
Private Sub ChkNew_Click()
If ChkNewValue = 0 Then
LblPasswordsureVisible = False
TxtPasswordSureVisible = False
CmdLoadCaption = "登陆"
Else
LblPasswordsureVisible = True
TxtPasswordSureVisible = True
CmdLoadCaption = "注册"
End If
CboNameSetFocus
End Sub
Private Sub CmdExit_Click()
End
End Sub
Private Sub CmdLoad_Click()
If CboNameText = "" Then
MsgBox "请输入用户名!", vbExclamation, "友情提示"
CboNameSetFocus
Exit Sub
ElseIf TxtPasswordText = "" Then
MsgBox "请输入密码!", vbExclamation, "友情提示"
TxtPasswordSetFocus
Exit Sub
ElseIf TxtPasswordSureText = "" And TxtPasswordSureVisible = True Then
MsgBox "请输入确认密码!", vbExclamation, "友情提示"
TxtPasswordSureSetFocus
Exit Sub
End If
If CmdLoadCaption = "注册" Then
If TxtPasswordSureText <> TxtPasswordText Then
MsgBox "密码和确认密码不同!", vbExclamation, "Sorry"
TxtPasswordSureSetFocus
Exit Sub
End If
If CheckStr(CboNameText) Then
MsgBox "用户名可以是中文,也可以是英文或数字,但其中不能带有" & vbNewLine & "像“" & StrRule & "”的字符!", vbExclamation, "友情提示"
CboNameSetFocus
Exit Sub
ElseIf CboNameText = "user" Then
MsgBox "user是保留用字,不能用作用户名!", vbExclamation, "Sorry"
Exit Sub
End If
Set MyDatabase = Workspaces(0)OpenDatabase(AppPath & "\save")
Set Myrs = MyDatabaseOpenRecordset("select from user where Name = " & Chr(34) & CboNameText & Chr(34))
If MyrsRecordCount > 0 Then
MsgBox "用户已经存在", vbExclamation, "友情提示"
CboNameSetFocus
Exit Sub
End If
Set Myrs = MyDatabaseOpenRecordset("select from user")
MyrsAddNew
MyrsFields("name") = CboNameText
MyrsFields("password") = TxtPasswordText
MyrsFields("logontime") = Now
MyrsFields("Best") = "0"
MyrsFields("Run") = "0"
MyrsUpdate
MyrsClose
Set MyTable = MyDatabaseCreateTableDef(CboNameText)
Set MyField = MyTableCreateField("Result", 10, 4)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("RightRatio", 10, 6)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("TestTime", 10, 19)
MyTableFieldsAppend MyField
MyDatabaseTableDefsAppend MyTable
MsgBox "注册成功!", vbExclamation, "恭喜你"
CboNameAddItem (CboNameText)
ChkNewValue = 0
MyDatabaseClose
Else '登陆
If TxtPasswordText <> TxtPasswordSureText And TxtPasswordSureVisible = True Then
MsgBox "密码与确认密码不同,请重新输入!", vbExclamation, "友情提示"
TxtPasswordSetFocus
Exit Sub
End If
Set MyDatabase = Workspaces(0)OpenDatabase(AppPath & "\save")
Set Myrs = MyDatabaseOpenRecordset("select from user where name =" & Chr(34) & CboNameText & Chr(34))
If MyrsRecordCount = 0 Then
MsgBox "用户不存在!", vbExclamation, "友情提示"
Exit Sub
End If
MyrsMoveFirst
If TxtPasswordText = MyrsFields("password") Then '登陆成功
MyrsClose
Set Myrs = MyDatabaseOpenRecordset("select from user")
If MyrsBOF = False Then MyrsMoveLast
If MyrsBOF = False Then MyrsMoveFirst
MyrsMoveFirst
For i = 0 To MyrsRecordCount - 1
MyrsEdit
If MyrsFields("Name") = CboNameText Then
MyrsFields("Run") = "1"
Else
MyrsFields("Run") = "0"
End If
MyrsUpdate
MyrsMoveNext
Next
'登陆成功,你加入要执行的 *** 作
Unload Me '登陆窗口卸载
Else
Static WrongTime As Integer
WrongTime = WrongTime + 1
If WrongTime = 2 Then End
MsgBox "密码错误!", vbExclamation, "友情提示"
End If
MyrsClose
MyDatabaseClose
End If
End Sub
Private Sub Form_Activate()
StrRule = "`~!@#$%^&()_-+=|\;:<,>/" & Chr(34) & Chr(39)
If Dir(AppPath & "\savemdb", vbHidden) = "" Then '数据文件不见了
Set MyDatabase = CreateDatabase(AppPath & "\save", dbLangGeneral)
Set MyTable = MyDatabaseCreateTableDef("user")
Set MyField = MyTableCreateField("Name", 10, 16)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("Password", 10, 6)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("LogonTime", 10, 19)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("Best", 10, 4)
MyTableFieldsAppend MyField
Set MyField = MyTableCreateField("Run", 10, 1)
MyTableFieldsAppend MyField
MyDatabaseTableDefsAppend MyTable
ChkNewValue = 2
Else
Set MyDatabase = Workspaces(0)OpenDatabase(AppPath & "\save")
Set Myrs = MyDatabaseOpenRecordset("select from user")
If MyrsEOF = False Then MyrsMoveLast
If MyrsBOF = False Then MyrsMoveFirst
Dim i As Integer
Dim ShowIndex As Integer
For i = 0 To MyrsRecordCount - 1
CboNameAddItem (MyrsFields("Name"))
If MyrsFields("Run") = "1" Then ShowIndex = i
MyrsMoveNext
Next
If CboNameListCount > 0 Then
CboNameListIndex = ShowIndex
Else
ChkNewValue = 2
End If
MyrsClose
MyDatabaseClose
End If
TxtPasswordSetFocus
End Sub
Private Function CheckStr(StrTheword As String) As Boolean
Dim i As Integer
For i = 1 To Len(StrRule)
If InStr(1, StrTheword, Mid(StrRule, i, 1)) <> 0 Then '含有这样的字符
CheckStr = True
Exit Function
End If
Next
CheckStr = False
End Function
'你要源文件可以发EMail给我索要
'我的电邮地址是:wangaochao@163com
在登陆和退出登陆边上,新增
按钮
注册,在注册按钮单击时,d出注册
窗口
,注册窗口应该有
用户名
、
密码
输入、密码确认和
其他信息
的输入
文本框
和提交按钮,输入信息后,单击提交按钮,应该首先查询数据库中是否有该用户名,如没有,新增记录并提示注册成功,然后关闭窗口返回
登录界面
,如已有该用户名,提示用户名已存在,终止注册。
其他注意事项:判断
密码
和
确认密码
输入的内容是否一致,也可以简化为直接使用一个明文的文本框,直接输入密码而不隐藏。
数据库中密码字段用text型,然后保存密码的时候 用一个MD5函数加密放入数据库。
输入密码用MD5加密得到加密字符串,与从数据库中取出的密码字符串比对。
这种方式是对密码大小写敏感的。
以上就是关于vb登陆,连接数据库全部的内容,包括:vb登陆,连接数据库、vb登陆界面、vb做登陆界面,用数据库查询,怎么区分大小写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)