Kenny
Kenny

Reputation: 849

Query to find numeric and text in MS Access

I have written a Search query to find data from MS Access table. But when the user does not type in the number like ID, the query fails.

"Select UID, FirstName from tUserInfo where UID = " + UserID + " and  FirstName like '%" + txtSearchFirst.Text + "%'"

How to search table when the number is empty?

Upvotes: 0

Views: 367

Answers (2)

Fionnuala
Fionnuala

Reputation: 91366

Some notes.

    thisCommand.CommandText = "SELECT UID, FirstName FROM tUserInfo " +
    " WHERE FirstName Like '%' & ?  & '%' And UID & "" Like ? & '%';";

    //Names are irrelevant with OLEDB and MS Access, the order is important
    thisCommand.Parameters.AddWithValue("@Param", txtSearchFirst.Text);
    thisCommand.Parameters.AddWithValue("@Param", UserID + "");

Upvotes: 0

juergen d
juergen d

Reputation: 204766

Select UID, FirstName from tUserInfo 
where (UID = " + UserID + " or UID is null)
and FirstName like '%" + txtSearchFirst.Text + "%'

Upvotes: 2

Related Questions