If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
ByRev / ByVal :: function needs to return value unchanged
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? -- Clif |
#2
|
|||
|
|||
ByRev / ByVal :: function needs to return value unchanged
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). -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada |
#3
|
|||
|
|||
ByRev / ByVal :: function needs to return value unchanged
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 On Apr 8, 10:01*am, "Albert D. Kallal" wrote: 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 |
#4
|
|||
|
|||
ByRev / ByVal :: function needs to return value unchanged
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! -- Clif On Apr 8, 10:01*am, "Albert D. Kallal" wrote: 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. snip 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). -- Albert D. Kallal * *(Access MVP) Edmonton, Alberta Canada |
Thread Tools | |
Display Modes | |
|
|