Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

combobox initial value...

Mini_MaggitMini_Maggit Posts: 28Member
Hi

I've populated my combobox with data out of a database using my dataset... There's no problem there, the thing is, on form load, I want the initial value of the combobox (ie the thing that would be Combobox1.Text if it wasn't populated with data), when it isn't dropped down to be nothing or some text such as "Select a category..."

Any ideas?
TIA

Comments

  • d3v1lmand3v1lman Posts: 66Member
    The simplest way to handle this is to simply set the text property to whatever you want it to be i.e.(if you were populating from a DB):
    [code]
    [blue]Private Sub[/blue] Form_Load()
    [blue]Dim[/blue] conn [blue]As New[/blue] ADODB.Connection
    [blue]Dim[/blue] rs [blue]As New[/blue] ADODB.Recordset
    [blue]Dim[/blue] SQLQuery [blue]As String[/blue]
    SQLQuery = "SELECT DISTINCT fldCategory FROM tblCategories"
    conn.Open [i]connection string[/i]
    rs.ActiveConnection = conn
    rs.Open SQLQuery
    [blue]Do While Not[/blue] rs.EOF
    cboCategory.AddItem rs.Fields("fldCategory").Value
    rs.MoveNext
    [blue]Loop[/blue]
    rs.Close
    conn.Close
    [blue]Set[/blue] rs = [blue]Nothing[/blue]
    [blue]Set[/blue] conn = [blue]Nothing[/blue]
    cboCategory.Text = "Please Select a Category"
    [blue]End Sub[/blue]
    [/code]
    This will fill the combobox with the data from the database, but set the text in the box to be "Please Select a Category", even though it is not an item that has been added to the list.
    You were on the right track, but the answer really does seem to be too simple... It never hurts to just sit and play with the properties of your objects. Sometimes that is the best way to learn and increase your skills.


    To ERR is human, but to really f**k something up requires a computer...

  • d3v1lmand3v1lman Posts: 66Member
    Sorry, this code is in VB6, but the concepts are still the same. The code example should still work (if you are using ADO and not ADO.NET!)
  • GogiGogi Posts: 258Member
    : Hi
    :
    : I've populated my combobox with data out of a database using my dataset... There's no problem there, the thing is, on form load, I want the initial value of the combobox (ie the thing that would be Combobox1.Text if it wasn't populated with data), when it isn't dropped down to be nothing or some text such as "Select a category..."
    :
    : Any ideas?
    : TIA
    :

    ComboBox1.Items.Add ("Select a cathegory")
    ComboBox1.Text="Select a cathegory"

    P.S.I'm not sure if this will work,didn't test it. ;-)
Sign In or Register to comment.