How to Use Wildcards in SQL Query to Make Dynasets & SnapshotsLast reviewed: February 18, 1996Article ID: Q110069 |
The information in this article applies to:
- Professional Edition of Microsoft Visual Basic for Windows, version 3.0
SUMMARYYou can build a Dynaset or Snapshot based upon wildcard field-search characters in an SQL query. The find methods (FindFirst, FindLast, FindNext, and FindPrevious) can also search a Dynaset or Snapshot using wildcard search characters in an SQL query.
MORE INFORMATIONBy using the Like statement in the SQL query language, you can search for database field values using the asterisk (*) and question mark (?) characters as wildcards. The * and ? wildcards let you find a wider set of field values beginning or ending with any desired root. For example, the following SQL syntax selects the records from a table where the Authorfield field values begin with the letter b:
Select * from XTable Where Authorfield Like 'b*'NOTE: The Seek method, which only applies to Table object variables, cannot use SQL queries or wildcard search characters. The Seek method is limited to finding a single record using the comparison operators: >, >=, <=, <, =, and <>.
Asterisk (*) Wildcard UsageIn the SQL syntax for the Like statement, the asterisk (*) acts as a wildcard place holder for any number of characters, from zero up to the field length. A search for b* finds any field value beginning with the letter b. A search for *b finds any field value ending with b. A search for *xxxx* finds any field value that contains the xxxx substring. A search for * by itself matches all field values.
Question Mark (?) Wildcard UsageIn the SQL syntax for the Like statement, the question mark (?) acts as a wildcard place holder for a single character. A search for ??b* finds any field value that has b in the third character. A query for *b?? finds any field value with b as the third from the last character.
Speed ConsiderationsOf the following two techniques, 1 is faster than 2:
Example Using SQL Wildcard Search with a Data ControlThe Text1 box in the following program shows individual records of the Author field of the BIBLIO.MDB database. When you click the Command1 button, the program automatically appends and prefixes the * wildcard search character to any search string that you enter in the Text2 text box. That widens the resulting recordset shown in Text1. You can browse the recordset shown in Text1 by clicking the data control.
The Seek Method Does Not Support Wildcard SearchesThe Seek method, which works only with Table object variables, is very fast but doesn't support wildcard searches. Seek is mainly useful for finding a single record that matches a given criteria. The find and move methods are more practical than the Seek method for finding a group of records. The Seek method feature that is closest to a wildcard search is a comparison operator: >, >=, <=, or <. For example, you could find the first record that is greater than or equal to your search key value as follows:
Dim MyDB As Database, MyTable As Table Set MyDB = OpenDatabase("BIBLIO.MDB") ' Open a database. Set MyTable = MyDB.OpenTable("Publishers") ' Open a table. MyTable.Index = "PrimaryKey" ' Define current index. MyTable.Seek ">=", 3 ' Seek a record with PrimaryKey >= 3. If MyTable.NoMatch Then MsgBox "match was not found" Else MsgBox "match was found" End If |
Additional reference words: 3.00 faster slow speedy quick quicker
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |