-
Dec 4th, 2017, 03:28 PM
#1
Thread Starter
Enjoy the moment
[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
-
Dec 4th, 2017, 03:35 PM
#2
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
-
Dec 4th, 2017, 03:35 PM
#3
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.
-
Dec 4th, 2017, 04:04 PM
#4
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")
-
Dec 5th, 2017, 12:40 AM
#5
Thread Starter
Enjoy the moment
Re: how do i load all citys to 5 combobox calling a function only
I'm assuming he IS doing it this way...
exactly
-
Dec 5th, 2017, 01:03 AM
#6
Thread Starter
Enjoy the moment
Re: how do i load all citys to 5 combobox calling a function only
Originally Posted by Arnoutdv
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
-
Dec 5th, 2017, 02:26 AM
#7
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
-
Dec 5th, 2017, 02:40 AM
#8
Thread Starter
Enjoy the moment
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
-
Dec 5th, 2017, 02:45 AM
#9
Re: how do i load all citys to 5 combobox calling a function only
Originally Posted by salsa31
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.
-
Dec 5th, 2017, 03:56 AM
#10
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.
-
Dec 5th, 2017, 06:27 AM
#11
Thread Starter
Enjoy the moment
Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only
thnk you chris & eduardo
-
Dec 5th, 2017, 07:56 AM
#12
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
-
Dec 5th, 2017, 09:41 AM
#13
Thread Starter
Enjoy the moment
Re: [RESOLVED] how do i load all citys to 5 combobox calling a function only
Originally Posted by SamOscarBrown
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
-
Dec 5th, 2017, 01:40 PM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|