passing a control to a function

G

Guest

Hi

I have a block of code that will check & format a phone number. I want the
same code to check the fax #, cell#, etc... i.e. send the text box on the
form to a function and do my checking from there. How do I send a control to
a function?

code:
Sub Phone_Number_AfterUpdate()
number_check (???)
End Sub

function number_check(ctrl as ????)
Do Until InStr(1, ctrl.Value, "(") = 0
ctrl.Value = Left(ctrl.Value, InStr(1, ctrl.Value, "(") - 1) _
& Right(ctrl.Value, Len(ctrl.Value) - InStr(1, ctrl.Value, "("))
Loop
etc...
end function
 
G

Guest

Thanks for the suggestion - I need a little clarification:

Number_Check(me.phone_number) returns error 424 - object required
 
P

(PeteCresswell)

Per yibbit:
Number_Check(me.phone_number) returns error 424 - object required

What is .phone_number?

Might it be a text box that is (unfortunately....) named the same as it's
..ControlSource?


If so, try renaming the text box to something like .txtPhoneNumber and see if it
works.
 
G

Guest

good suggestion - and you're right in that it was named the same (that's the
default when you drag & drop from the field list. I changed the control name
to "phone" and called the function as

sub phone_after_update
Number_check(me.phone)
end sub

with the same error as the result.
 
D

Douglas J. Steele

Did you remember to change your function declaration to

function number_check(ctrl as Control)

?

Of course, I'm not sure your function is going to work.

You've got a loop "Do Until InStr(1, ctrl.Value, "(") = 0", but no where in
the loop to you ever get rid of the initial ( if there is one, so you have
an infinite loop.

Why bother passing the control, though?

What not simply pass it the value in the control, and have it return the
corrected value?


Sub Phone_Number_AfterUpdate()
Me.Phone_Number = number_check (Me.Phone_Number)
End Sub

function number_check(PhoneNumber As String) As String
number_check = PhoneNumber
Do Until InStr(1, number_check, "(") = 0
number_check = Left(number_check, InStr(1, number_check, "(") - 1) _
& Right(number_check, Len(ctrl.Value) - InStr(1, number_check,
"("))
Loop
etc...
end function

(sorry, I kept your incorrect code in number_check...)
 
G

Guest

The function works as intended (I didn't include all of it in the post) but
why I didn't think of just passing it the string - thanks for pointing out
the super-duper obvious.
Sigh.
 

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