Pass Control as Argument

C

Clyde

I have a funtion like this:
Function SelectAnItem(By Ref ctl as control, strItem as string)

Have been successfully calling like this:
SelectAnItem(frm!lstModel, strLineItem)
where frm is dimmed as Form and lstModel is a list box.

All of a suddent this code now passes the ctl argument as the value of the
list box and of course the function fails. I have no idea what I changed
where to cause this change in behavior, but I know that ?frm!lstModle
obviously is the value of the selection. Makes me wonder how it ever worked.

In the calling procedure I have tried Set ctl = frm!lstModel (diming ctl as
control) and the result is the same.

So... how do I pass the control frm!lstModel to the function as a control?

Thanks in advance.
 
A

Allen Browne

That should work.

Some things to check:
1. Any broken references?
http://allenbrowne.com/ser-38.html

2. Does your code compile?
(Compile on Debug menu, in the code window.)

3. Try adding the Call keyword, i.e.:
Call SelectAnItem(frm!lstModel, strLineItem)

4. Set a breakpoint immediately before this Call line, and use the Immediate
Window (Ctrl+G) to ask Access what's going on, e.g.:
Debug.Print ctl.Name
Debug.Print ctl.ControlType
 
C

Clyde

Thanks, as usual some slopiness involved.
1. No broken refs (did appreciate your link, cleaned up a few not needed)
2. Yes but see below
3. Have gone back and forth on using "Call". Don't think ever had a
problem with or without. But when I tried inserting Call I got an error
because I really wasn't calling like I posted. My calling line was
SelectAnItem frm!lstModel, strModel. Now it is: Call
SelectAnItem(frm!lstModel, strModel) that got an ok from Access. There were
five or six places I made a call to that function. All like the original.
Some of those calls still worked. At any rate I changed them all to include
Call and (...). Mystery why works on one line and not the next. BTW Access
2007.
Anyway THANKS again.
 
A

Allen Browne

VBA is somewhat inconsistent in the way you call Subs and Functions. The
Call keyword, while optional, can help you avoid problems associated with
those inconsistencies. Your example with/without the brackets is what we're
talking about.
 

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