無論是用
Data1.Recordset.FindFirst 方法
還是用
Data1.Recordset.FindNext 方法
來進行查找記錄,都要使用
Data1.Recordset.NoMatch
來判斷是否找到符合的記錄,如果
Data1.Recordset.NoMatch 回傳 True,就表示沒有找到,如果回傳 False 就表示找到了。
而不能直接(最好不要直接)像你這樣使用:
Data1.Recordset.FindNext "字段2 Like" & "'*" & Text8.Text & "*'"
Combo1.AddItem Data1.Recordset.Fields(1)
而是要這樣:
Data1.Recordset.FindNext "字段2 Like" & "'*" & Text8.Text & "*'"
if Data1.Recordset.NoMatch Then
'這裡寫出終止繼續尋找的對應程式碼
else
Combo1.AddItem Data1.Recordset.Fields(1)
.........
End If
我上面說的最好不要直接像你這樣使用,意思在有的地方還是可以直接使用的,那就是你確定查找的記錄一定存在?
'新增一個TextBox(名為Text1, MultiLine屬性設True,用來輸入)、一個CommandButton(名為Command1,用來啟動分析)、一個ListBox(名為List1,用來證明陣列分析未出錯) 。
'新增如下程式碼。
Option Explicit
Private Sub Command1_Click()
Dim Num() As Long, i As Long, j As Long
#List1.Clear
If StrToNum(Text1.Text, Num) Then
ReDim t(0 To UBound(Num, 2)) As String
For i = 0 To UBound(Num, 1)
For j = 0 To UBound(Num, 2)
t(j) = CStr(Num(i, j))
Next
List1.AddItem Join(t)
Next
Else
MsgBox "Wrong Input!"
#End If
End Sub
Private Function StrToNum(S As String, N() As Long) As Boolean
##Dim A() As String, B() As String, C As Long, D As String, E As Boolean, F As Long
Dim i As Long, j As Long
##If Len(S) = 0 Then Exit Function
A = Split(S, vbCrLf)
For i = 0 To UBound(A)
Erase B
C = 0
E = False
For j = 1 To Len(A(i))
D = Mid(A(i), j, 1)
Select Case Asc(D)
Case 48 To 57
If Not E Then
ReDim Preserve B(0 To C)
C = C 1
E = True
End If
B(C - 1) = B(C - 1) & D
Case 32
E = False
Case Else
Exit Function
End Select
Next
If C = 0 Then Exit Function
If i = 0 Then
ReDim N(0 To UBound(A), 0 To C - 1)
F = C
Else
If FC Then Exit Function
End If
For j = 0 To C - 1
N(i, j) = Val(B(j))
Next
Next
StrToNum = True
###End Function###以上是使用VB循環查詢資料庫字串並將其新增至下拉列錶框的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!