Solved

How to use Combo Box with Database

Posted on 1997-03-29
1
168 Views
Last Modified: 2008-03-06
Dear Experts;

     In my application ; It is necessary to use Combo Box to show information from DB. It must show information. 1 Combo is shown 2 fields..

     2. How to code to seek information.[ Assume that I want to seek Citystate ]

     Please answer me... How to code...

     I have  ANUM.[Counter field and Index], Addr1, addr2, citystate and Zip
0
Comment
Question by:u3520384
1 Comment
 
LVL 1

Accepted Solution

by:
ksleung earned 10 total points
ID: 1424816
In the combo boxes (normal Combo Box or DBCombo Box), they cannot show two columns.  You have two choices though.  One is to buy some 3rd party OCX (e.g. Sheridan Data Widgets) to accomplish this task.  Or you can write some code to do it.  What you should do is as follows.

Assumptions:
1.  The form where the combo box (Combo1) resides on is called Form1.
2.  The database is called Test.MDB which is located in C:\TEST.
3.  The table is called tbl_Test.
4.  The Combo box shows the Citystate and ZIP fields.
5.  Set the Combo Box Style to 1 so that the user can type in the searching string.

In the Form Load event,

Sub Form1_Load()
  Dim DB As Database, Rs As Recordset

  'connect to the database
  Set DB=DBEngine.Workspaces(0).OpenDatabase("C:\Test\Test.MDB")
  'open a read only recordset
  Set Rs=DB.OpenRecordSet("tbl_Test", DBOpenSnapshot)
  'Goto the first record
  Rs.MoveFirst
  Do Until Rs.EOF
    Combo1.AddItem Rs.CityState & " " & Rs.ZIP
    Rs.MoveNext
  Loop
End Sub

The above code will create a combo box which shows 2 fields whenever the form is loaded.

To search the Citystate field, code the followings.

In the Combo1 Click Event,

Sub Combo1_Click()

  Dim sSearch As String

  sSearch="CityState='" & Trim$(Combo1.Text) &"'"
  Rs.FindFirst sSearch
  If Not Rs.NoMatch Then
    Combo1.Text=Rs.CityState & " " & Rs.ZIP
  Else
    MsgBox "Entry does not exist."
    Exit Sub
  End If
End Sub

The above code will use the user input as the search string to search through the database.  If it exists, it will show up in the Combo Box.  Otherwise, the user will be warned for wrong entry.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now