ByRev / ByVal :: function needs to return value unchanged

C

Clif McIrvin

Passing a function argument by value is not doing what I expected it
to (all fields in this code are unboundtext or
combo boxes):

I expect

Me.JobID = "ABC" '(simulate user input)
Me.JobID = EnterInit(Me.JobID)

to leave JobID with the value of "ABC", but it returns as Null.


Private Function EnterInit(ByVal vValue As Variant) As Variant
rem v = vValue
Me.ContractorFind = Null
Me.JobID = Null
Me.JobIDPart = Null
Me.JobName = Null
Me.JobNamePart = Null
ReadyToFind = True
eiReturn:
EnterInit = vValue 'return parameter to caller
rem EnterInit = v 'return parameter to caller
End Function


By single-stepping through the function with the locals window
open I observed that vValue changed from "ABC" to Null when
the Me.JobID = Null statement was executed -- which I would
expect if passing by reference.


My soulution was to add the two lines of code I have shown as 'rem'.


Am I missing something here?
 
A

Albert D. Kallal

The problem is you're not actually passing the value of the control to that
function, you're actually passing a *control* to that function!

You *do not* want to do that.

In *most* cases, when developing under MS access, the default property of a
control is .value.

However if the receiving sub/function, or variable assignment of the control
is to some object type (or variant) is done, you're not actually assigning
the *value* but are in fact assign the *control*.

eg:

dim myContorl as contorl

me.LastName = "hello"

myContorl = me.LastName

Msgbox "name of control is lastname " & MyContorl.Name

in your sample code, try:

Me.JobID = "ABC" '(simulate user input)
Me.JobID = EnterInit(Me.JobID.Value)

In fact, more syntax correct would be the following (however I think the
above example is fine):

Me.JobID.value = "ABC" '(simulate user input)
Me.JobID.value = EnterInit(Me.JobID.Value)


In most cases, it ok to use value, but if you adding controls to a
collection, then:

myCollection.Add me.LastName <--- adds the control to the collection,
NOT the *value* of the control.

if you wanted to add just the value, and not a control (with all of its
properties+methods), then:

myCollection.Add me.LastName.Value <--- adds the value of the control
to the collection

I would suggest that if you know the type of value of the control that
you're going to send, I would not use variant for the type declare in that
function. Simply use long integer, string, or whatever data type the control
supposed to be, and then the compiler would have caught this (in fact,
access would likely have "cast" the data type, and it would have been forced
to use the default .value property of the control).
 
C

Clif McIrvin

Sounds like lazy programmer got bit by <assumed> (vs) <explicit> <g>

Your explanation is clear; I'll test it when I get the opportunity.

Thanks!

--
Clif

The problem is you're not actually passing the value of the control to that
function, you're actually passing a *control* to that function!

You *do not* want to do that.

In *most* cases, when developing under MS access, the default property of a
control is .value.

However if the receiving sub/function, or variable assignment of the control
is to some object type (or variant) is done, you're not actually assigning
the *value* but are in fact assign the *control*.

<snip>
 
C

Clif McIrvin

Albert, it works just as you said (but you already knew that, didn't
you?! <g>)

As to specifying type, in this particular instance I was using Variant
so that I could test for Null as a valid condition. There are always
wrinkles, aren't there?

Thanks again!
 

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