Solved

How to use Combo Box with Database

Posted on 1997-03-29
1
167 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 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…

763 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

13 Experts available now in Live!

Get 1:1 Help Now