在前幾課裡,你已經學會了使用數據控件和ADO控件來設計數據庫應用程式。不過,這些都是相對簡單的應用程式。在這一課裡,您將學習如何建立一個比較復雜的數據庫應用程式,那就是電子書庫。這個電子書庫將可以接受用戶的注冊以及使用密碼登錄,從而提高數據庫方面的安全。基本上,這個應用程式包含一個歡迎用戶的菜單及其程序,一個注冊菜單及其程序,一個登錄菜單及其程序和電子書庫及其程序。這一序列程序說明如下:
首先,你需要設計如圖26.1的注冊及登錄菜單。您可以效法以下的例子:
圖26.1
在這個表單裡,您需要插入三個命令按鈕並設定他們的屬性如下:
控件 | 屬性 |
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
cmdCancel_Click()
End
End Sub
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.2 所示:
圖26.2
此登記表格有兩個文本框,三個命令按鈕和一個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.3 所示:
圖26.3
登錄菜單上面有兩個文本框和一個命令按鈕,其屬性設置如下:
控件 | 属性 |
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.4 所示:
图26.4
所有控件的屬性如下表中所列:
控件 | 屬性 |
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