Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use Combo Box with Database

Posted on 1997-03-29
1
Medium Priority
?
174 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 20 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

688 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