Try this code...
'used stuffs :- button(command1), list box(list1) , database,table and fields name remain same
Option Explicit
Dim connection As ADODB.connection
Dim recordset As ADODB.recordset
Dim CITYARRAY(5) As String, QUERY As String
Private Sub Command1_Click()
Dim i, f, nf As Integer
f = 0 'variable for tracking total no. of cities found in the database
nf = 0 'variable for tracking total no. of cities not found in the database
CITYARRAY(0) = "MIAMI"
CITYARRAY(1) = "FRED"
CITYARRAY(2) = "INDIAN HILL"
CITYARRAY(3) = "MARTINSVILLE"
CITYARRAY(4) = "MARY"
'looping through each element of the array,sending its value through the query and checking whether this value does exist in the database or not.
'if match found then increasing the f variable by 1 & printing msg in the listbox otherwise increasing the nf variable by 1 & also displaying a msg in the
'listbox
For i = 0 To 4 Step 1
QUERY = "select city from uscity where city in(" & Chr(34) & CITYARRAY(i) & Chr(34) & ")"
recordset.Open QUERY, connection, adOpenDynamic, adLockOptimistic
If recordset.RecordCount > 0 Then
List1.AddItem CITYARRAY(i) & " [ Found in the database ]"
f = f + 1
Else
List1.AddItem CITYARRAY(i) & " [ Not Found in the database ]"
nf = nf + 1
End If
recordset.Close
Next i
'printing summary of the above query
List1.AddItem ""
List1.AddItem "-=Result of Searching=-"
List1.AddItem "Cities found : " & f
List1.AddItem "Cities not found : " & nf
End Sub
Private Sub Form_Load()
'initiating the connection and recordset objects here. try to avoid doing the same in general declaration section
Set connection = New ADODB.connection
Set recordset = New ADODB.recordset
recordset.CursorLocation = adUseClient
recordset.CursorType = adOpenDynamic
'specifiying location of the database file dynamically so there is no need to mention the path of the database everytime the location of that file is
'changed. app.path automatically detects path of the current working directory of the project file.
connection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\usgeo.mdb" & ";PERSIST SECURITY INFO=FALSE"
End Sub