Simple Subroutine Problem

D

Dan Nawrocki

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
 
E

Ebbe

Try:
--------------------------------------
Option Explicit

Private Sub CommandButton1_Click()
AddToList ListBox1

End Sub

Private Sub AddToList(lb As ListBox)
lb.AddItem ("asdf")
End Sub
 
D

Dan Nawrocki

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???
 
E

Ebbe

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
 
D

Dan Nawrocki

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
 
V

Vince

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?
 
D

Dan Nawrocki

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
 
E

Ebbe

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top