Solved

How to use Combo Box with Database

Posted on 1997-03-29
1
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

752 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