第二十二课: 创建数据库应用程式- 第四部分

 

在前几个课程里,你学会了使用数据控件和ADO控件来设计数据库应用程序。不过,这些都是很简单的应用。在这一课程里,您将学习如何建立一个比较复杂的数据库应用程式,那就是电子图书馆。这个电子图书馆将可以接受用户的注册以及处理需要使用密码登录命令,从而提高数据库方面的安全。基本上,这个应用程式将构成一个欢迎用户的菜单,一个注册菜单,一个登录的菜单和主数据库菜单。这一序列菜单说明如下:

 


首先,你需要设计的欢迎菜单。您可以效法以下的例子:

在这个表单里,您需要插入三个命令按钮并设定他们的属性如下:

 

Form name main_menu
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 cmdCancel_Click()
End
End Sub

Private Sub cmdLogin_Click()
main_menu.Hide
Login_form.Show
End Sub

Private Sub cmdRegister_Click()
main_menu.Hide
Register.Show
End Sub

如果一个新的用户按一下注册按钮,,登记表格将会出现。其中一个例子的下图所示:

 

此登记表格有两个文本框,三个命令按钮和一个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 UserInfo
 

请注意,该passwordchar的文本框二是设置为*这意味着用户将无法看到输入的密码,他们将看到的只是*符号。
该程式如下:


Private Sub cancel_Click( )
End
End Sub

Private Sub cmdClear_Click( )
txtName.Text = ""
txtpassword.Text = ""

End Sub

Private Sub cmdConfirm_Click()

UserInfo.Recordset.Fields("username") = txtName.Text
UserInfo.Recordset.Fields("password") = txtpassword.Text
UserInfo.Recordset.Update

Register.Hide

Login_form.Show

End Sub


Private Sub Form_Load()
UserInfo.Recordset.AddNew
End Sub

登录菜单如下图所示:

登录菜单上面有两个文本框和一个命令按钮,其属性设置如下:

 

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
Login_form.Hide
frmLibrary.Show
Exit Sub

Else
Register.UserInfo.Recordset.MoveNext
End If

Loop

Msg = MsgBox("Invalid password, try again!", vbOKCancel)
If (Msg = 1) Then
Login_form.Show
txtName.Text = ""
txtpassword = ""

Else
End
End If


End Sub
 

主数据库菜单上的界面如下:

所有控件的属性如以下表中所列:

 

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("Are you sure you want to delete this record?", 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


 

[返回主页]