Function or Public Sub ?

timothyl

Registered User.
Local time
Today, 03:13
Joined
Jun 4, 2009
Messages
92
I cobbled together my first function two days ago(with help from the form)

Function ClearAll(c As String, f As Form)
Dim ctl As Control
For Each ctl In f.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And ctl.Tag <> c Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl

End Function

then behind a button: ClearAll("c", Me.Form)

When I tried to use it I recivced an error msg: exspected = sign
and...... of course, functions return values, and non are being asked for so an error would be exspected
Next tried: Call ClearAll("c", Me.Form) and this works, so is the above a function or a public sub, and if its a public sub the above imply's that
call function = public sub ?.

In general: A function returns a value and a Public Sub exacutes a procedure, is this right ?. Any help in clearing up my confusion is appriciated
 
for a start try just

ClearAll("c", Me)


also, this may be the culprit

If ctl.ControlSource = "" And ctl.Tag <> "c" Then

a) you definitely need "" round the string value
b) it might be if isnull(ctl.controlsource) instead - not sure offhand

--------
this is pretty advanced actually - passing form and ctrl parameters is not normally the first sort of sub/function you would try!
 
gemma-the-husky, thanks for responding, I put "" around the c, and tried Me did not change the need for the: Call (I left them in the code, thanks for catching that), also tried
if isnull(ctl.controlsource), recived error, then changed to If ctl.ControlSource is Null, which access seemed to like better but still recived error "object required"
 
sub and function are broadly similar, except a function can return a value. You dont have to return a value, so just having the function name works.

eg - you can program any event to run a function

in the event property just put

=functionname() {the brackets arev necessary, and can contain a value - this is how the inbuilt switchboard manager exectues button clicks. These are functions, but dont return a value.

general the syntax is

(the call is optional - its for illustration)
call subname
or
call functionname

but
variable = functionname {assigns the result of calling the function)
(you cannot use a sub in this way)
 
When calling your ClearAll function you can use the

Call ClearAll("c",Me)

which requires parens

or just

ClearAll "c", Me

which does not require parens

and since you were using parens without an equals sign or the call keyword it was erroring.
 
gemma-the-husky, boblarson, thank you, that was most helpful
 

Users who are viewing this thread

Back
Top Bottom