We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

dbcombo box using ODBC to SQL 6.5, how to write code properly??

s033slb
s033slb asked
on
Medium Priority
344 Views
Last Modified: 2008-02-01
How do I "bind" a dbcombo box to a single field in an SQL table with more than one field?  For example, I have a 'Cars' table with 2 fields.  The first field is the primary key 'CarID' and the second field is 'Car_Type' (which is unique).  I'd like the 'Car_Type' to be displayed in the dbcombo box list.  Once selected from the list, the 'Car_Type' text can be captured and the then used in a query on a different table.  How do I do this?  Currently, the datacontrol works fine and shows the appropriate data in text boxes as I scroll through the records.  The dbcombo box of car types should be a static list.
Comment
Watch Question

Author

Commented:
Edited text of question

Author

Commented:
Edited text of question

Author

Commented:
Edited text of question

Author

Commented:
Adjusted points to 100

Commented:
Are you sure you want to bind with a data control, in general it is better to control these things yourself!

Commented:
If you want the list to show the cars, set the following properties:
RowSource - the data control which is bound to the table.
ListField - Type (or select) Car_Type

Commented:
Of course, your question alos asks how to write code properly.  The best way to approach this problem is to use the enterprise version of VB, RDO or ADO, create your own resultsets, use an unbound control, and populate the control yourself from your resultset.  You have much more control of both the control and your access/locking effects on the SQL tables involved.

Read "Hitchiker's Guide to Visual Basic and SQL server" by W. R. Vaughn.  Good examples, thorough analysis and reasoning for choosing one access method over another.  Includes history and comparison between DAO/Jet, RDO, ADO and other methods, with adequate discussion of locking implications in multiuser scenarios of access method choices and good program behaviour.

Author

Commented:
The SQL tables are constantly changing.  In order to maintain a current list in the combo box, the control must be able to query the 'Cars' table (SELECT car_type FROM cars) and display  'Car_Type' in the list.  The user should then be able to select a car type which is used in a query to select all records from another table matching that 'Car_Type'.  The query results in records which can be scrolled through (data is seen in text boxes) using the datacontrol.  

The concept works now with a regular combo box and a bunch of Additem lines to create the list.  Obviously though, when the 'Cars' table had additions or deletions then my list no longer reflects the current table.  I'd like this to be automatic since I need to make about 10 more combo boxes to do the same thing with data from different tables.  I'm trying to use the combo boxes to make queries of the main table simpler (instead of having to type the query text all the time -- it can be selected from a combo box).

If anyone has a better idea or way of doing this, I'm all ears!!

Author

Commented:
To clifABB,

Before posting, I had already tried what you suggested, however it does not work.  The list that came up in the combo box was not unique.  Actually it was identical to the car field text box that is displayed from an initial query to display data in all the text boxes.  I'm ucing ODBC to connect to SQL.  I don't fully understand how it works, but a coworker helped me get the code working for the connection.

Do I need a second data control?  The one I'm using seems to be able to query all the tables.  If so, how do I go about this?

Thanks!!

Author

Commented:
cymbolic,

Your suggestion, "... create your own resultsets, use an unbound control, and populate the control yourself from your resultset."
This make perfect sense to me, but I don't have a clue how to accomplish it.  Can you help me (write the code)????  

Over the weekend I purchased the Hitchhiker's guide that you recommended, and have already done a bunch of reading.  It looks like a great resource, but at the moment the information is a bit over my head.  I'm just starting out with this and I'm confused by data objects, DAO, recordsets, and all the other acronyms.  I only began this project three weeks ago knowing nothing about SQL and very little about VB and MS Access 97.

I think the SQL database is accessed with Interface #6 on page 21 of the Hitchhiker's guide.  The code on pages 45-46 looks the most familiar.  However, there is also a data control used to stroll through the records which works.

I'd be very appreciative if you could help me!!!!

-- Stacey --

s033slb@discover.wright.edu

Author

Commented:
Adjusted points to 150

Commented:
Yes you should use a second data control.  The data control's RecordSource should be the Cars table.
If you are basing all the textboxes on what is selected in the combobox, you should set the following property:
BoundColumn = CarID

and put the following code in the combo boxes Click event:
Private Sub DBCombo1_Click()
  Dim sSQL As String

  sSQL = "SELECT * FROM Cars "
  sSQL = sSQL & "WHERE CarID = '" & DBCombo1.BoundText & "'"
  Data1.RecordSource = sSQL 'Data1 is your original data control
  Data1.Refresh
End Sub

As an added note, with all these other experts suggesting that you not use a data control.  Previous to VB5, the data control was, for the most part, slow and resource hungry.  MS fixed the data control with VB5.  I wouldn't suggest using a lot of data controls at one time, but two or three on a form is ok.

I am submiting this as a comment.  If it solves your problem, let me know and I'll re submit it as an answer.

Author

Commented:
clifABB,

Thanks for your suggestion.

Adding another data control (I'm using ODBC rather than JET) and another recordset (with appropriate SQL query) seems to work.  

During one of my 'surfing' ventures I found a portion of code that scrolled through a recordset (with a loop) putting a desired field of a record into a combo box as an .Additem.  Pretty nifty.

I repeated this procedure to create another 9 combo boxes.

Do you know if there is a better way to do this, possibly without needing all the additional 9 data controls?

I have 6 different forms that are the identical situation.

-- Stacey --


Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks clifABB!!

I should have mentioned that I did switch back to regular combo boxes yesterday when I got things working.

-- Stacey --

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.