![]() |
|
Visual Basic课程实训指导书 一、课程实训的意义: 1) 学会综合运用VB有关的控件、函数和对象的知识。 2) 学习各种数据处理控件的使用方法,掌握数据库应用程序的开发技术。 3) 学会运用VB进行程序设计的特点和方法。 4) 在学会运用VB进行面向对象程序设计步骤和方法的基础上,能够设计其他的实际应用系统,从而达到一定的应用程序设计水平。 二、课程实训的内容: 三、课程实训的要求: 四、课程实训的步骤: 五、课程实训的过程 dbconnection.Open 读者管理:包括读者登记和读者删除。 借书管理:包括借书、还书和借阅查询三项功能。 Cbobooktype.AddItem "哲学" Cbobooktype.AddItem "政治" Cbobooktype.AddItem "军事" Cbobooktype.AddItem "经济" Cbobooktype.AddItem "文学" Cbobooktype.AddItem "历史地理" Cbobooktype.AddItem "数理化" Cbobooktype.AddItem "工业技术" Cbobooktype.AddItem "生物化学" Cbobooktype.AddItem "其他" booksize = "16开" End Sub booksize=optsize(index).Caption End Sub Adodc1.RecordSource = "select * from
book where bookid='" & Trim(Txtbookid.Text) & "'" Adodc1.Refresh If Adodc1.Recordset.EOF = True And
Adodc1.Recordset.BOF = True Then Adodc1.Recordset.AddNew
Adodc1.Recordset("bookid")
= Txtbookid
Adodc1.Recordset("booktype") = Cbobooktype
Adodc1.Recordset("ISBN") = TxtISBN
Adodc1.Recordset("bookname") = Txtbookname
Adodc1.Recordset("publisher") = Txtpublisher
Adodc1.Recordset("author") = Txtauthor
Adodc1.Recordset("size") = booksize
Adodc1.Recordset("price") = CCur(Txtprice)
Adodc1.Recordset("pages") = CInt(Txtpages)
Adodc1.Recordset("stocks") = CInt(Txtstocks)
Adodc1.Recordset("spare") = CInt(Txtstocks) Adodc1.Recordset("abstract")
= Txtabstract
Adodc1.Recordset("location") = Txtlocation
Adodc1.Recordset("pubdate") = CDate(Txtpubdate) Adodc1.Recordset.Update Adodc1.Recordset.Close MsgBox "登记完毕" Txtbookid = "" TxtISBN = "" Txtbookname = "" Txtpublisher =
"" Txtauthor = "" Txtprice = "" Txtpages = "" Txtstocks = "" Txtabstract = "" Txtpubdate = "" Txtlocation = "" Else MsgBox "书号重复!" Txtbookid.SetFocus Adodc1.Recordset.Close End If End Sub 逻辑设计: Set dbrec.ActiveConnection = dbconnection End Sub Set dbrec = Nothing End Sub If KeyAscii = 13 And Trim(Txtbookid)
<> "" Then Call getbookdetail End If End Sub If CInt(Txtstocks) = 0 Then dbrec.Delete dbrec.Update Else dbrec("spare") =
dbrec("spare") + CInt(Txtstocks) - dbrec("stocks") dbrec("stocks")
= CInt(Txtnewstocks) dbrec.Update dbrec.MoveLast End If MsgBox "已完成!" Unload Me End Sub dbrec.Source = "select * from book
where bookid= '" & Trim(Txtbookid.Text) & "'" dbrec.CursorType = adOpenDynamic dbrec.LockType = adLockOptimistic dbrec.Open If dbrec.EOF = True And dbrec.BOF = True
Then MsgBox "书号不存在!" Txtbookid.SelStart = 0 Txtbookid.SelLength =
Len(Txtbookid) dbrec.Close Else Cbobooktype.AddItem
dbrec.Fields("booktype") Txtisbn =
dbrec.Fields("isbn") Txtbookname =
dbrec.Fields("bookname") Txtpublisher =
dbrec.Fields("publisher") Txtauthor =
dbrec.Fields("author") Select Case
dbrec.Fields("size") Case "32开"
Optsize(0).Value = True
Case "小16开"
Optsize(1).Value = True
Case "16开"
Optsize(2).Value = True End Select Txtprice =
dbrec.Fields("price") Txtpages =
dbrec.Fields("pages") Txtstocks =
dbrec.Fields("stocks") Txtabstract =
dbrec.Fields("abstract") Txtlocation =
dbrec.Fields("location") Txtpubdate =
dbrec.Fields("pubdate") dbrec.Requery Txtbookid.Enabled = False Txtnewstocks.SetFocus End If End Sub
逻辑设计: Dim ssql As String Dim sqlbookid, sqlbookname, sqlauthor,
sqlpublisher If Trim(Txtbookname.Text)
<> "" Then
sqlbookname = "(bookname like '%" & Trim(Txtbookname.Text)
& "%') and" End If If Trim(Txtbookid.Text)
<> "" Then
sqlbookid = "bookid='" & Trim(Txtbookid.Text) &
"' and " End If If Trim(Txtauthor.Text)
<> "" Then
sqlauthor = "(author like '%" & Trim(Txtauthor.Text)
& "%') and " End If If Trim(Txtpublisher.Text)
<> "" Then
sqlpublisher = "(publisher like '%" &
Trim(Txtpublisher.Text) & "%') and " End If ssql = sqlbookid &
sqlbookname & sqlauthor & sqlpublisher Debug.Print ssql
ssql = Left(ssql, Len(ssql) - 5) If ssql <>
"" Then
ssql = "select * from book where " & ssql
Debug.Print ssql Else
ssql = "select * from book" End If
Adodc1.RecordSource = ssql Adodc1.Refresh End Sub Dim dbrec As New ADODB.Recordset Set dbrec.ActiveConnection = dbconnection dbrec.Source = "select * from reader
where readerid= '" & Trim(Txtreaderid.Text) & "'" dbrec.CursorType = adOpenDynamic dbrec.LockType = adLockOptimistic dbrec.Open If dbrec.EOF = True And
dbrec.BOF = True Then
dbrec.AddNew
dbrec.Fields("readerid") = Txtreaderid
dbrec.Fields("readername") = Txtreadername
dbrec.Fields("allownum") = CInt(Txtallownum)
dbrec.Fields("borrownum") = 0
dbrec.Update
dbrec.Close
Set dbrec = Nothing
MsgBox "登记完毕"
Unload Me Else
MsgBox "编号重复,请修改!"
Txtreaderid.SetFocus
dbrec.Close Set dbrec = Nothing End If End Sub Dim dbrec As New ADODB.Recordset Set dbrec.ActiveConnection = dbconnection dbrec.Source = "select * from reader
where readerid= '" & Trim(Txtreaderid.Text) & "'" dbrec.CursorType = adOpenDynamic dbrec.LockType = adLockOptimistic dbrec.Open If dbrec.EOF = True And dbrec.BOF = True
Then dbrec.Close MsgBox "无此读者!" Else If
dbrec.Fields("borrownum") = 0 Then
dbrec.Delete
dbrec.Update MsgBox
"已完成!" Else
MsgBox "该读者还有书未还!" End If End If End Sub
Adodc1.RecordSource =
"select * from reader where readerid='" & Trim(Txtreaderid)
& "'" Adodc1.Refresh If Adodc1.Recordset.EOF =
True And Adodc1.Recordset.BOF = True Then
MsgBox "读者编号输入错误!"
Cancel = True Else
Txtborrownum = Adodc1.Recordset("borrownum") If
Adodc1.Recordset("borrownum") =
Adodc1.Recordset("allownum") Then
MsgBox "读者借书数已达限额!"
Cancel = True
End If End If End Sub Adodc2.RecordSource =
"select * from book where bookid='" & Trim(Txtbookid) &
"'" Adodc2.Refresh If Adodc2.Recordset.EOF =
True And Adodc2.Recordset.BOF = True Then
MsgBox "书号输入错误!"
Cancel = True ElseIf
Adodc2.Recordset("spare") = 0 Then
MsgBox "该书已全部借出!"
Cancel = True End If End Sub If Trim(Txtbookid) =
"" Then
Txtbookid.SetFocus
Exit Sub End If '修改读者的借书
Adodc1.Recordset("borrownum") =
Adodc1.Recordset("borrownum") + 1 Adodc1.Recordset.Update '修改书籍的可借数
Adodc2.Recordset("spare") =
Adodc2.Recordset("spare") - 1 Adodc2.Recordset.Update '增加借书记录 Adodc3.RecordSource =
"select * from borrow where 1=2" Adodc3.Refresh Adodc3.Recordset.AddNew
Adodc3.Recordset("readerid") = Txtreaderid
Adodc3.Recordset("bookid") = Txtbookid
Adodc3.Recordset("borrowdate") = Date Adodc3.Recordset.Update MsgBox "借书操作完成 !" Txtreaderid = "" Txtbookid = "" Txtborrownum =
"" Txtreaderid.SetFocus End Sub 主要控件的属性设置:
Dim i As Integer If KeyAscii = 13 And
Trim(Txtreaderid) <> "" Then
Adodc3.RecordSource = "select * from borrow where
readerid='" & Trim(Txtreaderid) & "'"
Adodc3.Refresh
List1.Clear If
Adodc3.Recordset.EOF = True And Adodc3.Recordset.BOF = True Then
MsgBox "没有借书记录!"
Else
For i = 0 To Adodc3.Recordset.RecordCount - 1
Adodc2.RecordSource = "select * from book where bookid='"
& Adodc3.Recordset("bookid") & "'"
Adodc2.Refresh
List1.AddItem Adodc3.Recordset("bookid") & Space(5)
& Adodc2.Recordset("bookname")
Adodc3.Recordset.MoveNext
Next
End If End If End Sub Dim i As Integer '修改读者表的借书数 Adodc1.RecordSource =
"select * from reader where readerid='" & Trim(Txtreaderid)
& "'" Adodc1.Refresh
Adodc1.Recordset("borrownum") =
Adodc1.Recordset("borrownum") - List1.SelCount Adodc1.Recordset.Update '循环取出列表中选中的项目进行处理 Adodc3.Recordset.MoveLast For i = List1.ListCount - 1 To 0 Step -1 If List1.Selected(i) =
True Then
'修改书目表中的可借数
Adodc2.RecordSource = " select * from book where bookid='"
& Adodc3.Recordset("bookid") & "'"
Adodc2.Refresh
Adodc2.Recordset("spare") =
Adodc2.Recordset("spare") + 1
Adodc2.Recordset.Update
'删除借书记录
Adodc3.Recordset.Delete
List1.RemoveItem i End If Adodc3.Recordset.MovePrevious Next End Sub ③借阅查询:
逻辑设计: Dim ssql As String Dim sqlbookid As String, sqlreaderid As String,
sqldate As String
If Trim(Txtbookid.Text) <> "" Then
sqlbookid = "b.bookid='" & Trim(Txtbookid.Text) &
"' and "
End If
If Trim(Txtreaderid.Text) <> "" Then
sqlreaderid = "b.readerid='" & Trim(Txtreaderid.Text)
& "' and "
End If
If Trim(Txtborrowdate.Text) <> "" Then
sqldate = "b.borrowdate=#" & Trim(Txtborrowdate.Text)
& "# and "
End If
ssql = sqlbookid & sqlreaderid & sqldate
If ssql <> "" Then
ssql = Left(ssql, Len(ssql) - 5)
ssql = "select *from borrow b,book where book.bookid=b.bookid and
" & ssql
Else
ssql = "select *from borrow,book where book.bookid=b.bookid"
End If
Adodc1.RecordSource = ssql Adodc1.Refresh End Sub Frmaddbook.Show 1 End Sub Private Sub mnudelbook_Click() Frmdelbook.Show 1 End Sub Private Sub mnusearchbook_Click() ‘书目查询 End Sub Private Sub mnureaderadd_Click() |