Microsoft Knowledge Base Email Alertz

KBAlertz.com: (164923) - This article includes a Visual Basic for Applications sample macro that fills a ListBox control with values from an external database using the DAO 3.5 Object Library reference.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 164923 - Last Review: January 19, 2007 - Revision: 2.4

OFF97: How to Fill a UserForm ListBox with Database Values

This article was previously published under Q164923

On This Page

SUMMARY

This article includes a Visual Basic for Applications sample macro that fills a ListBox control with values from an external database using the DAO 3.5 Object Library reference.

MORE INFORMATION

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

This Visual Basic for Applications example uses three routines for future expansion and portability. To use the procedures, place them in the UserForm module. These procedures are as follows:
  • The UserForm Initialize procedure sets the database values to use, such as the database path and name values, and sets the table and field values in the selected table.
  • The DBConnect procedure makes the actual connections to the database, the table, and the fields that you want to use.
  • The PopulateListBoxData procedure populates the list box.

Referencing the DAO 3.5 Object Library

For this example to work, you must add the DAO 3.5 Object Library reference.

To add the DAO 3.5 Object Library reference, use the following steps:
  1. In the Visual Basic Editor, click References on the Tools menu to display the References dialog box.

    The References dialog box displays all object libraries and projects that are registered with the operating system.
  2. Scroll through the list for the library you want to reference. If the application is not listed, click Browse to search for object library (.olb and .tlb) files or executable (.exe and .dll) files. References with check boxes that are selected are used by the project; references with check boxes that aren't selected are not used.
  3. Click to select the check box for the reference in the Available References list and click OK.

Typing the Code in the UserForm

Type the following code in the General Declarations section of the UserForm:
   Public oDataBase As Database
   Public oWorkSpace As Workspace
   Public oRecordSet As Recordset
   Public strDB As String
   Public strDBTable As String
   Public strDBField As String
				

Sample Macro

The following example macro uses the Microsoft Access 97 Northwind.mdb database, which is located in the default Office folder in the Samples folder:
      Private Sub UserForm_Initialize()
      ' ********************************************
      ' Form initialization.
      ' Enter all values here for Database, Table
      ' and Field to use.
      ' ********************************************
      ' DataBase to Use.
      strDB = Options.DefaultFilePath(wdProgramPath) & _
      "\Samples\Northwind.mdb"
      ' Database Table to Use.
      strDBTable = "Customers"
      ' Database Field to Use.
      strDBField = "CompanyName"
      DBConnect
      PopulateListBoxData
   End Sub

   Sub DBConnect()
      ' ********************************************
      ' Initializes Jet workspace and opens a database
      ' ********************************************
      ' Establish database WorkSpace.
      Set oWorkSpace = CreateWorkspace(Name:="JetWorkspace", _
      UserName:="admin", Password:="", UseType:=dbUseJet)
      ' Open the database.
      Set oDataBase = OpenDatabase(strDB)
      ' Set the record set to the specified table.
      Set oRecordSet = oDataBase.OpenRecordset(strDBTable)
   End Sub

   Sub PopulateListBoxData()
   ' Fill list box with data from selected field in table.
      ' This example populates the list box with values from
      ' the CompanyName field of the Customers table.
      oRecordSet.MoveFirst
      Do Until oRecordSet.EOF
         ListBox1.AddItem oRecordSet.Fields(strDBField)
         oRecordSet.MoveNext ' Move to next record in table.
      Loop
   End Sub
				
NOTE: To use the example, change the values to the full database path and name and change the values for the table and fields that you want to use.

For additional information, please see the following article in the Microsoft Knowledge Base:
164815  (http://kbalertz.com/Feedback.aspx?kbNumber=164815/EN-US/ ) How To Create And Display A Custom Dialog Box

APPLIES TO
  • Microsoft Word 97 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office 97 Standard Edition
Keywords: 
kbhowto kbprogramming KB164923
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please