Search multiple fields access using findfirst criteria in VBA

user1687929 picture user1687929 · Sep 12, 2013 · Viewed 10.9k times · Source

The following code is populating values on a form after it finds a match in a Mobile phone records table, in the criteria, how can I search for the value in 2 different fields? I could replace the combo box with a text box and would like to search in 2 fields, IMEI and Mob_number.

Private Sub Combo0_AfterUpdate()

Dim D As Database
Dim rsmob As Recordset
Dim Criteria As String

Set D = CurrentDb
Set rsmob = D.OpenRecordset("Mobile_Phones", DB_OPEN_DYNASET)

Criteria = "[MOB_NUMBER]='" & [Combo0] & "'"

rsmob.FindFirst Criteria

Me!Location = rsmob("User_Name")
Me!MODEL = rsmob("Model")
Me!IMEI = rsmob("IMEI")
Me!DIR = rsmob("DIR")
Me!Status = rsmob("Status")
Me!Account = rsmob("ACCOUNT")
Me!Plan = rsmob("Plan")
Me!MobOrWifi = rsmob("Mob_Or_Wifi")

rsmob.Close

End Sub

Answer

Gord Thompson picture Gord Thompson · Sep 12, 2013

how can I search for the value in 2 different fields? [IMEI and Mob_number]

You should be able to search on two fields by changing your statement to

Criteria = "[MOB_NUMBER]='" & [Combo0] & "' OR [IMEI]='" & [Combo0] & "'"