给你一个现成的帮助例子,再分析一下你自己的,看看你究竟错再那里
Sub FindFirstX()
Dim dbsNorthwind As Database
Dim rstCustomers As Recordset
Dim strCountry As String
Dim varBook As Variant
Dim strMessage As String
Dim intCommand As Integer Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstCustomers = dbsNorthwind.OpenRecordset( _
"SELECT CompanyName, City, Country " & _
"FROM Customers ORDER BY CompanyName", _
dbOpenSnapshot) Do While True
' Get user input and build search string.strCountry = _
Trim(InputBox("Enter country for search."))
If strCountry = "" Then Exit Do
strCountry = "Country = '" & strCountry & "'" With rstCustomers
' Populate recordset.
.MoveLast
' Find first record satisfying search string. Exit
' loop if no such record exists.
.FindFirst strCountry
If .NoMatch Then
MsgBox "No records found with " & _
strCountry & "."
Exit DoEnd If Do While True
' Store book of current record.
varBook = .Book
' Get user choice of which method to use.
strMessage = "Company: " & !CompanyName & _
vbCr & "Location: " & !City & ", " & _
!Country & vbCr & vbCr & _
strCountry & 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 ' Use selected Find method. If the Find fails,
' return to the last current record.
If FindAny(intCommand, rstCustomers, _
strCountry) = False Then
.Book = varBook
MsgBox "No match--returning to " & _
"current record."
End If Loop End With Exit Do
Loop rstCustomers.Close
dbsNorthwind.Close
End Sub
Sub FindFirstX()
Dim dbsNorthwind As Database
Dim rstCustomers As Recordset
Dim strCountry As String
Dim varBook As Variant
Dim strMessage As String
Dim intCommand As Integer Set dbsNorthwind = OpenDatabase("Northwind.mdb")
Set rstCustomers = dbsNorthwind.OpenRecordset( _
"SELECT CompanyName, City, Country " & _
"FROM Customers ORDER BY CompanyName", _
dbOpenSnapshot) Do While True
' Get user input and build search string.strCountry = _
Trim(InputBox("Enter country for search."))
If strCountry = "" Then Exit Do
strCountry = "Country = '" & strCountry & "'" With rstCustomers
' Populate recordset.
.MoveLast
' Find first record satisfying search string. Exit
' loop if no such record exists.
.FindFirst strCountry
If .NoMatch Then
MsgBox "No records found with " & _
strCountry & "."
Exit DoEnd If Do While True
' Store book of current record.
varBook = .Book
' Get user choice of which method to use.
strMessage = "Company: " & !CompanyName & _
vbCr & "Location: " & !City & ", " & _
!Country & vbCr & vbCr & _
strCountry & 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 ' Use selected Find method. If the Find fails,
' return to the last current record.
If FindAny(intCommand, rstCustomers, _
strCountry) = False Then
.Book = varBook
MsgBox "No match--returning to " & _
"current record."
End If Loop End With Exit Do
Loop rstCustomers.Close
dbsNorthwind.Close
End Sub
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货