Solved

How to use Combo Box with Database

Posted on 1997-03-29
1
173 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month6 days, 15 hours left to enroll

623 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