[AccessD] Checking for Optional Parameters

Bill Benson bensonforums at gmail.com
Wed Jan 25 08:00:21 CST 2023


Won’t lie, I get into trouble with them sooner or later. But adding them in
a pinch has saved me rewriting large procs and functions many a time. I use
at most 2 if possible.

I do like to keep a optional boolean parameter for closing files or
quitting Excel instances etc in procedures where I have opened something
like Excel or a logfile and am holding the object in a Static variable. If
I pass True, it closes or quits or in some manner resets, and usually that
is all it does. Something luke this (from memory and is air code):

Function WriteMsgToLog(str as String, Optional blnCloseFile as
Boolean=False)

Static objText as FileStream

On Error Resume Next
If blnCloseFile and Not objText Is Nothing Then
     objText.Close
     Set objText = Nothing
Else
     If objText Is Nothing Then
         Set
objText=CreateObject(“Scripting.FileSystemObject”).CreateTextFile(Filename:=“C:\Users\”
& Environ(“username”) & “\Documents\LogFile.txt”, Overwrite:=True)
     End If
      objText.Write str
End IF
End Function

On Wed, Jan 25, 2023 at 7:03 AM Jim Dettman via AccessD <
accessd at databaseadvisors.com> wrote:

> Arthur,
>
>  I do not.
>
>  I like running down the list of arguments in order as I type and follow
> the
> intelligence.   If I don't like the order for some reason, I do a Find &
> Replace and change it.
>
>  I typically do not use a lot of optional parameters either. I find that if
> I'm using a procedure so many ways that it requires more than a few
> optional
> parameters, then I probably need another procedure.   I find having a lot
> of
> optional arguments makes a procedure more complex to write and understand,
> and more difficult to test and debug.
>
>  It's the same concept as normalization with records in a RDBMs.  If the
> records in a table are different "shapes" (you leave some fields null in
> some records because they don't apply at all rather simply not knowing the
> value), then you need to breakout some of the records into another table.
> Likewise with a procedure, if you are trying to do twenty different things
> with it, then you need to ask yourself if you shouldn't have twenty
> different procedures.
>
>   And of course, I'm talking about VBA here, which doesn't allow
> overloading
> of procedures.
>
> Jim.
>
> -----Original Message-----
> From: AccessD On Behalf Of Arthur Fuller
> Sent: Tuesday, January 24, 2023 9:00 PM
> To: Access Developers discussion and problem solving
> <accessd at databaseadvisors.com>
> Subject: Re: [AccessD] Checking for Optional Parameters
>
> Hi John et. al.,
>
> Actually I knew that already. What I was trying to ask is whether you folks
> use them in practice, or not.
>
> On Tue, Jan 24, 2023 at 5:21 PM John Colby <jwcolby at gmail.com> wrote:
>
> > Named params allow you to pass them in any order because you now use the
> > syntax "ParamA - 1234", IOW you can actually tell the function which
> params
> > you are passing in what order by specifying the name.
> >
> > On Tue, Jan 24, 2023 at 7:47 AM Arthur Fuller <fuller.artful at gmail.com>
> > wrote:
> >
> > > I keep forgetting which check to use to determine whether or not an
> > > optional parameter was passed to a function.
> > >
> > > <code>
> > > Function myFunc( Optional d as Date ) as <whatever>
> > > 'is it:
> > > If IsMissing( d) or
> > > If IsNull(d) or ?
> > > </code>
> > >
> > > Side question: As a matter of coding style, do you use named parameters
> > in
> > > your own functions (i.e. as in, say, a call to MsgBox, which gives you
> > that
> > > prompt that walks you through the syntax as you type)?
> > >
> > > --
> > > Arthur
> > > --
> > > AccessD mailing list
> > > AccessD at databaseadvisors.com
> > > https://databaseadvisors.com/mailman/listinfo/accessd
> > > Website: http://www.databaseadvisors.com
> > >
> >
> >
> > --
> > John W. Colby
> > Colby Consulting
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
>
>
> --
> Arthur
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


More information about the AccessD mailing list