I'm sure there is a simple solution to this, but I can't find it :(
I have a form with a list box and a button in it. When I hit the button, I
would like to add some text to the list box ("asdf" suffices for now).
However, I would like to add the text from another subroutine, not directly
from the button_click subroutine. My problem is that when I pass the list
box control to the subroutine, it ALWAYS is null. How can I fix this?
Sample code:
Private Sub button_Click()
subroutine (listBoxControl)
End Sub
Private Sub subroutine (lb as ListBox)
lb.AddItem ("asdf")
End Sub
I always get a 424 error on the line "subroutine (listBoxControl)".
However, the following code works as expected:
Private Sub button_Click()
listBoxControl.AddItem ("asdf")
End Sub
I've tried every combination I could think of using the full form name, the
"Me" variable, dots and bangs, but everything I have tried will pass null to
the function.
Thanks,
dan
Ebbe - 29 Mar 2005 21:13 GMT
Try:
--------------------------------------
Option Explicit
Private Sub CommandButton1_Click()
AddToList ListBox1
End Sub
Private Sub AddToList(lb As ListBox)
lb.AddItem ("asdf")
End Sub
--------------------------------------
Ebbe
> I'm sure there is a simple solution to this, but I can't find it :(
>
[quoted text clipped - 29 lines]
> Thanks,
> dan
Dan Nawrocki - 29 Mar 2005 21:27 GMT
Hmm, still doesn't work...
I've also noticed something very strange. When I change the code to look
like:
Private Sub button_Click()
Dim x as ListBox
set x = Me!ListBox1
AddToList x
End Sub
When I hove the mouse over the "x" variable, the tooltip text "x = null"
shows up. Howver, in the local variable debugger window, I can browse all
the properties of the the list box as if it were assigned correctly. What's
going on here???
> Try:
> --------------------------------------
[quoted text clipped - 45 lines]
> > Thanks,
> > dan
Ebbe - 29 Mar 2005 21:37 GMT
A very foolish answer:
Are you sure you spell the name of the listbox correctly?
Some time ago I had a similar problem because of a simple misspelling :-(
Ebbe
> Hmm, still doesn't work...
>
[quoted text clipped - 65 lines]
>> > Thanks,
>> > dan
Dan Nawrocki - 29 Mar 2005 21:57 GMT
Yes, it's spelled right. Too bad it wasn't that :(
I'm starting to wonder if I've got my environment set up wrong - I get a 424
error when trying the code below:
Private Sub button_Click()
Dim x as MyForm
Set x = Forms!MyForm
End Sub
I am trying to add a form and a macro to automate some document generation.
Right now, I am just testing the form by itself, so the macro hasn't come
into play yet. Both the form and macro are being saved in the Normal.dot
template as it needs to be used on several documents. All of this is being
done within Word 2003, and (as far as I know) I haven't changed any of the
default settings. Does anyone have any ideas on what I could do?
Thanks,
dan
> A very foolish answer:
> Are you sure you spell the name of the listbox correctly?
[quoted text clipped - 70 lines]
> >> > Thanks,
> >> > dan
Vince - 30 Mar 2005 06:00 GMT
Not sure if this is going to help but are you sure you are referencing the
Forms 2 .0 object library? (VBE - Tools - References). And the usual VBA /
Office as well?
> Yes, it's spelled right. Too bad it wasn't that :(
>
[quoted text clipped - 91 lines]
> > >> > Thanks,
> > >> > dan
Dan Nawrocki - 30 Mar 2005 17:55 GMT
Yup, it's a simple solution, a problem in how I called the subroutine. By
placing parenthesis around the variable, VBA tried to call the subroutine
passing arguments by value. When I took the parenthesis off, it passed
arguments by reference, which worked. Correct code is:
Private Sub button_Click()
subroutine listBoxControl ' <------ no parenethesis!!
End Sub
Private Sub subroutine (lb as ListBox)
lb.AddItem ("asdf")
End Sub
> I'm sure there is a simple solution to this, but I can't find it :(
>
[quoted text clipped - 25 lines]
> Thanks,
> dan
Ebbe - 31 Mar 2005 05:07 GMT
Hey Dan!
I am sorry, that I didn't mention the parentheses explicitly in my first
proposal.
It was, what I tried to tell you ;-)
I am glad, that you fixed the problem anyway.
Ebbe
> Yup, it's a simple solution, a problem in how I called the subroutine. By
> placing parenthesis around the variable, VBA tried to call the subroutine
[quoted text clipped - 42 lines]
>> Thanks,
>> dan