×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

fill combobox in one shot only
2

fill combobox in one shot only

fill combobox in one shot only

(OP)
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<<<<

RE: fill combobox in one shot only

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

RE: fill combobox in one shot only

(OP)
I strongm,
perhaps i can us a datacombo instead?
I have see is possible to fill it in one shot only or not?

RE: fill combobox in one shot only

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.

RE: fill combobox in one shot only

>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 gstrErrSQL 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"
Call PopulateCombo(cboCity, True, txtCity)

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 one line of code 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

RE: fill combobox in one shot only

smile Sort of - but your

CODE -->

    Do While Not .EOF
        cbo.AddItem .Fields(0).Value
        If blnItemD Then
            cbo.ItemData(cbo.NewIndex) = .Fields(1).Value
        End If
        .MoveNext
    Loop 

is a version of exactly what I was advising, and close to what I came up with myself ...

RE: fill combobox in one shot only

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 -

Quote (strongm)

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

RE: fill combobox in one shot only

(OP)
HI strongm,
instead
Set mycombo.DataSource = myrs

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

or with an aray?

????

RE: fill combobox in one shot only

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:

Quote (VB6 Documentation)


Syntax

object.DataSource [=datasource]

The DataSource property syntax has these parts:

Part          Description 
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). 

Neither an array nor your strDBRows qualify as a legitimate datasource

RE: fill combobox in one shot only

(OP)
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?

RE: fill combobox in one shot only

>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.

RE: fill combobox in one shot only

>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.

RE: fill combobox in one shot only

(OP)
STRONGM,
i'm study...

but how to clear all item in the datacombo1?

RE: fill combobox in one shot only

Did you try:

CODE

Set DataCombo1.DataSource = Nothing 

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close