在前几课中,你已经学会了使用数据控件和ADO控件来设计数据库应用程序。不过,这些都是相对简单的应用程序。在这一课中,您将学习如何建立一个比较复杂的数据库应用程序,那就是电子书库� 这个电子书库将可以接受用户的注册以及使用密码登录,从而提高数据库方面的安全。基本上,这个应用程序包含一个欢迎用户的菜单及其程序,一个注册菜单及其程序,一个登录菜单及其程序和电子书库及其程序。这一序列程序如图26.1所示:
首先,你需要设计如�26.1的注册及登录菜单。您可以效法如图26.2所示的例子�
在这个表单中,您需要插入三个命令按钮并设定它们的属性如下:
控件 | 属� |
---|---|
Form name | frmMainMenu |
command button 1 Name | cmdRegister |
command button 1 Caption | Register |
command button 2 Name | cmdLogin |
command button 2 Caption | Login |
command button 3 Name | cmdCancel |
command button 3 Caption | Cancel |
Private Sub cmdLogin_Click()
Me.Hide
frmLogin.Show
End Sub
Private Sub cmdRegister_Click()
Me.Hide
frmRegister.Show
End Sub
* Me.Hide 是把欢迎菜单 frmMainMenu 隐藏起来。我们也可以� frmMainMenu.Hide 来达至同样效果,不过用Me可使我们的程序代码更精简�
如果一个新的用户按一下注册按钮,注册表格将会出现。其中一个例子如�26.3 所示:
此登记表格有两个文本框,三个命令按钮和一个ADO控件。它们的属性设置如下:
控件 | 属� |
---|---|
Form name | Register |
textbox 1 name | txtName |
textbox 2 name | txtPassword |
textbox 2 PasswordChar | * |
command button 1 name | cmdConfirm |
command button 1 Caption | Confirm |
command button 2 name | cmdClear |
command button 2 Caption | Clear |
command button 3 name | cmdCancel |
command button 3 Caption | Cancel |
ADO control name | AdoUserInfo |
请注意,你必须把密码文本框的passwordchar属性设置为 * �这是预防别人看到输入的密码�
在编写注册表格程序代码之前,你必须创建一个收录用户资料的数据库。你可以用MS Access 建立一个简单的用户数据库,这个数据库开始只需要两个字段,即username � password。把这个数据库保存为 userinfo.mdb。这之后你可把AdoUserInfo 连接到这个数据库�
Private Sub cancel_Click( )
End
End Sub
Private Sub cmdClear_Click( )
txtName.Text = ""
txtpassword.Text = ""
End Sub
Private Sub cmdConfirm_Click()
AdoUserInfo.Recordset.AddNew
AdoUserInfo.Recordset.Fields("username") = txtName.Text
AdoUserInfo.Recordset.Fields("password") = txtPassword.Text
AdoUserInfo.Recordset.Update
Me.Hide �
frmLogin.Show
End Sub
登录菜单如图 26.4 所示:
登录菜单上面有两个文本框和一个命令按钮,其属性设置如下:
控件 | 属� |
---|---|
Textbox 1 name | txtName |
Textbox 2 name | txtpassword |
Command button 1 name | cmdLogin |
Command button 1 Caption | Login |
Form name | Login_form |
Private Sub cmdLogin_Click()
Dim usrname As String
Dim psword As String
Dim usernam As String
Dim pssword As String
Dim Msg As String
Register.UserInfo.Refresh
usrname = txtName.Text
psword = txtpassword.Text
Do Until Register.UserInfo.Recordset.EOF
If Register.UserInfo.Recordset.Fields("username").Value = usrname And
Register.UserInfo.Recordset.Fields("password").Value = psword Then
Me.Hide
frmLibrary.Show
Exit Sub
Else
Register.UserInfo.Recordset.MoveNext
End If
Loop
Msg = MsgBox("Invalid password, try again!", vbOKCancel)
If (Msg = 1) Then
Me.Show
txtName.Text = ""
txtpassword = ""
Else
End
End If
End Sub
书库的界面如�26.5 所示:
所有控件的属性如下表中所列:
控件 | 属� |
---|---|
Form name | frmLibrary |
ADO control name | adoLibrary |
ADO visible | False |
TextBox 1 name | txtTitleA |
TextBox 2 name | txtAuthor |
TextBox 3name | txtPublisher |
TextBox 4 name | txtYear |
TextBox 5 name | txtCategory |
Command button 1 name | cmdSave |
Command button 1 caption | &Save |
Command button 2 name | cmdNew |
Command button 2 caption | &New |
Command button 3 name | cmdDelete |
Command button 3 caption | &Delete |
Command button 4 name | cmdCancel |
Command button 4 caption | &Cancel |
Command button 5 name | cmdNext |
Command button 5 caption | N&ext |
Command button 6 name | cmdPrevious |
Command button 6 caption | &Previous |
Command button 7 name | cmdExit |
Command button 7 caption | E&xit |
Private Sub
cmdCancel_Click()
txtTitle.Text = ""
txtAuthor.Text = ""
txtPublisher.Text = ""
txtYear.Text = ""
txtCategory.Text = ""
End Sub
Private Sub cmdDelete_Click()
Confirm = MsgBox("您确定要删除此记�?", vbYesNo,
"Deletion Confirmation")
If Confirm = vbYes Then
adoLibrary.Recordset.Delete
MsgBox "Record Deleted!", , "Message"
Else
MsgBox "Record Not Deleted!", , "Message"
End If
End Sub
Private Sub cmdExit_Click()
End
End Sub
Private Sub cmdNew_Click()
adoLibrary.Recordset.AddNew
End Sub
Private Sub cmdNext_Click()
If Not adoLibrary.Recordset.EOF Then
adoLibrary.Recordset.MoveNext
If adoLibrary.Recordset.EOF Then
adoLibrary.Recordset.MovePrevious
End If
End If
End Sub
Private Sub cmdPrevious_Click()
If Not adoLibrary.Recordset.BOF Then
adoLibrary.Recordset.MovePrevious
If adoLibrary.Recordset.BOF Then
adoLibrary.Recordset.MoveNext
End If
End If
End Sub
Private Sub cmdSave_Click()
adoLibrary.Recordset.Fields("Title").Value =
txtTitle.Text
adoLibrary.Recordset.Fields("Author").Value = txtAuthor.Text
adoLibrary.Recordset.Update
End Sub