PC Review


Reply
Thread Tools Rate Thread

Pass Control as Argument

 
 
Clyde
Guest
Posts: n/a
 
      23rd Feb 2010
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.
--
Clyde
 
Reply With Quote
 
 
 
 
Allen Browne
Guest
Posts: n/a
 
      24th Feb 2010
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

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Clyde" <(E-Mail Removed)> wrote in message
news:0B7F74D3-7870-4AEB-B52A-(E-Mail Removed)...
> 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.
> --
> Clyde


 
Reply With Quote
 
 
 
 
Clyde
Guest
Posts: n/a
 
      24th Feb 2010
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.
--
Clyde


"Allen Browne" wrote:

> 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
>
> --
> Allen Browne - Microsoft MVP. Perth, Western Australia
> Tips for Access users - http://allenbrowne.com/tips.html
> Reply to group, rather than allenbrowne at mvps dot org.
>
>
> "Clyde" <(E-Mail Removed)> wrote in message
> news:0B7F74D3-7870-4AEB-B52A-(E-Mail Removed)...
> > 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.
> > --
> > Clyde

>
> .
>

 
Reply With Quote
 
Allen Browne
Guest
Posts: n/a
 
      25th Feb 2010
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.

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.


"Clyde" <(E-Mail Removed)> wrote in message
news:4684F9AE-028D-4BAF-8170-(E-Mail Removed)...
> 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.
> --
> Clyde
>
>
> "Allen Browne" wrote:
>
>> 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
>>
>> --
>> Allen Browne - Microsoft MVP. Perth, Western Australia
>> Tips for Access users - http://allenbrowne.com/tips.html
>> Reply to group, rather than allenbrowne at mvps dot org.
>>
>>
>> "Clyde" <(E-Mail Removed)> wrote in message
>> news:0B7F74D3-7870-4AEB-B52A-(E-Mail Removed)...
>> > 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.
>> > --
>> > Clyde

>>
>> .
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Throwing Argument exception when method argument is a Guid Andy B Microsoft VB .NET 2 11th Jan 2009 07:38 PM
Cannot pass argument from control to sub and back Revolvr Microsoft Excel Programming 4 18th Sep 2008 06:46 PM
Function (array argument, range argument, string argument) vba Witek Microsoft Excel Programming 3 24th Apr 2005 03:12 PM
How to pass an Excel range as an argument to a SQL Server stored Procedure Belinda Microsoft Excel Programming 7 8th Apr 2004 11:24 AM
How to overcome the limitation: Cannot pass 'argument' as ref or out, because ' argument ' is a marshal-by-reference class Mountain Bikn' Guy Microsoft C# .NET 2 15th Nov 2003 08:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:59 AM.