CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: combo box

  1. #1
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    combo box

    I have a combo box that displays an id number and a name. I need to just be able to read the id number in an if statement. Ex.
    If cmbSite.text = 7 then etc.
    Obviously this does not work, because it tries to read the whole box.
    Anybody...Thanks


  2. #2
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: combo box

    Hi,

    To get only the number part of a string (cmbSite.text), you can use Mid function. But I prefer using the ItemData property to hold the IDs. For example:

    ===
    With cmbSite
    .Clear

    .AddItem ("Site1")
    .ItemData(.NewIndex) = 7

    .AddItem ("Site2")
    .ItemData(.NewIndex) = 8


    ' You can add more
    End With

    ===

    Then, to read the id, you can use the following statement:

    If cmbSite.ItemData(cmbSite.ListIndex)= 7 then etc.

    Hope this helps.



    Regards,

    Michi

  3. #3
    Join Date
    Sep 2001
    Location
    South Dakota
    Posts
    20

    Re: combo box

    Thanks for your help.
    But, here's the deal--the combo box is populated by adding sites into the database from another form in the program. Will this work with that?


  4. #4
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: combo box

    The site name and site ID in the codes can be replaced by database field values. You can create an ADO record set to loop through.

    Regards,

    Michi

  5. #5
    Join Date
    Sep 2001
    Location
    IL, USA
    Posts
    1,090

    Re: combo box

    The code below show how to extract the ID from the combo text property. You should add some error handling and tighten the contraints but the code below should get you started.

    private Sub Command1_Click()
    If GetID <> -1 then
    MsgBox GetID
    End If
    End Sub
    Function GetID() as Long
    Dim ID as string, Islong as Boolean, Index as Long
    Index = 0
    Islong = IsNumeric(Left(LTrim(Combo1.Text), Index + 1))
    While Islong
    Index = Index + 1
    Islong = IsNumeric(mid(LTrim(Combo1.Text), Index + 1, 1))
    Wend
    If Index > 0 then
    GetID = CLng(LTrim(Left(Combo1.Text, Index)))
    else
    GetID = -1
    End If
    End Function






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

Featured