摘要:在VB6.0環(huán)境下,通過連接Access2000數(shù)據(jù)庫,建立了一個學(xué)生管理系統(tǒng)。在這個系統(tǒng)里,可以實(shí)現(xiàn)學(xué)生學(xué)號,姓名,年齡,聯(lián)系方式,照片等各種信息的錄入,精確查找,模糊查找,更新,刪除等操作。從程序的界面設(shè)計到源代碼,都做了詳盡的說明,適合初學(xué)者快速掌握信息管理系統(tǒng)開發(fā)的步驟。
關(guān)鍵詞:VB;數(shù)據(jù)庫;管理系統(tǒng);access;查詢
中圖分類號:TP39 文獻(xiàn)標(biāo)識碼:A文章編號:1009-3044(2008)35-2374-04
Designing of Student Information Management System by VB
HUANG Min
(Xuzhou Normal Higher School, Xuzhou 221116, China)
Abstract: It is designed for the establishment of student information managementsystem in vb6.0 by connecting Access2000 database. This system can realize Information Processing including data entry, query, update, delete and so on.The Information means the student's number,name,age, contact.From the interface design to the source code, it has shown a detailed description, which suitable for beginners to master the designing of system quickly.
Key words: vb; database; system of management; access; query
1 引言
基于數(shù)據(jù)庫的管理系統(tǒng),可以完成數(shù)據(jù)的輸入,修改,刪除,查詢等。數(shù)據(jù)包含文本,數(shù)值,二進(jìn)制等。很多人都覺得VB簡單,但用VB開發(fā)一個項目,又無從下手,其實(shí)可以從最簡單的系統(tǒng)做起,然后不斷去完善這個系統(tǒng),都是一個很好的學(xué)習(xí)過程,并且能夠觸類旁通,完成其它系統(tǒng)的開發(fā)與設(shè)計。
2 功能需求分析
學(xué)生管理系統(tǒng)采用Visual Basic6.0作為開發(fā)工具,后臺數(shù)據(jù)庫采用Assecc2000。系統(tǒng)開發(fā)的總體任務(wù)是實(shí)現(xiàn)學(xué)生信息管理的條理化,系統(tǒng)化。用戶通過界面,完成對學(xué)生學(xué)號、姓名,年齡,照片、通訊地址,電話等信息的管理。可以對學(xué)生信息方便的實(shí)現(xiàn)輸入,刪除,修改,模糊查詢,精確查詢,在瀏覽記錄時可以切換上一條,下一條,第一條和最后一條。
3 數(shù)據(jù)庫建立與連接
3.1 數(shù)據(jù)庫建立
針對學(xué)生信息管理系統(tǒng)的需求和目標(biāo),在Assecc2000中建立一個學(xué)生信息記錄表,用來存放學(xué)生信息。字段名字和類型表1所示。并將學(xué)生的學(xué)號字段作為主鍵。
3.2 數(shù)據(jù)庫連接
VB提供了多種與數(shù)據(jù)庫連接的方式,這里使用數(shù)據(jù)控件(Data)。數(shù)據(jù)控件(Data)提供了一種方便地訪問數(shù)據(jù)庫中數(shù)據(jù)的方法,使用數(shù)據(jù)控件無須編寫代碼就可以對VB所支持的各種類型的數(shù)據(jù)庫執(zhí)行大部分?jǐn)?shù)據(jù)訪問操作。
數(shù)據(jù)控件本身不能顯示和直接修改記錄,但是可以通過與數(shù)據(jù)控件相關(guān)聯(lián)的數(shù)據(jù)約束控件中顯示各個記錄。
4 用戶界面設(shè)計
在Visual Basic6.0環(huán)境中,建立一個標(biāo)準(zhǔn)的exe工程。學(xué)生的學(xué)號,姓名,年齡,通訊地址,郵政編碼,電話號碼,電子郵件,qq號碼用文本框控件來顯示,名稱分別為text1,text2…text8, 他們分別與數(shù)據(jù)庫中的相應(yīng)字段綁定。用圖片框控件Picture1來顯示學(xué)生的照片,它與數(shù)據(jù)庫中的照片字段綁定。選擇數(shù)據(jù)控件data1,并設(shè)置它的屬性,使得它與數(shù)據(jù)庫連接。實(shí)現(xiàn)記錄的第一條,上一條,下一條,最后一條的切換以及添加記錄,刪除記錄和編輯記錄的操作用按鈕來控制實(shí)現(xiàn),名稱分別為command1,command2…command7。對按不同的條件進(jìn)行查詢,這里使用了組合框Combo1和Combo2,分別用來選擇精確查找和模糊查找的條件。學(xué)生信息的界面如圖1所示。
5 代碼
程序的實(shí)現(xiàn)主要使用了data控件的常用方法。對于精確查找和模糊查找中的“查找第一個”與“查找下一個”按鈕的事件中,它們所使用的查找條件是一樣的,所以需要先定義窗體變量。
5.1 定義窗體變量
Dim s1 As String
Dim s2 As String
5.2 照片的輸入
靜態(tài)設(shè)置:通過一個圖片編輯程序?qū)⒄掌b入剪貼板,等待從剪貼板復(fù)制到Picture1控件。
動態(tài)設(shè)置:通過Picture1_DblClick事件來完成剪貼板到Picture1控件的復(fù)制,當(dāng)移動記錄指針時,Picture1控件內(nèi)的照片存入數(shù)據(jù)庫。
Private Sub Picture1_DblClick()
Picture1.Picture = Clipboard.GetData
End Sub
5.3 記錄的跳轉(zhuǎn)
Private Sub Command1_Click()
'移動到第一條
Data1.Recordset.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = True
Command4.Enabled = True
End Sub
Private Sub Command2_Click()
'移動到上一條
Data1.Recordset.MovePrevious
Command3.Enabled = True
Command4.Enabled = True
If Data1.Recordset.BOF = True Then
Data1.Recordset.MoveFirst
Command1.Enabled = False
Command2.Enabled = False
End If
End SubPrivate Sub Command3_Click()
'移動到后一條
Data1.Recordset.MoveNext
Command1.Enabled = True
Command2.Enabled = True
If Data1.Recordset.EOF = True Then
Data1.Recordset.MoveLast
Command3.Enabled = False
Command4.Enabled = False
End If
End Sub
Private Sub Command4_Click()
'移動到最后一條
Data1.Recordset.MoveLast
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = False
Command4.Enabled = False
End Sub
5.4記錄的添加、刪除和編輯
Private Sub Command5_Click()
If Command9.Caption = \"確 定\" Then
'更新記錄集
Data1.UpdateRecord
'移動到最后一條記錄
Data1.Recordset.MoveLast
Command5.Caption = \"添加記錄\"
Else
'添加新的記錄
Data1.Recordset.AddNew
Command5.Caption = \"確 定\"
End If
End Sub
Private Sub Command6_Click()
'刪除記錄
If Data1.Recordset.EOF Then
Else
Data1.Recordset.Delete
Data1.Recordset.MoveLast
End If
End Sub
Private Sub Command7_Click()
'編輯記錄
Data1.Recordset.Edit
Data1.Recordset.Update
End Sub
5.5 記錄的查找
Private Sub Command8_Click()
'精確查找第一個符合條件的記錄,用窗體變量s1來表示查找的條件
s1 = InputBox(\"請輸入要查找的內(nèi)容\")
If s1 = \"\" Then
MsgBox \"請輸入查詢內(nèi)容!\", 48, \"提示\"
Exit Sub
End If
If Combo1.Text = \"姓名\" Then
Data1.Recordset.FindFirst \"姓名=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"學(xué)號\" Then
Data1.Recordset.FindFirst \"學(xué)號=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"年齡\" Then
Data1.Recordset.FindFirst \"年齡\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"電話號碼\" Then
Data1.Recordset.FindFirst \"電話號碼=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"qq號碼\" Then
Data1.Recordset.FindFirst \"qq號碼=\" \"'\" s1 \"'\"
End If
If Data1.Recordset.NoMatch Then
MsgBox \"沒有符合條件的記錄\", 64, \"提示\"
End If
End Sub
Private Sub Command9_Click()
'精確查找下一個符合條件的記錄,由于和“查找第一個”的條件相同,所以同樣使用s1作為查找的條件。
If s1 = \"\" Then
MsgBox \"請輸入查詢內(nèi)容!\", 48, \"提示\"
Exit Sub
End If
If Combo1.Text = \"姓名\" Then
Data1.Recordset.Findnext \"姓名=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"學(xué)號\" Then
Data1.Recordset.Findnext \"學(xué)號=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"年齡\" Then
Data1.Recordset.Findnext \"年齡\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"電話號碼\" Then
Data1.Recordset.Findnext \"電話號碼=\" \"'\" s1 \"'\"
ElseIf Combo1.Text = \"qq號碼\" Then
Data1.Recordset.Findnext \"qq號碼=\" \"'\" s1 \"'\"
End If
If Data1.Recordset.NoMatch Then
MsgBox \"沒有符合條件的記錄\", 64, \"提示\"
End If
End Sub
Private Sub Command10_Click()
'模糊查找第一個符合條件的記錄,用窗體變量s2來表示模糊查找的條件
s2 = InputBox(\"請輸入\")
If s2 = \"\" Then
MsgBox \"請輸入查詢內(nèi)容!\", 48, \"提示\"
Exit Sub
End If
If Combo2.Text = \"姓名\" Then
Data1.Recordset.FindFirst \"姓名 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"學(xué)號\" Then
Data1.Recordset.FindFirst \"學(xué)號 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"年齡\" Then
Data1.Recordset.FindFirst \"年齡 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"電話號碼\" Then
Data1.Recordset.FindFirst \"電話號碼 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"qq號碼\" Then
Data1.Recordset.FindFirst \"qq號碼 like \" \"'*\" s2 \"*'\"
End If
If Data1.Recordset.NoMatch Then
MsgBox \"沒有符合條件的記錄\", 64, \"提示\"
End If
End Sub
Private Sub Command11_Click()
'模糊查找下一個符合條件的記錄,由于和模糊查找中的“查找第一個”的條件相同,所以同樣使用s2作為查找的條件。
If s2 = \"\" Then
MsgBox \"請輸入查詢內(nèi)容!\", 48, \"提示\"
Exit Sub
End If
If Combo2.Text = \"姓名\" Then
Data1.Recordset.Findnext \"姓名 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"學(xué)號\" Then
Data1.Recordset.Findnext \"學(xué)號 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"年齡\" Then
Data1.Recordset.Findnext \"年齡 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"電話號碼\" Then
Data1.Recordset.Findnext \"電話號碼 like \" \"'*\" s2 \"*'\"
ElseIf Combo2.Text = \"qq號碼\" Then
Data1.Recordset.Findnext \"qq號碼 like \" \"'*\" s2 \"*'\"
End If
If Data1.Recordset.NoMatch Then
MsgBox \"沒有符合條件的記錄\", 64, \"提示\"
End If
End Sub
6 結(jié)束語
本系統(tǒng)操作界面直觀,簡潔,使用方便,只要擁有VB基礎(chǔ),就可以完全理解整個系統(tǒng)開發(fā)的思想。通過本項目的實(shí)踐訓(xùn)練,編程初學(xué)者可以輕松愉悅的練習(xí)中,把零散的知識點(diǎn)貫穿起來,并具有系統(tǒng)開發(fā)的經(jīng)驗(yàn)。當(dāng)然,在實(shí)際工
作中,系統(tǒng)的完善和性能的優(yōu)化還需要逐步提高,從而真正的提高信息管理的效率。
參考文獻(xiàn):
[1] 劉恩濤,等.VB6.0編程技巧與實(shí)例分析[M].北京:中國水利水電出版社,1999.
[2] 劉炳文.Visual Basic 6.0程序設(shè)計[M].北京:人民郵電出版社,1999.
[3] 伍俊良.VB6控件應(yīng)用實(shí)例教程[M].北京:北京希望電子出版社,2002.