[AccessD] Closure on the String to Date topic

Stuart McLachlan stuart at lexacorp.com.pg
Tue Nov 27 16:05:17 CST 2012


Such one line functions are very useful where 

1. You are not using them many times in a loop, but multiple times at various places in your 
application

2. You want easy to read code with descriptive function names for maintenance purposes.

3.  You want to save repeatedly typing an long, complicated function  :->   


It would be nice if VBA had Macro capabilities like C / C++ and PowerBasic - to name a few. 
Then you could get the same advantages without the overhead of the function call

I use BoM(Date()) and EOM(Date()) frequently as default values in text boxes on Report 
Menus.   It is a lot more obvious what the code does and the time is irrelevant. (and I am less 
likely to make a typo when entering it <g>)


-- 
Stuart    

On 27 Nov 2012 at 21:50, Salakhetdinov Shamil wrote:

> Hi Arthur and Stuart,
> 
> Here is a bit quicker version of EoM :)
> 
> Public Function EoM2(dt As Date) EoM2 = DateSerial(Year(dt), Month(dt)
> + 1, 0) End FunctionIf a bit more seriously what's the use of such one
> line function(s)? - as you can find from the test results below almost
> 25% of time is spent to pass parameter(s) and to get return value. Of
> course that are just microseconds but imagine you have a lot of such
> "one-liners" and they are called in cycles etc. 
> 
> Thank you.
> 
> -- Shamil
> 
> P.S. Tests results:
> 
> EoM: Elapsed time = 0.140625
> EoM2: Elapsed time = 0.125
> Inline: DateSerial(Year(dt), Month(dt) + 1, 0): Elapsed time = 0.09375P.P.S. Test code:
> 
> Option Compare Database
> Public Sub Test()
> Const MAX_COUNT As Long = 100000
> Dim startTime As Single
> Dim endTime As Single
> Dim counter As Long
> Dim testName As String
> Dim result As Date
> Dim dt As Date
> dt = Now
> ' EoM
> testName = "EoM"
> startTime = Timer
> For counter = 1 To MAX_COUNT
> result = EoM(dt)
> Next counter
> endTime = Timer
> Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
> ' EoM2
> testName = "EoM2"
> startTime = Timer
> For counter = 1 To MAX_COUNT
> result = EoM2(dt)
> Next counter
> endTime = Timer
> Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
> ' Inline: DateSerial(Year(dt), Month(dt) + 1, 0)
> testName = "Inline: DateSerial(Year(dt), Month(dt) + 1, 0)"
> startTime = Timer
> For counter = 1 To MAX_COUNT
> result = DateSerial(Year(dt), Month(dt) + 1, 0)
> Next counter
> endTime = Timer
> Debug.Print testName & ": Elapsed time = " & (endTime - startTime)
> End Sub
> Public Function EoM(dt As Date)
> EoM = DateSerial(Year(dt), Month(dt) + 1, 1) - 1
> End Function
> Public Function EoM2(dt As Date)
> EoM2 = DateSerial(Year(dt), Month(dt) + 1, 0)
> End Function
> 
> 
> Tue 27 Nov 2012 00:08:11  Arthur Fuller <fuller.artful at gmail.com>:
> >	
> >
> >
> 	
> 	
> >
> 		
> 		
> 			
> >There we go. Thanks!
> >
> 
> >
> 
> >
> On Mon, Nov 26, 2012 at 11:27 PM, Stuart McLachlan
> >
> <stuart at lexacorp.com.pg>wrote:
> >
> 
> >
> > Alternative (simpler?)  functions:
> >
> >
> >
> > Public Function BoM(dt As Date) As Date
> >
> >     BoM = DateSerial(year(dt),month(dt),1)
> >
> > End Function
> >
> >
> >
> > Public Function BoY(dt As Date) As Date
> >
> >      BoY = DateSerial(year(dt),1,1)
> >
> >  End Function
> >
> >
> >
> > Public Function EoY(dt As Date) As Date
> >
> >      EoY = Dateserial(year(dt),12,31)
> >
> > End Function
> >
> >
> >
> > Public Function EoM(dt as date)
> >
> >   EOM =  DateSerial(year(dt),month(dt) +1,1) -1
> >
> > End Function
> >
> >
> >
> >
> ><<< skipped >>>
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com





More information about the AccessD mailing list