Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fill combobox in one shot only 2

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
422
IT
Possible to fill a combobox with this array value?

Set RS = New ADODB.Recordset
RS.CursorLocation = adUseClient
SQL = "SELECT * FROM COMUNI UNION SELECT * FROM STATI"
RS.Open Source:=SQL, _
ActiveConnection:=CON, _
CursorType:=adOpenForwardOnly, _
LockType:=adLockReadOnly
RS.Sort = ("PROVINCIA, DESCRIZIONE")

RS.MoveFirst
Erase strDBRows()
strDBRows = RS.GetRows()
RS.Close
Set RS = Nothing

Me.CCOMNASC.List = strDBRows<<<<
 
Well, in theory, yes. But it has been a long time since I tried it in VB6, and I r3ecall that, even if using the dataenvironment, it was a real pain to get working properly (there may be others in this forum who have more expertise in using VB6 databound controls).

Frankly you'll be better off writing a simple routine that iterates through the rowset and adds each item individually to the combo
 
I strongm,
perhaps i can us a datacombo instead?
I have see is possible to fill it in one shot only or not?
 
In theory, with both the standard combo box and the datacombo, it is supposed to be pretty much as simple as

Set mycombo.DataSource = myrs

Theoretically, there's only a tiny bit of additional config you need to do - but for me this only ever ends up populating the text part of the combobox, never the dropdown. I can only ever get it working if I correctly set up in a dataenvironment.

And most examples you will find on the internet also support the view that it is easiest to iterate through the recordset.

However, VB6 databinding isn 'rt an area I ever spent a lot of time on even when VB6 was a major language, so it may be there is somneone else in this forum who can provide different advice.

 
>fill combobox in one shot only
I take it you want to populate a combo box in one line of code. I do this, kind of... And a little bit more.

I keep all my SQLs a (global *) variable [tt]gstrErrSQL[/tt] And sometimes when the outcome of my Select returns just one record, I want to show it in a TextBox instead of ComboBox so the user knows there is nothing else to select. ComboBox is placed over the TextBox, both have the same dimensions and Top and Left properties.
And sometimes (well, most of the times) I also want to deal with the ItemDate of the ComboBox. So, I made a deal with myself (a binding Contract [wink]) to have first field (column) of my recordset of what I want to display in a combobox, and (most of the time) second field is what I use as a ItemData of the combobox.

Code:
gstrErrSQL = "Select CityName, CityNo From Cities Order By CityName"[blue]
Call PopulateCombo(cboCity, True, txtCity)[/blue]

Private Sub PopulateCombo(ByRef cbo As ComboBox, ByRef blnItemD As Boolean, _
    Optional ByRef txtBox As TextBox = Nothing)
Dim recC As New ADODB.Recordset

cbo.Clear

With recC
    .Open gstrErrSQL, Cn
        gstrErrSQL = vbNullString
    
    Do While Not .EOF
        cbo.AddItem .Fields(0).Value
        If blnItemD Then
            cbo.ItemData(cbo.NewIndex) = .Fields(1).Value
        End If
        .MoveNext
    Loop

    .Close
End With
Set recC = Nothing

With cbo
    If .ListCount > 0 Then
        .ListIndex = 0
    End If
    .Visible = True
    If Not txtBox Is Nothing Then
        If .ListCount = 1 Then
            txtBox.Text = .List(0)
            txtBox.Visible = True
            .Visible = False
        Else
            txtBox.Visible = False
        End If
    End If
End With
So, in an essence, I have [blue]one line of code[/blue] that I use to populate my ComboBoxes.

* I am not a fun of Global variables, but they do have their uses when used correctly.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
:) Sort of - but your

Code:
[COLOR=blue]    Do While Not .EOF
        cbo.AddItem .Fields(0).Value
        If blnItemD Then
            cbo.ItemData(cbo.NewIndex) = .Fields(1).Value
        End If
        .MoveNext
    Loop[/color]

is a version of exactly what I was advising, and close to what I came up with myself ...
 
True. [thumbsup]
My point was - there is a lot more you can do if you write the logic yourself rather than relying on build-in functionality.
And yes -
strongm said:
you'll be better off writing a simple routine that iterates through the rowset and adds each item individually to the combo

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
HI strongm,
instead
Set mycombo.DataSource = myrs

possible to set the rowsource with the strDBRows = RS.GetRows()?

or with an aray?

????
 
No

I'd have said if it was.

It really pays to read the documentation, but appreciate that if you don't have a proper, full install of VB6 which includes the help files, that these can be difficult to find these days. So here's an extract:

VB6 Documentation said:
Syntax

object.DataSource [=datasource]

The DataSource property syntax has these parts:

[pre]Part Description[/pre]
[pre]object An object expression that evaluates to an object in the Applies To list.
datasource An object reference that qualifies as a data source, including ADO Recordset objects, and classes or user controls defined as data sources (DataSourceBehavior property = vbDataSource).[/pre]

Neither an array nor your strDBRows qualify as a legitimate datasource
 
nice explain. Tks.
i dpont have the guide instlled on my VB6, i can dowload? Where?


in other case how to load into myarray, the value in strDBRows = RS.GetRows()????

at to the end loop the array?
 
>Where?

Should be on your VB6 CDs

Alternatively, the last comprehensive set of help files made available by Microsoft for VB6 that could be integrated with the IDE were on the October 2001 MSDN 3 CD set. If you have a current Visual Studio subscription (which superseded the MSDN subscription), you should have access to these and be able to download them (unfortunately the free Visual Studio subscription does not provide access to such old content; you need the paid subscription). There may be other sources, but I cannot provide a link.
 
>in other case how to load into myarray, the value in strDBRows = RS.GetRows()????

>at to the end loop the array?

Er, the result of

RS.GetRows()

is already an array. So I am not quite sure I understand what you are asking here.

 
STRONGM,
i'm study...

but how to clear all item in the datacombo1?
 
Did you try:

Code:
Set DataCombo1.DataSource = Nothing

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top