📄 frmquerybook.frm
字号:
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "Msflxgrd.ocx"
Begin VB.Form frmQueryBook
Caption = "图书查询"
ClientHeight = 4785
ClientLeft = 60
ClientTop = 345
ClientWidth = 9255
LinkTopic = "Form1"
ScaleHeight = 4785
ScaleWidth = 9255
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdBack
Caption = "返回(&B)"
Height = 375
Left = 4920
TabIndex = 12
Top = 1800
Width = 1575
End
Begin VB.Frame Frame2
Caption = "查询条件"
Height = 1455
Left = 5880
TabIndex = 8
Top = 120
Width = 3255
Begin VB.TextBox txtKeywords
Height = 375
Left = 960
TabIndex = 9
Top = 720
Width = 1455
End
Begin VB.Label Label1
Caption = "请输入查询条件:"
Height = 255
Left = 480
TabIndex = 10
Top = 360
Width = 1575
End
End
Begin VB.Frame Frame1
Caption = "选择查询方式"
Height = 1455
Left = 120
TabIndex = 2
Top = 120
Width = 5655
Begin VB.OptionButton optQuery
Caption = "按作者查询"
Height = 255
Index = 5
Left = 3120
TabIndex = 11
Top = 1080
Width = 1575
End
Begin VB.OptionButton optQuery
Caption = "按ISBN查询"
Height = 255
Index = 4
Left = 3120
TabIndex = 7
Top = 720
Width = 1575
End
Begin VB.OptionButton optQuery
Caption = "按出版社查询"
Height = 255
Index = 3
Left = 3120
TabIndex = 6
Top = 360
Width = 1575
End
Begin VB.OptionButton optQuery
Caption = "按书名查询"
Height = 255
Index = 2
Left = 360
TabIndex = 5
Top = 1080
Width = 1575
End
Begin VB.OptionButton optQuery
Caption = "查询全部"
Height = 255
Index = 0
Left = 360
TabIndex = 4
Top = 360
Width = 1575
End
Begin VB.OptionButton optQuery
Caption = "按图书编号查询"
Height = 495
Index = 1
Left = 360
TabIndex = 3
Top = 600
Width = 1815
End
End
Begin MSFlexGridLib.MSFlexGrid fgBook
Height = 2055
Left = 120
TabIndex = 1
Top = 2640
Width = 9015
_ExtentX = 15901
_ExtentY = 3625
_Version = 393216
End
Begin VB.CommandButton cmdQuery
Caption = "查询(&Q)"
Height = 375
Left = 2160
TabIndex = 0
Top = 1800
Width = 1575
End
End
Attribute VB_Name = "frmQueryBook"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim sql As String
Private Sub cmdBack_Click()
Unload Me
End Sub
Private Sub cmdQuery_Click()
Dim i As Integer
Dim j As Integer
fgBook.Visible = False
If optQuery(0).Value <> True And txtKeywords.Text = "" Then
frmQueryBook.Height = 2800
MsgBox "查询条件不能为空,请重新输入查询条件!!", vbExclamation, "错误提示"
txtKeywords.SetFocus
Exit Sub
End If
For i = 0 To 5
If (optQuery(i).Value) Then
Select Case i
Case 0
sql = "SELECT * FROM book"
Case 1
sql = "SELECT * FROM book where bookid='" & txtKeywords.Text & "'"
Case 2
sql = "SELECT * FROM book where bookname like '%" & txtKeywords.Text & "%'"
Case 3
sql = "SELECT * FROM book where publisher like '%" & txtKeywords.Text & "%'"
Case 4
sql = "SELECT * FROM book where ISBN='" & txtKeywords.Text & "'"
Case 5
sql = "SELECT * FROM book where author like '%" & txtKeywords.Text & "%'"
End Select
End If
Next i
'设置具体的SQL语句
cmd.CommandText = sql
'执行SQL语句,建立打开记录集
rs.Open cmd, , adOpenKeyset
'先将记录集移到首记录,然后显示记录内容
'rs.MoveLast
If rs.RecordCount = 0 Then
frmQueryBook.Height = 2800
MsgBox "没有查找满足条件的数据!", vbExclamation, "提示"
Else
fgBook.Rows = rs.RecordCount + 1
fgBook.Cols = 8
'Print fgBook.Rows
'设定行高
For i = 0 To fgBook.Rows - 1
fgBook.RowHeight(i) = 280
Next i
'设定列的属性
fgBook.Row = 0
For i = 0 To fgBook.Cols - 1
fgBook.Col = i '指定当前列为第i列
fgBook.FixedAlignment(i) = 4 '每列内容居中显示
Select Case i
Case 0
fgBook.ColWidth(i) = 620 '设定列宽
fgBook.Text = "序号"
Case 1
fgBook.ColWidth(i) = 920 '设定列宽
fgBook.Text = "图书编号"
Case 2
fgBook.ColWidth(i) = 2500 '设定列宽
fgBook.Text = "书名"
Case 3
fgBook.ColWidth(i) = 2000 '设定列宽
fgBook.Text = "作者"
Case 4
fgBook.ColWidth(i) = 1000 '设定列宽
fgBook.Text = "价格"
Case 5
fgBook.ColWidth(i) = 2000 '设定列宽
fgBook.Text = "出版社"
Case 6
fgBook.ColWidth(i) = 1000 '设定列宽
fgBook.Text = "图书类别"
Case 7
fgBook.ColWidth(i) = 1500 '设定列宽
fgBook.Text = "ISBN"
End Select
Next i
'rs.MoveFirst
i = 1
While (Not rs.EOF)
fgBook.Row = i
For j = 0 To fgBook.Cols - 1
fgBook.Col = j '设置当前为列为第j列
fgBook.CellAlignment = 4 '每列内容居中显示
Select Case j
Case 0
fgBook.Text = "" & i
Case 1
fgBook.Text = rs.Fields("bookid")
Case 2
fgBook.Text = rs.Fields("bookname")
Case 3
fgBook.Text = rs.Fields("author")
Case 4
fgBook.Text = rs.Fields("bookprice")
Case 5
fgBook.Text = rs.Fields("publisher")
Case 6
fgBook.Text = rs.Fields("booktype")
Case 7
fgBook.Text = rs.Fields("ISBN")
End Select
Next j
'Print rs.Fields("bookid"), rs.Fields("bookname")
rs.MoveNext
i = i + 1
Wend
fgBook.Visible = True
frmQueryBook.Height = 5190
End If
rs.Close
End Sub
Private Sub Form_Load()
'窗体居中显示
Me.Top = (Screen.Height - Me.Height) \ 2
Me.Left = (Screen.Width - Me.Width) \ 2
'使用Connection对象与具体的数据库文件相连接
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db\library.mdb"
'每个Command必须与一个Connection对象连接
Set cmd.ActiveConnection = conn
'设置命令的类型是使用SQL语句
cmd.CommandType = adCmdText
optQuery(0).Value = True
frmQueryBook.Height = 2800
End Sub
Private Sub Form_Unload(Cancel As Integer)
conn.Close
End Sub
Private Sub optQuery_Click(Index As Integer)
If Index = 0 Then
txtKeywords.Enabled = False
Else
txtKeywords.Enabled = True
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -