function BuildSQL() as string dim strSQL as string dim strSQLWhere as string dim strSQLSortOrder as string 'Initialize the query strings, you can add a sort order parameter to the query, here it will be just empty strSQL="select field1,field2,field3 from tblYourTable where 1=1 " strSQLWhere="" strSQLSortOrder="" 'The way VBA and VB handle strings makes me cautions so I always 'concatenate them with an empty string to avoid invalid use of null errors if (txtField1Filter1 & "") <> "" then 'Remember to leave a space at the end. This will look for fields starting with the chars 'in the search box for this. If you want to search the whole field look at filter 2 strSQLWhere = strSQLWhere & "AND Field1 like """ & txtFieldFilter1 & "*"" " end if if (txtField1Filter2 & "") <> "" then strSQLWhere = strSQLWhere & "AND Field2 like ""*" & txtFieldFilter2 & "*"" " end if if (txtField1Filter3 & "") <> "" then strSQLWhere = strSQLWhere & "AND str(Field3) like """ & txtFieldFilter3 & "*"" " end if BuildSQL=strSQL & strSQLWhere & strSQLSortOrder end functionMethod 2: This function assumes that you have ONE textbox and this will return a sql statement with any rows that contained the search criteria in any of the rows in any poart of the fields.
function BuildSQL() as string dim strSQL as string dim strSQLWhere as string dim strSQLSortOrder as string 'Initialize the query strings, you can add a sort order parameter to the query, here it will be just empty strSQL="select field1,field2,field3 as SearchField from tblYourTable where 1=1 " strSQLWhere="" strSQLSortOrder="" 'The way VBA and VB handle strings makes me cautions so I always 'concatenate them with an empty string to avoid invalid use of null errors if (txtField1Filter1 & "") <> "" then strSQLWhere = strSQLWhere & "AND (field1 & field2 & str(field3)) like ""*" & txtFieldFilter1 & "*"" " end if BuildSQL=strSQL & strSQLWhere & strSQLSortOrder end functionOnce you decide on the method all you have to do is call this function as the SQL for the recordset you will use.