Update textbox via subroutine call

  • Thread starter Thread starter Steve S
  • Start date Start date
S

Steve S

I need to update a field (textbox) on several very similar forms. To reduce
the amount of code and to preclude having duplicate code to do the
calculations in each form I want to use a subroutine to perform the
calculations.

I want to call the subroutine with several parameters and have it return the
result directly to the form. A very stripped down (simplified) sample is
shown below. Text4 is the field I want to update

Private Sub Command6_Click()
subTestCall 2, 3, Me.Text4
MsgBox Me.Text4
End Sub
--------------------------------------------------------------------------

Public Sub subTestCall(varA As Byte, varB As Byte, ByRef varC As Byte)
varC = varA + varB
MsgBox varC
End Sub
---------------------------------------------------------------------

MsgBox varC shows a value of 5 as expected

But MsgBox Me.Text4 shows that Text4 still contains 1 which is the initial
value of Text4 before the call.

What do I have to do to get Text4 to update via the returned parameter in
the call??
 
try

Private Sub Command6_Click()
Me!Text4 = subTestCall(2, 3)
MsgBox Me.Text4
End Sub
--------------------------------------------------------------------------

Public Function subTestCall(ByVal varA As Byte, ByVal varB As Byte) As Byte
subTestCall = varA + varB
End Sub

hth
 
Thanks Tina,

Your suggestion would work great in the simplified example I provided but
what I actually need is to be able to returen several (4-5) values to the
form instead of just one. With this solution I would need to make 4-5
function calls.

Thereal question is: Why doesn't the field on the form update if I am
passing the parameter via 'ByRef' which is the default anyway in access. As
I understand it the 'byRef' option in Access passes the address of the
parameter so it can be changed within the called subroutine. To prevent the
possibility of the source being changed I whould use 'ByValue'. This is
common functionality even in much older (non event driven) programming
languages such as Fortan, Pascal, Cobol, PL1,,etc.

With your help and that of several members in a local users group I have
developed 3 or 4 alternative solutions but the core question is: Can you
update fields on a form with values returned via s subroutine. From what I
have read, and considering the 'byRef' and byValue' options, the answer seems
to be YES but I can't get it to work.

any and all help is appreciated.
 
Thereal question is: Why doesn't the field on the form update if I am
passing the parameter via 'ByRef' which is the default anyway in access.

yes, i saw that and wondered if that was specifically what you were trying
to do. i answered as i did, on the off-chance that it wasn't (it's hard to
determine an op's skill level in the initial post, sometimes; i don't always
guess right). and you're right, AFAIK, ByRef should result in a change to
the value of the source cited in the reference. i don't use ByRef, so i
don't have any practical experience with it, and so don't know why it's not
working that way, or if i'm misunderstanding the premise.

at any rate, an alternative would be to pass the control reference itself,
not a reference to the value in the control, as

Private Sub Command6_Click()
subTestCall 2, 3, Me!Text4
MsgBox Me.Text4
End Sub
--------------------------------------------------------------------------

Public Sub subTestCall(varA As Byte, varB As Byte, ByVal ctl As Control)
ctl = varA + varB
End Sub

if subTestCall is running in a standard module, you may have to also pass a
form reference, or include the full syntax of the control. sorry, i'm not
too wide awake right now, so i'm drawing a blank. just start with the
simplest argument above, and if it doesn't work then fiddle with it until
Access recognizes the control after it's passed into the procedure.

hth
 
Back
Top