Results 1 to 7 of 7

Thread: Creating sub to fill combobox. Help

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    48

    Question

    Hi all,

    I have a sub which I want to call to fill a combobox. But it doesn't work. Here's the code:
    ---------------------------
    Sub AddListItems(cmbname As ComboBox)

    Dim sTemp As String
    cmbname.Clear

    Open (App.Path & "\list.txt") For Input As #1
    While Not EOF(1)
    Line Input #1, sTemp
    cmbname.AddItem sTemp
    Wend
    Close #1

    End Sub
    ----------------------------

    For example when the combobox I want to fill is named cmbStuff, I would call it AddListItems (cmbStuff)

    It should work right? But it doesn't. It gives me type mismatch.

    If I replace the line
    "Sub AddListItems(cmbname As ComboBox)"
    with
    "Sub AddListItems"
    and change all the "cmbname" with "cmbStuff" in the above code, and call the simply by AddListItems, it works FINE.

    Where is my mistake? Thanks in advance for any help.

    Martin

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    I think...

    why do you have it like this? the way you have it set up..it wants a variable...Is the combobox name going to change all the time?

    if so..maybe try

    Sub AddListItems(cmbname As Object)

    Just a guess though.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    48
    Hmm

    I have it like that because this sub will be implemented in different comboboxes.

    I tried using cmbname as Control/Controls and it did NOT work.

    And yes, they're both on the same form.

    Any clues?

    Thanks for the replies.

  5. #5
    Addicted Member ZanM's Avatar
    Join Date
    Oct 1999
    Location
    The here and now.
    Posts
    191
    Code:
    Public Sub LoadComboFromFile(cbo As ComboBox, Path As String)
        Dim mFile As Integer
        Dim mSTR As String
        mFile = FreeFile
        Open Path$ For Input As #mFile
        While Not EOF(mFile)
            Line Input #mFile, mSTR
            cbo.AddItem mSTR
        Wend
    End Sub
    That should do it (I Know cause I tested it)
    if you don't have VB6 try changing As ComboBox to As Variant

    [Edited by ZanM on 11-07-2000 at 04:53 PM]
    Magiaus
    Visual Basic 6.0 SP5
    Visual C++ 6.0 SP5


    The only sovereign you can allow to rule you is reason.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2000
    Posts
    48
    Thanks for those who replied, but it was a STUPID mistake. I figured it out.

    Instead of calling it: AddListItems (cmbStuff)

    I have to call it: AddListItems cmbStuff

    The parentheses screws up everything for some reason.
    Thanks everyone.

    Martin

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    If you use parenthesis around your parameter, then it means that you're passing it ByVal, therefore it is not a reference anymore. In order for you to have a combobox populated, you have to pass it ByRef (which is without paranthesis). That's why it was screwed up.

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