Aditya Iyer
Aditya Iyer

Reputation: 9

Combobox Value to match with Query record value

How can a combobox value match with a column value in a query. Provided the combobox record source is from a table

I tried this code but it doesnt seem to work any help is appreciated

Private Sub open_button()
  If Me.CboName.Value = "Query!Query_test!Column(0)" then 

    Docmd.OpenForm FormName:="Customer"
  End if
End Sub

Upvotes: 0

Views: 642

Answers (1)

gizlmo
gizlmo

Reputation: 1922

You would need to loop through the query and check each record against the combobox value:

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("Query_test", dbOpenSnapshot) 'Open Query as Snapshot

rs.MoveFirst

Do Until rs.EOF 'loop through the query

    If rs.Fields(0).Value = Me.CboName.Value Then 'if column 0 matches Combobox

        'do something and exit loop
        DoCmd.OpenForm FormName:="Customer"
        Exit Do
    End If

    rs.MoveNext 'next record
Loop

'cleanup
rs.Close
Set rs = Nothing

Upvotes: 1

Related Questions