Logicwurks Home Page

Links To Excel Code Examples

Tracing VBA Statements
Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
ConvertCellAddressToRange
Set Conditional Formatting
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
ImportExternalWorksheets
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Range Copy With Filter
ExcelFileOpenSaveClose
ExcelFileOpenSaveCSV
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Save A Workbook
Save A Workbook Using mso
Clone A Workbook
Test If WEB URL Exists
Parse Using Split Command
Using Classes in Excel
TypeStatementStructures
Color Management
Convert Cell Color To RGB
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Search Using Xlookup 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
Insert Data Validation Sub
Read A Text File w/Handle
Write A Text File w/Handle
Read a Binary File w/Handle
Update a Binary File w/Handle
Binary File Copy and Update
Read A Text Fiile w/Script
Text File Processing Examples
Test For Exists Or Open
Splash Screen
Dynamically Load Formulas
PaymentStreamsByDate
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Load Arrays Using Evaluate
ChartsAndGraphsVBA
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

DAO Versus ADODB
SearchVBACodeStrings
Interface Excel With Access
Create Form Manually
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
Control Excel via Access VBA
On Error Best Practices
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

 

Using Multiple Row Sources For a Combo Box

Let's say you have an application that includes a table of client names, addresses and other important information. The client is assigned a "friendly" ID, such as the first four letters of their last name followed by a couple of numbers. In addition, Access assigns the actual unique primary key ID.

When you want to pull up the client's record in a form, you would like to be able to search by the "user friendly" ID, but also be able to find them based on a last name, first name combination. Additionally, it is useful to be able to locate them by the Access-assigned primary key.

A combo box with some VBA in the backend will accomplish this goal. The key to making this function propertly is to dynamically assign the combo box's RowSource property. Using the above example where the user would like to look up a client in three different ways, the developer can create three different queries to be used as the RowSource property.

One technique to provide an interface to select which of the three methods to query is a radio button group. The client chooses which of the three methods by clicking the correct radio button, and then clicks the drop down box.

This combination of radio buttons and a combo box appears as follows:

The combo box drop down has three columns, so that the user can see the three keys for each client, regardless of which method is used to search for the record. Each of the three queries is structured so that the third column is the Internal Access-assigned primary key. This is the key that is used to locate the record, since it is guaranteed to be unique. In other words, the third column is the BOUND column value for the combo box search result value. The property sheet for the combo box is shown below:

The code below shows the code for the radio buttons and the combo box.

Program Code

Private Sub cmbClientSelected_GotFocus()
' *******************************************************
' When The Combo Box receives the focus, set the
' RowSource property based on which radio button
' is active.  The values of 1, 2 and 3 are the
' radio button values which depend on which radio
' button was active at the time the combo box received
' the focus.  
' *******************************************************
cmbClientSelected = ""
Select Case frmSearchOption
    Case 1
        cmbClientSelected.RowSource = "qryByCANClientID"
    Case 2
        cmbClientSelected.RowSource = "qryByName"
    Case 3
        cmbClientSelected.RowSource = "qryByInternalID"
End Select

End Sub


Private Sub cmbClientSelected_AfterUpdate()
' *******************************************************
' When The User Clicks the Combo Box and then selects
' a client, this  sub is activated. The value of the
' combo box object is the third BOUND column of the
' query.
' *******************************************************
Dim rst As DAO.Recordset
Dim lngClientContactID As Long
Dim bkmBookmark As Variant

' *****************************************************************
' Exit If The User Deleted the Content of the Combo Box
' *****************************************************************
If IsNull(Me.cmbClientSelected) Or Me.cmbClientSelected = "" Then
    Exit Sub
End If

On Error GoTo Bookmark_Err

' *********************************************************************
' Using The Form's RecordsetClone, Locate The Record Selected
' From the Combo Box
' *********************************************************************
lngClientContactID = CLng(Me.cmbClientSelected)
Set rst = Me.RecordsetClone
rst.FindFirst "CCClientContactID = " & lngClientContactID
If rst.NoMatch Then
    MsgBox "Record not found"
    rst.Close
    Set rst = Nothing
    Exit Sub
End If
bkmBookmark = rst.Bookmark
    
' *********************************************************************
' Position the form record to the Selected Value
' *********************************************************************
Me.Bookmark = bkmBookmark
    
' *********************************************************************
' Close The Recordset Clone
' *********************************************************************
    rst.Close
    Set rst = Nothing

Bookmark_Exit:
    Exit Sub

Bookmark_Err:
    MsgBox Error$
    Resume Bookmark_Exit
End Sub