📄 frmaddbook.frm
字号:
VERSION 5.00
Begin VB.Form frmAddBook
Caption = "图书添加"
ClientHeight = 3510
ClientLeft = 60
ClientTop = 345
ClientWidth = 7245
LinkTopic = "Form1"
ScaleHeight = 3510
ScaleWidth = 7245
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command2
Caption = "取消(&C)"
Height = 495
Left = 3840
TabIndex = 13
Top = 2880
Width = 1575
End
Begin VB.CommandButton Command1
Caption = "添加(&A)"
Height = 495
Left = 1800
TabIndex = 12
Top = 2880
Width = 1575
End
Begin VB.Frame Frame1
Caption = "新增图书信息"
Height = 2415
Left = 360
TabIndex = 0
Top = 240
Width = 6495
Begin VB.TextBox txtAuthor
Height = 375
Left = 960
TabIndex = 16
Text = "Text1"
Top = 1800
Width = 5055
End
Begin VB.TextBox txtId
Height = 375
Left = 960
TabIndex = 1
Text = "Text5"
Top = 360
Width = 1455
End
Begin VB.ComboBox cmbType
Height = 315
Left = 3600
TabIndex = 4
Text = "Combo1"
Top = 360
Width = 2415
End
Begin VB.TextBox txtISBN
Height = 375
Left = 3600
TabIndex = 6
Text = "Text4"
Top = 1320
Width = 2415
End
Begin VB.TextBox txtPub
Height = 375
Left = 3600
TabIndex = 5
Text = "Text3"
Top = 840
Width = 2415
End
Begin VB.TextBox txtPrice
Height = 375
Left = 960
TabIndex = 3
Text = "Text2"
Top = 1320
Width = 1455
End
Begin VB.TextBox txtName
Height = 375
Left = 960
TabIndex = 2
Text = "Text1"
Top = 840
Width = 1455
End
Begin VB.Label Label7
Caption = "作 者:"
Height = 255
Left = 240
TabIndex = 15
Top = 1920
Width = 735
End
Begin VB.Label Label6
Caption = "编 号:"
Height = 375
Left = 240
TabIndex = 14
Top = 480
Width = 855
End
Begin VB.Label Label5
Caption = "图书类别:"
Height = 375
Left = 2760
TabIndex = 11
Top = 480
Width = 975
End
Begin VB.Label Label4
Caption = " ISBN:"
Height = 255
Left = 2880
TabIndex = 10
Top = 1440
Width = 735
End
Begin VB.Label Label3
Caption = "价 格:"
Height = 255
Left = 240
TabIndex = 9
Top = 1440
Width = 735
End
Begin VB.Label Label2
Caption = "出版社:"
Height = 375
Left = 2880
TabIndex = 8
Top = 960
Width = 855
End
Begin VB.Label Label1
Caption = "书 名:"
Height = 255
Left = 240
TabIndex = 7
Top = 960
Width = 735
End
End
End
Attribute VB_Name = "frmAddBook"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim dbBook As Database
Dim dbType As Database
Dim rstBook As Recordset
Dim rstType As Recordset
Dim dbPath As String
Dim illeagleChar As Boolean
Private Sub Command1_Click()
'首先判断图书编号、书名、类型、价格、出版社、ISBN、图书类别是否为空
'如为空给出提示
If txtId = "" Then
MsgBox "图书ID必须填写完整!", 0 + 48, "信息提示"
txtId.SetFocus
Exit Sub
End If
If txtName = "" Then
MsgBox "书名必须填写完整!", 0 + 48, "信息提示"
txtName.SetFocus
Exit Sub
End If
If cmbType.Text = "" Then
MsgBox "请选择图书类型!", 0 + 48, "信息提示"
cmbType.SetFocus
Exit Sub
End If
If txtPrice = "" Or txtPub = "" Or txtISBN = "" Or txtAuthor = "" Then
MsgBox "请将所有信息补充完整!", 0 + 48, "信息提示"
Exit Sub
End If
rstBook.Seek "=", Trim(txtId.Text)
If rstBook.NoMatch = False Then
MsgBox "图书编号已存在!", 0 + 48, "提示信息"
'把图书编号设置为空,并将光标定位在图书编号,方便重新输入
txtId.SelText = ""
txtId.SetFocus
Exit Sub
End If
If Not verifyNumbers() Then
MsgBox "价格只能为数字!!", 0 + 48, "提示"
'把图书编号设置为空,并将光标定位在图书编号,方便重新输入
txtPrice.SetFocus
txtPrice.SelStart = 0
txtPrice.SelLength = Len(txtPrice.Text)
Exit Sub
End If
'如果图书编号不存在,则添加新的图书信息
rstBook.AddNew
rstBook.Fields("bookid") = Trim(txtId.Text)
rstBook.Fields("bookname") = txtName.Text
rstBook.Fields("booktype") = cmbType.Text
rstBook.Fields("bookprice") = txtPrice.Text
rstBook.Fields("publisher") = txtPub.Text
rstBook.Fields("ISBN") = txtISBN.Text
rstBook.Fields("author") = txtAuthor.Text
rstBook.Update
MsgBox "添加成功!按回车继续", 0 + 48, "成功"
'对表单进行初始化
resetForm
txtId.SetFocus
End Sub
Private Sub Command2_Click()
Unload frmAddBook
End Sub
Private Sub Form_Load()
Dim i As Integer
'使窗体居中显示
Me.Top = (Screen.Height - Me.Height) \ 2
Me.Left = (Screen.Width - Me.Width) \ 2
resetForm
'打开数据库和book表
dbPath = App.Path + "\db\library.mdb"
Set dbBook = Workspaces(0).OpenDatabase(dbPath, False)
Set rstBook = dbBook.OpenRecordset("book", dbOpenTable)
rstBook.Index = "bookid"
'打开数据库和Type表
Set dbType = Workspaces(0).OpenDatabase(dbPath, False)
Set rstType = dbType.OpenRecordset("type", dbOpenTable)
'从数据库中获取所有的图书类型
rstType.MoveLast
rstType.MoveFirst
For i = 1 To rstType.RecordCount
cmbType.AddItem rstType.Fields("type")
rstType.MoveNext
If rstType.EOF Then Exit Sub
Next
End Sub
Public Sub resetForm()
'设置所有输入框为空
txtId.Text = ""
txtName.Text = ""
txtPrice.Text = ""
txtISBN.Text = ""
txtPub.Text = ""
txtAuthor.Text = ""
cmbType.Text = ""
End Sub
Private Function verifyNumbers() As Boolean
Dim tmpStr As String
Dim i As Integer
Dim charAsc As Integer
tmpStr = txtPrice.Text
'利用循环遍历整个字符串
For i = 1 To Len(tmpStr) Step 1
'获取单个字符并求取它的ASCII值
charAsc = Asc(Mid(Trim(tmpStr), i, 1))
'判断价格文本框中的字符是否是数字和小数点
If Not (charAsc <= Asc("9") And charAsc >= Asc("0") Or charAsc = Asc(".")) Then
verifyNumbers = False
Exit For
Else
verifyNumbers = True
End If
Next
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -