📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "查询数据记录"
ClientHeight = 3360
ClientLeft = 60
ClientTop = 345
ClientWidth = 4725
LinkTopic = "Form1"
ScaleHeight = 3360
ScaleWidth = 4725
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "查找记录"
Height = 615
Left = 720
TabIndex = 0
Top = 2520
Width = 3375
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
MyFindFirst
End Sub
Sub MyFindFirst()
Dim dbsNorthwind As Database
Dim rstProduct As Recordset
Dim strCategory As String
Dim varBookmark As Variant
Dim Category As String
Dim strMessage As String
Dim intCommand As Integer
'打开数据库
'根据实际设置数据库位置
Set dbsNorthwind = DBEngine.OpenDatabase(App.Path + "\data.mdb")
Set rstProduct = dbsNorthwind.OpenRecordset("SELECT Quantity,Product, Category, PerPrice " & _
"FROM 产品表 ORDER BY ProductID ", dbOpenSnapshot)
Do While True
'通过用户的选择来建立查询方法
strCategory = Trim(InputBox("输入类别进行查询. "))
If strCategory = "" Then Exit Do
Category = "Category='" & strCategory & "'"
With rstProduct
'移动Recordset指针
.MoveLast
'选择第一个符合查询条件的记录
.FindFirst Category
'如果没有符合条件的记录则提示
If .NoMatch Then
MsgBox "No records found with " & Category & "."
Exit Do
End If
Do While True
'在当前记录处设置书签.
varBookmark = .Bookmark
'让用户选择查询方法
strMessage = "Quantity: " & !Quantity & vbCr & "PerPrice: " & !PerPrice & ", " & _
!Category & vbCr & vbCr & Category & vbCr & vbCr & _
" [1 - FindFirst, 2 - FindLast, " & vbCr & "3 - FindNext, " & _
"4 - FindPrevious] "
intCommand = Val(Left(InputBox(strMessage), 1))
If intCommand < 1 Or intCommand > 4 Then Exit Do
'使用所选择的方法查询,如失败返回最后一条当前的记录
If FindAny(intCommand, rstProduct, Category) = False Then
.Bookmark = varBookmark
MsgBox "No match--returning to " & _
"current record. "
Else
MsgBox "找到一条记录", vbInformation, "用DAO操作数据库!"
End If
Loop
End With
Exit Do
Loop
rstProduct.Close
dbsNorthwind.Close
End Sub
Function FindAny(intChoice As Integer, rstTemp As Recordset, strFind As String) As Boolean
Select Case intChoice
Case 1
rstTemp.FindFirst strFind
Case 2
rstTemp.FindLast strFind
Case 3
rstTemp.FindNext strFind
Case 4
rstTemp.FindPrevious strFind
End Select
'根据NoMatch(意味着未找到)属性返回值
FindAny = IIf(rstTemp.NoMatch, False, True)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -