Monday, August 15, 2005

A printf for VBScript - derivative work

Uwe Keim, on the CodeProject site, has a "printf"-ish VBScript function. Here's my version (which I now realise is almost word for word the same as someone else's take on Uwe's original.) So ...

Sven Axelsson, on the above site, has a version of Uwe's original, called "FormatMessage-like formatting in VBScript" which looks almost exactly like that below (except he calls his "Fmt", rather than "Subst").
function Subst(str, args)
    dim res
    dim i
    res = str

    for i = 0 to UBound(args)
        res = Replace(res, "%" & CStr(i+1) & "%" , args(i) )
    next

    res = Replace(res, "\n", vbCrLf)
    res = Replace(res, "\t", vbTab)

    Subst = res
end function
And an example of use:
theSelector = "SELECT * FROM %1% WHERE [aKey] >= (DATESERIAL(%2%, %3%, %4% ) + TIMESERIAL( %5%, 0, 0 )) AND [aKey] <= (DATESERIAL(%2%, %3%, %4% ) + TIMESERIAL( %5%, 59, 59 ))"
theSelect = Subst( theSelector, Array( theTable, theYear, theMonth, theDay, theHour ) )

No comments: