-
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
-
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.
-
Is the AddListItems subroutine in the same form as cmbstuff? If not, you need to put AddListItems in a module and call it like AddListItems(Form1.cmbstuff)
-
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.
-
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]
-
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
-
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.