Logicwurks Home Page

Links To Excel Code Examples

Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
Union Of Ranges
Parse Range Strings
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
TypeName And TypeOf
Loop Through Worksheets
Loop Through Open Workbooks
Form Button Magic
Command Button Magic
Add Worksheets Dynamically
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Using Find Instead of Vlookup
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Edit Numerics In UserForm
Load Combo And List Boxes
Floating Sheet Combo Boxes
Advanced User Form Coding
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Read A Text File w/Handle
Write A Text File w/Handle
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Date Picker Alternative
Generate Multiple Worksheets
Read Access Data Into Excel
Send Outlook Email w/Attach
Copy AutoFilters To Sheets
Export A Text File
Get Windows User Name
VBA Format Statement
Manipulate Files via VBA
Dynamically Load Images
Loop Through Worksheet Objects
Loop Through Form Objects
Loop Through Files with DIR
Active-X Checkboxes
Add Forms Checkboxes Dynam
Paste Pictures Into Excel
Copy Pictures Sheet To Sheet
Copy Pictures Sheet To Sheet
Create Forms Buttons With VBA
Extract Filename From Path
Convert R1C1 Format to A1
Special Cells Property
Insert Cell Comments

Links To Access Code Examples

Create Recordset With AddNew
Multi-Select List Boxes
Update Field(s) In A Recordset
Update Excel Pivot From Access
Import A Tab Delimited File
Export Excel FileDialog
Create Excel Within Access
Open Excel Within Access
Open Excel OBJ From Access
Format Excel From Access
Import Tab Delim w/WinAPI
Initialize Global Variables
Using TempVars For Globals
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Bookmarks And Forms
Combo Box Multiple Sources
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Reference Subform Objects
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Dlookup Command
Dlookup In Form Datasheet
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects
DSNLess Connections To MySQL
Print Active Form Record
Count Records in Linked Tables
Delete Empty Tables
Open Linked SQL Tables


Use Dlookup In A Datasheet Form To Populate an Unbound Text Box From Another Table

For a simple example, we will create an Access database with only 2 tables:
(1) Products - This contains two columns: ItemId and Description
(2) Finders - This contains one column: ItemId.

The Products table contains

(1) ItemID
(2) Description

The Finders table only contains one column: ItemID. Assume that there are 5000 products in the Product table, but the user only wants to select about 100 of those for a report. The Finders table contains ItemIDs for only the ones the user wants to report.

Occasionally, the user needs to update the finders table by adding or removing items. To do this, we create a form to handle this process. It will add new items and validate that the user entered the ItemID correctly by displaying the description. It is important to note that the Description is not a part of the forms's primary data source, and must be looked up.

(1) Create a form where the data source is the Finders table. It will only have one column so far.
(2) Add an unbound text box
(3) To populate the textbox, enter the following in the Control Source property for that text box:

=DLookUp("Description","Products","ItemID = '" & [Forms]![frmTest]![ItemID] & "'")

This syntax uses the ItemId entered by the user on the form, searches the Products table for a matching ItemID, and returns the Description to be populated in the text box.

Mo VBA is required to accomplish populating the text box: Just a modification of the property for the Text Box.

Depending on the data type to be returned, the syntax of the DLookup is different as shown below:

Syntax Examples

General: DLookup Usage Samples
Dev Ashish
Note that the same logic applies to most Domain Aggregate Functions (DMax, DMin etc.)

Normal usage

For numerical values:
        DLookup("FieldName" , "TableName" , "Criteria = n")

For strings: (note the apostrophe before and after the value)
        DLookup("FieldName" , "TableName" , "Criteria= 'string'")

For dates:
        DLookup("FieldName" , "TableName" , "Criteria= #date#")
Refering to a form control

For numerical values:
        DLookup("FieldName", "TableName", "Criteria = " & forms!FormName!ControlName)

For strings: (note the apostrophe before and after the value)
        DLookup("FieldName", "TableName", "Criteria = '" & forms!FormName!ControlName & "'")

        DLookup("FieldName", "TableName", "Criteria = #" & forms!FormName!ControlName & "#")


   DLookup("FieldName", "TableName", "Criteria1 = " & Forms!FormName!Control1  _
             & " AND Criteria2 = '" & Forms!FormName!Control2 & "'" _
            & " AND Criteria3 =#" & Forms!FormName!Control3 & "#")