Results 1 to 10 of 10

Thread: [RESOLVED] Combo Box

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Resolved [RESOLVED] Combo Box

    Hi,

    On my form I have a combo box called 'cmbtraininglocation'. This field can be left blank on first form upload as training location is not yet selected.

    At my backend SQL I have a table called -

    dbo.tbltraininglocType

    LocationID (PK) LocationName
    1 New York
    2 London
    3 Toronto


    Now, I called a dataset to populate the combo box on form load..

    The problem is if the field was not populated at the first instance it populates New York as it is the first record on the table.

    Note: the combo box has a dropdownstyle = DropDownList..

    Ok performed a work around


    LocationID (PK) LocationName
    1 New York
    2 London
    3 Toronto
    4

    New locationId = 4 with empty string, then I ordered by LocationName.. This resolved the problem and on form load if the combo box is emoty at the backend I do get an empty combo box.

    However, the combo box appears like

    Empty Space
    London
    New York
    Toronto

    The annoying bit is the Empty space, which serves the purpose, but also as it is top on the list of selection will ignite wrong selection in some occassions. Also, having an ampty space in the backend table is NOT neat..

    Any suggestions please..

    Many thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,761

    Re: Combo Box

    Why not just have:

    London
    New York
    Toronto
    Training

    That away when you sort it, it should appear at the bottom of the list and in your database you'll instantly recognize what it is(as opposed to just an empty space).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Combo Box

    I did not know which is your back end , how ever i would like to say that
    (1) don't add empty string in the records ( LocationName ) for this purpose

    (2) if you want the specific LocationName on top of the ORDER , then REORDER the default ORDER BY in your SQL statement which loads the combobox
    i,e In the below example ordering is shown
    vb.net Code:
    1. SELECT
    2. traininglocation.LocationId,
    3. traininglocation.LocationName
    4. FROM
    5. traininglocation
    6. ORDER BY
    7. CASE WHEN traininglocation.LocationName = "INDIA" THEN -1 ELSE traininglocation.LocationId END
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box

    Ok Tried;

    Code:
    SELECT        LocationID, TrainingLoc
    FROM            tblTrainingLocType
    ORDER BY CASE WHEN dbo.tblTrainingLocType.TrainingLoc = "London" THEN - 1 ELSE dbo.tblTrainingLocType.LocationID END
    At runtime, I receive the error - Inavlid Column Name 'London'

    Please help - Thank you ...

  5. #5
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Combo Box

    read post #3
    I did not know which is your back end , how ever i would like to say that
    (1) don't add empty string in the records ( LocationName ) for this purpose
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Combo Box

    i doubt the miss spell of the filed names
    i reproduce the same query as per your table just try it as it is

    SELECT
    tblTrainingLocType.LocationID ,
    tblTrainingLocType.TrainingLoc
    FROM
    tblTrainingLocType
    ORDER BY
    CASE WHEN tblTrainingLocType.TrainingLoc = "London" THEN - 1 ELSE tblTrainingLocType.LocationID END
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Combo Box

    Hi,

    You have both made the same simple mistake in that SQL code. You need to use the Single Quote character in that Case statement and not the Double Quote character.

    Err?, I have just quickly read the rest of this thread and if I understand what you are wanting to do correctly, just set the SelectedIndex property of the ComboBox to -1 in the FormShown event to make sure that nothing is selected in the ComboBox after the Form has loaded.

    Hope that helps.

    Cheers,

    Ian

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box

    Ok -

    This works but still I dont get my empty space on the top of th ecombo box instead I receive London on Form Load

    Code:
    SELECT 
    tblTrainingLocType.LocationID ,
    tblTrainingLocType.TrainingLoc 
    FROM 
    tblTrainingLocType
    ORDER BY 
    CASE WHEN tblTrainingLocType.TrainingLoc = "London" THEN - 1 ELSE tblTrainingLocType.LocationID END
    I know I can use selectedindex = -1 onmn form load to have an ampty space, but when I click th eedit button. The user should be able to insert an empty string as location is not allocated a s yet.. My dropdownlist combobox MUST have an empty option..

    Adding an empty string on the table at the backend doesnt look good..

    Any ideas

    Thank you

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,541

    Re: Combo Box

    Oi....

    1) Remove the empty row from your table... it should have never been there in the first place.
    2) Re-write the query as follows:
    Code:
    SELECT -1 as LocationID , '' As LocationName
    UNION ALL
    SELECT
    traininglocation.LocationId,
    traininglocation.LocationName
    FROM
    training
    Order by 1
    Note: that is TWO single quote marks... not a single double... muy importante!

    This will give you a result where the blank floats to the top, and the rest remain in their ID order.

    Not sure what it was everyone else was trying to do with a CASE in the ORDER clause - it only affected one row anyways, and not even the row that was needing manipulation. But since the blank record should never have existed in the first place, it's moot.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: Combo Box

    Resolved.. Thank you guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width