Results 1 to 14 of 14

Thread: [RESOLVED] how do i load all citys to 5 combobox calling a function only

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Resolved [RESOLVED] how do i load all citys to 5 combobox calling a function only

    hey,
    i have 5 combox that i load same values.
    now each form the i have this combobox i put a very long code
    to load the citys.
    my questions is can i make a single function that will do the job for me
    instead of having to copy and paste the same code but in a diffrent form?
    tnx for any help
    salsa

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,871

    Re: how do i load all citys to 5 combobox calling a function only

    Code:
    Private Sub FillCityCombo(oCombo As ComboBox)
      With oCombo
      ' your code
      End With
    End Sub
    
    Private Sub FillComboBoxes()
      FillCityCombo ComboBox1
      FillCityCombo ComboBox2
      ' etc etc
    End Sub

  3. #3
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,392

    Re: how do i load all citys to 5 combobox calling a function only

    Very long code? How are you storing the cities? Not hard-coded I hope.

    For example, if you store the cities in a simple text file, 1 line per city, loading them into a Combo Box should be a few lines of code at most.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: how do i load all citys to 5 combobox calling a function only

    Very long code? How are you storing the cities? Not hard-coded I hope
    I'm assuming he IS doing it this way...

    Code:
    Combo1.Additem("Madrid")
    Combo1.Additem("Barcelona")
    .
    .
    .
    Combo1.Additem("Valencia")

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i load all citys to 5 combobox calling a function only

    I'm assuming he IS doing it this way...
    exactly

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i load all citys to 5 combobox calling a function only

    Quote Originally Posted by Arnoutdv View Post
    Code:
    Private Sub FillCityCombo(oCombo As ComboBox)
      With oCombo
      ' your code
      End With
    End Sub
    
    Private Sub FillComboBoxes()
      FillCityCombo ComboBox1
      FillCityCombo ComboBox2
      ' etc etc
    End Sub
    dosnt work for me
    i put this in a module
    Code:
    Public Sub FillCityCombo(oCombo As ComboBox)
     With oCombo
        oCombo.Additem("Barcelona")
        oCombo.Additem("Madrid")
      End With
    End Sub
    this is call from a form
    Code:
    Private Sub FillComboBoxes()
        FillCityCombo CmbCity
    End Sub
    i get error type mismatch

  7. #7
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,871

    Re: how do i load all citys to 5 combobox calling a function only

    This works fine for me...
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      FillComboBoxes
    End Sub
    
    Private Sub FillComboBoxes()
      FillCityCombo Combo1
      ' etc etc
    End Sub
    
    Private Sub FillCityCombo(oCombo As ComboBox)
      With oCombo
        .AddItem "Barcelona"
        .AddItem "Madrid"
        .ListIndex = 1
      End With
    End Sub

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: how do i load all citys to 5 combobox calling a function only

    you are right it is working with a regular combobox
    thank you

  9. #9
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: how do i load all citys to 5 combobox calling a function only

    Quote Originally Posted by salsa31 View Post
    you are right it is working with a regular combobox
    thank you
    Change:

    Code:
    Private Sub FillCityCombo(oCombo As ComboBox)
    to:

    Code:
    Private Sub FillCityCombo(oCombo As Object)
    and it will work with other conboboxes types.

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only

    Hi salsa,

    sure is strange to Hardcode the Cities. You do work with Databases as I can remember.


    well here another way...
    in a Modul..
    Code:
    Option Explicit
    
    Public strCity(5) As String
    
    
    Public Sub CityList()
    strCity(0) = "Madrid": strCity(1) = "Barcalona": strCity(2) = "Valencia"
    strCity(3) = "Berlin": strCity(4) = "Frankfurt": strCity(5) = "München"
    End Sub
    in the Form..
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
     Dim i As Integer
        With cboCity
        .Clear
        For i = 0 To 5
        .AddItem strCity(i)
        Next
        End With
        cboCity.ListIndex = 0
    End Sub
    
    
    Private Sub Form_Load()
     
     Call CityList
    
    End Sub
    is there a reason why not a Database Table ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only

    thnk you chris & eduardo

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only

    Hey salsa...here is an Access Database with a table of a few of the larger cities in Spain. You could take it, add to it, and use it to fill your combobox(es).

    Samm

  13. #13

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only

    Quote Originally Posted by SamOscarBrown View Post
    Hey salsa...here is an Access Database with a table of a few of the larger cities in Spain. You could take it, add to it, and use it to fill your combobox(es).

    Samm
    thank you sami
    i appreciate that

  14. #14
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only

    if you don't want to use an external database, here's another take:

    Code:
    Public Sub FillCombos(Combo, Optional ByVal Clear As Boolean, Optional ByVal index&)
        Const Cities$ = "Madrid,Barcelona,Valencia"
        Dim City
    
        If Clear Then Combo.Clear
        For Each City In Split(Cities, ",")
            Combo.AddItem City
        Next
        Combo.ListIndex = index
    End Sub
    and you call it, FillCombos Combo1 or List1 if you use Listbox for any combo you want.
    you can also Clear the Combo and select what city to be selected at start.

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