Solved

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

Posted on 1998-07-09
15
311 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.
0
Comment
Question by:s033slb
15 Comments
 

Author Comment

by:s033slb
Comment Utility
Edited text of question
0
 

Author Comment

by:s033slb
Comment Utility
Edited text of question
0
 

Author Comment

by:s033slb
Comment Utility
Edited text of question
0
 

Author Comment

by:s033slb
Comment Utility
Adjusted points to 100
0
 
LVL 2

Expert Comment

by:chris_a
Comment Utility
Are you sure you want to bind with a data control, in general it is better to control these things yourself!
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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
0
 
LVL 9

Expert Comment

by:cymbolic
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:s033slb
Comment Utility
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!!

0
 

Author Comment

by:s033slb
Comment Utility
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!!
0
 

Author Comment

by:s033slb
Comment Utility
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
0
 

Author Comment

by:s033slb
Comment Utility
Adjusted points to 150
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
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.
0
 

Author Comment

by:s033slb
Comment Utility
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 --


0
 
LVL 6

Accepted Solution

by:
clifABB earned 150 total points
Comment Utility
As I said in my comment, using one or two data controls would be OK, but using nine?  When it comes to that many, your "discovery" would be a better idea.

Basically you would need to replace your DBComboboxes with standard combo boxes.  Hopefully CarID is a numeric field which will make life so much easier.
This code loads your combo box:
  Dim dbDatabase As Database
  Dim rsTable As Recordset

  Set dbDataBase = OpenDatabase(connectstring)
  Set rsTable = dbDatabase.OpenRecordset("Select CarID, Car_Type From Cars", dbOpenSnapshot)
  Do Until rsTable.EOF
    Combo1.AddItem rsTable.Fields("Car_Type")
    Combo1.ItemData(Combo1.NewIndex) = rsTable.Fields("CarID")
    rsTable.MoveNext
  Loop
  rsTable.Close
  dbDatabase.Close

Then you can use my code above with a small modification to load the text boxes:
Private Sub Combo1_Click()
  Dim sSQL As String

  sSQL = "SELECT * FROM Cars "
  sSQL = sSQL & "WHERE CarID = " & Combo1.ItemData(Combo1.ListIndex)
  Data1.RecordSource = sSQL 'Data1 is your original data control
  Data1.Refresh
End Sub

By the way, after connecting to the database, there is not much difference between Jet and ODBC from a coding point of view.
0
 

Author Comment

by:s033slb
Comment Utility
Thanks clifABB!!

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

-- Stacey --

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Share codes 68 114
Public Sub, Empty Cell 7 56
Vb.net Listview 9 76
passing parameter in sql procedure 9 50
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

6 Experts available now in Live!

Get 1:1 Help Now