How to Implement the DLookup Function in Visual BasicLast reviewed: June 21, 1995Article ID: Q99704 |
The information in this article applies to:
- Microsoft Visual Basic Programming System for Windows, version 3.0
SUMMARYMicrosoft Access provides a set of domain, or record set, functions that are useful in getting the value of one field based on criteria involving another field. The DLookup domain function is particularly useful. Although Visual Basic does not contain the DLookup function, you can write the equivalent using Visual Basic code. This article describes how to implement the DLookup domain function in Visual Basic.
MORE INFORMATIONIn Microsoft Access, the DLookup domain function returns the value of a field for a given set of criteria. The syntax for the DLookup function is as follows:
DLookup(expr, domain , criteria) Argument Description ---------------------------------------------------------------- expr String expression identifying the field that contains the data you want to return. Operands in expr can include the name of a table field. domain String expression identifying the records that constitute the record set. It can be a table name, query name, or SQL expression that returns data. criteria Optional string expression used to restrict the range of data on which DLookup is performed. For example, criteria could be the SQL expression's WHERE clause without the word WHERE. If criteria is omitted, DLookup evaluates expr against the entire record set. Step-by-Step to a Custom Visual Basic DLookup FunctionThe following steps show by example how to create a Visual Basic custom DLookup function.
Examples Showing How to Use DLookupBelow are some more examples showing how you can use the DLookup function.In the following example, from the Authors table in the Visual Basic BIBLIO.MDB sample database, DLookup uses the Au_ID field to return the corresponding author name for the author whose ID is 17. Assume that the variable AuthorName is a string.
AuthorName = DLookup("Author", "Authors", "Au_ID = 17")If the criteria argument contains non-numeric text other than field names, you must enclose the text in single quotation marks. In the following example from the Titles table of the BIBLIO.MDB database, ISBN is the name of a field, and 0895886448 is a string literal.
BookTitle1 = DLookup("Title", "Titles", "ISBN = '0895886448'") BookTitle2 = DLookup("Title", "Titles", "Au_Id = 17")Even if more than one record satisfies criteria, DLookup returns only one field. If no record satisfies criteria, or if the domain contains no records, DLookup returns a Null.
|
Additional reference words: 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |