Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2005, 08:09 PM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default VB6 ComboBox Source from Recordset

I am trying to set the selections in a combo box to the fields in an ADO database. Have tried many different types of coding it, but this is the closest I got. Unfortunetly when the combo box is pulled down, it has nothing but blanks.

Below is the code I am currently using. I specificly want to display all the "Status" fields in the "Selection_Fields" table of the "TS99_v101" database in "cboStatus" combobox.


''''''''''''''''''''''''''''''''''''''''
Dim DBconnectionSF As New ADODB.Connection
Dim StatusSFrset As New ADODB.Recordset

Private Sub Form_Load()DBconnectionSF.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
      & "Data Source=" & App.Path & "\TS99_v101.mdb;Jet _
      OLEDB:Database Password=work"
StatusSFrset.Open "SELECT Status FROM [Selection_Fields]"_
       , DBconnectionSF, adOpenDynamic, adLockOptimistic

With StatusSFrset
    Do While Not .EOF
        cboStatus.AddItem (Status)
        .MoveNext
    Loop
End With
cboStatus.Refresh

End Sub

Thanks for any help!!

 
Old April 10th, 2005, 01:08 PM
Authorized User
 
Join Date: Apr 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

I'm going to correct your code for you persuming you use a normal combobox:

Dim DBconnectionSF As ADODB.Connection
Dim StatusSFrset As ADODB.Recordset
dim strQ As String

Private Sub Form_Load()

DBconnectionSF.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
      & "Data Source=" & App.Path & "\TS99_v101.mdb;Jet _
      OLEDB:Database Password=work"
strQ = "SELECT DISTINCT([Status]) FROM [Selection_Fields] ORDER BY Status ASC"

StatusSFrset.Open strq,DBconnectionSF, adOpenForwardOnly, adLockReadOnly

While StatusSFrset.EOF = False
 with cbostatus
   .AddItem trim$(rs!Status)
  end with
  StatusSFrset.MoveNext
wend

StatusSFrset.Close: Set StatusSFrset = nothing
DBconnectionSF.Close: Set DBconnectionSF = nothing

End Sub

Shout if you need more help

Helga Anagnostopoulos
 
Old April 12th, 2005, 01:56 PM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Helga,
I copied your code exactly and am coming up with the error when the form loads:

Run-time error '424'
Object required

The debug highlights the following line:

.AddItem Trim$(rs!Status)

Sorry, but I really don't understand what the Trim$ of this line does. Any help would be greatly appreciated. Since I am a newbie with VB6, don't want to asume changing your code without knowing why it should work.

Also, is it required to use the strQ line instead of consolidating it like in my original code? Don't mind doing it, again just wanted to know how it works.

Thanks,
Tom
 
Old April 12th, 2005, 04:41 PM
Authorized User
 
Join Date: Jan 2005
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have been messing with the code a little more and noticed something. With my original code, the combo actually had a blank line for each line in the database field. It seems that the info is getting to the recordset, but the combo box is not displaying the actual text as it goes through each field.

To test this, I ran the following code:

Private Sub Form_Load()
cboStatus.AddItem ("temp 1")

DBconnectionSF.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\TS99_v101.mdb;Jet _
OLEDB:Database Password=work"
StatusSFrset.Open "SELECT ([Status]) FROM [Selection_Fields] _
ORDER BY Status ASC", DBconnectionSF, adOpenForwardOnly, _
adLockReadOnly
With StatusSFrset
    Do While Not .EOF
        cboStatus.AddItem (Status)
        .MoveNext
    Loop
End With
DBconnectionSF.Close

cboStatus.AddItem ("temp 2")
End Sub

The result of the combobox pulldown was as follows:

--------------
temp 1






temp 2
--------------

Without the code refering to the database, it looks as follows:

--------------
temp 1
temp 2
--------------

Hope this makes sense!
Tom






Similar Threads
Thread Thread Starter Forum Replies Last Post
Populate combobox from ADO recordset - RESOLVED! robzyc Access VBA 8 May 23rd, 2008 01:07 AM
Recordset retrieving Source ichabob Access VBA 2 December 26th, 2006 12:45 PM
How to bind data from a recordset to a combobox? wayne62682 Access VBA 6 April 2nd, 2006 08:45 PM
Database Printing Recordset VB6 Hondacars VB How-To 1 March 11th, 2006 06:15 AM
load data from combobox VB6 Perseus Beginning VB 6 0 July 30th, 2005 04:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.