Microsoft KB Archive/94003

= ACC: How to Use Dynamic Data Exchange (DDE) to Fill a List Box =

Article ID: 94003

Article Last Modified on 1/18/2007

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition
 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q94003



Advanced: Requires expert coding, interoperability, and multiuser skills.



SUMMARY
This article shows you how to create a sample user-defined Visual Basic for Applications function that uses dynamic data exchange (DDE) to populate a list box.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual.

NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; manual in Microsoft Access version 2.0



MORE INFORMATION
The following function uses DDE to retrieve a list of countries from Microsoft Excel. The list will be used to populate a list box bound to the Country field of the Suppliers table in the sample database Northwind.mdb (or NWIND.MDB in Microsoft Access versions 1.x and 2.0).   In a new worksheet in Microsoft Excel, enter the following data in the first column:       Cell   Value A1    Australia A2    China A3    Scotland  Save the worksheet as COUNTRY.XLS. Open the sample database Northwind.mdb in Microsoft Access, and open a new module.  Type the following lines in the Declarations section: Option Explicit Dim Countries(10) As String Dim CountryData As String   Type the following Sub procedure: Sub DDEFillCountries Dim EndString%, i%        Dim Chan

EndString% = 0 i% = 0 Chan = DDEInitiate(&quot;Excel&quot;, &quot;COUNTRY.XLS&quot;) CountryData$ = DDERequest(Chan, &quot;R1C1:R10C1&quot;) DDETerminate Chan EndString% = InStr(1, CountryData, Chr(13))

Do Until Len(CountryData$) = 0 Countries(i%) = Left(CountryData, EndString% - 1) CountryData$ = Right(CountryData, _                        Len(CountryData) - EndString% - 1) EndString% = InStr(1, CountryData, Chr(13)) i% = i% + 1 Loop

End Sub   Type the following function: Function DDEFillList (fld As Control, id, row, col, code)

Select Case code Case 0 Call DDEFillCountries DDEFillList = True Case 1 DDEFillList = id           Case 3 DDEFillList = 10 Case 4 DDEFillList = 1 Case 5 DDEFillList = -1 Case 6 DDEFillList = Countries(row) End Select End Function </li> Save the module as DDE Fill List Box.</li> Create a new form based on the Suppliers table.</li> Add a list box bound to the Country field and set the following property:

RowSourceType: DDEFillList

</li> Verify that Microsoft Excel is running and the worksheet, COUNTRY.XLS, is open. Switch to Form view. Note that the list box is populated with the countries that you entered in Microsoft Excel.</li></ol>

NOTE: This article demonstrates a simple example and is not a complete and robust function. If you intend to use this with a form, there are several things that you may want to add to your code, including:
 * Error checking for the DDE portion to handle cases where Microsoft Excel is not running or is busy.
 * Refreshing the form if needed to show updated data.

<div class="references_section">