Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Search Access Database
Message
Information générale
Forum:
Visual Basic
Catégorie:
Access
Divers
Thread ID:
01028564
Message ID:
01028675
Vues:
11
OK, you might wanbt to look at a parametized query. But, here is what I have done in the past:

Method 1: This function assumes that you have three textboxes, one for each of the fields you want to search and returns a SQL statement that will return matching records when run (WARNING: I am making an assumption that field1 and 2 are text, 3 is numeric, so pay attention, ok?)
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 function
Method 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 function
Once you decide on the method all you have to do is call this function as the SQL for the recordset you will use.

These are not the best ways, there are also parametized queries (pain to work with from VB), and creating the query on the fly and inserting it to the MDB (not advisable for multi-user apps), but I think will get your assignment done.

Let me know if you have any questions, ok?

Regards,
Ricardo A. Parodi
eSolar, Inc.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform