473,473 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

format or template function?

I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string as specified in the format string. For example
fmt("this is $1 format string. arg2 is $2", "short", 3)
would return the string
"this is short format string. arg2 is 3"

Now, the above is easy to write and I have done so. What I want
is something similar that also has iterating and conditional abilities.
For example if 'rs' is a recordset (or array variable) with 3 records
and the "iterate" format spec is "<$", "$>" then
fmt ("the members are $< $1!name($1!age),$>", rs)
would return
"the members are smith(23), wilson(37), dobbs(18) "

Of course I don't care much about the exact syntax used in
the format string, just that it has these capabilities in general.

Anyone know of anything like this (preferably free)? Several
hours of web searching did not turn up anything. And yes, this
is an Access question (sort of :-), at least in the sense that I
will be using this in an Access application.
Nov 13 '05 #1
6 2809
On Mon, 7 Mar 2005 08:23:22 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

There is the Windows API function wsprintf, but you may have a hard
time calling a "C calling convention" function from VBA.

-Tom.

I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string as specified in the format string. For example
fmt("this is $1 format string. arg2 is $2", "short", 3)
would return the string
"this is short format string. arg2 is 3"

Now, the above is easy to write and I have done so. What I want
is something similar that also has iterating and conditional abilities.
For example if 'rs' is a recordset (or array variable) with 3 records
and the "iterate" format spec is "<$", "$>" then
fmt ("the members are $< $1!name($1!age),$>", rs)
would return
"the members are smith(23), wilson(37), dobbs(18) "

Of course I don't care much about the exact syntax used in
the format string, just that it has these capabilities in general.

Anyone know of anything like this (preferably free)? Several
hours of web searching did not turn up anything. And yes, this
is an Access question (sort of :-), at least in the sense that I
will be using this in an Access application.


Nov 13 '05 #2
Thanks Tom, but besides the calling problems, wsprintf() does
only single variable subsitution, it does not do the iterative and
conditional template-style substitutions i need.

I can find functions that will do what I'm asking for in every other
modern language I've looked at (python, perl, java, c/c++, ruby)
and i'll bet if i looked hard, I could find one written in fortran!!
Why not VB??!!

"Tom van Stiphout" <no*************@cox.net> wrote in message news:gi********************************@4ax.com...
On Mon, 7 Mar 2005 08:23:22 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

There is the Windows API function wsprintf, but you may have a hard
time calling a "C calling convention" function from VBA.

-Tom.

I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string as specified in the format string. For example
fmt("this is $1 format string. arg2 is $2", "short", 3)
would return the string
"this is short format string. arg2 is 3"

Now, the above is easy to write and I have done so. What I want
is something similar that also has iterating and conditional abilities.
For example if 'rs' is a recordset (or array variable) with 3 records
and the "iterate" format spec is "<$", "$>" then
fmt ("the members are $< $1!name($1!age),$>", rs)
would return
"the members are smith(23), wilson(37), dobbs(18) "

Of course I don't care much about the exact syntax used in
the format string, just that it has these capabilities in general.

Anyone know of anything like this (preferably free)? Several
hours of web searching did not turn up anything. And yes, this
is an Access question (sort of :-), at least in the sense that I
will be using this in an Access application.

Nov 13 '05 #3
DFS
Stuart McGraw wrote:
I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string as specified in the format string. For example
fmt("this is $1 format string. arg2 is $2", "short", 3)
would return the string
"this is short format string. arg2 is 3"

Now, the above is easy to write and I have done so. What I want
is something similar that also has iterating and conditional
abilities. For example if 'rs' is a recordset (or array variable)
with 3 records and the "iterate" format spec is "<$", "$>" then
fmt ("the members are $< $1!name($1!age),$>", rs)
would return
"the members are smith(23), wilson(37), dobbs(18) "

Of course I don't care much about the exact syntax used in
the format string, just that it has these capabilities in general.

Anyone know of anything like this (preferably free)? Several
hours of web searching did not turn up anything. And yes, this
is an Access question (sort of :-), at least in the sense that I
will be using this in an Access application.


Stuart,

Since you specified the functionality so well, and you've written your own
functions, I'm wondering why you're having a difficult time writing this
one?

This may be what you're looking for:
Public Function fmt(funcType as String, rs as Recordset) as String

fmt = ""
if funcType = "iterate" then
if rs.recordcount = 0 then
fmt = "no members found"
else
do until rs.eof
fmt = fmt & rs("name") & "(" & rs("Age") & "), "
rs.movenext
loop
fmt = Trim(fmt)
fmt = "the members are " & left(fmt,len(fmt)-1)
endif
endif

End Function
Nov 13 '05 #4
On Tue, 8 Mar 2005 11:59:41 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

I don't agree wsprintf doesn't support multiple substitutions. but it
probably can't be used for other reasons anyway. I now seem to
remember you cannot call cdecl API functions from VBA period.

If you embark on writing your own, look into ParamArray option of the
Function statement. It is VBA's way to support cdecl calling
convention.
I also like the Array function, because it allows you to put as many
elements as you need into a single variant. In our company we have
written a code library that extensively uses Array(Array(...)) and
passes it around as a single variant.

If you write your own and have to decide on a syntax, try sticking
closely to something that already exists. Think regular expressions,
or the name/value pairs in a querystring, or HTML as examples of
standard syntax that perhaps could be used.

-Tom.
Thanks Tom, but besides the calling problems, wsprintf() does
only single variable subsitution, it does not do the iterative and
conditional template-style substitutions i need.

I can find functions that will do what I'm asking for in every other
modern language I've looked at (python, perl, java, c/c++, ruby)
and i'll bet if i looked hard, I could find one written in fortran!!
Why not VB??!!

"Tom van Stiphout" <no*************@cox.net> wrote in message news:gi********************************@4ax.com...
On Mon, 7 Mar 2005 08:23:22 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

There is the Windows API function wsprintf, but you may have a hard
time calling a "C calling convention" function from VBA.

-Tom.

>I am looking for a VBA "format" or "template" function, that is,
>a function that takes a format string and a varying number of
>arguments, and substitutes the argument values into the format
>string as specified in the format string. For example
> fmt("this is $1 format string. arg2 is $2", "short", 3)
>would return the string
> "this is short format string. arg2 is 3"
>
>Now, the above is easy to write and I have done so. What I want
>is something similar that also has iterating and conditional abilities.
>For example if 'rs' is a recordset (or array variable) with 3 records
>and the "iterate" format spec is "<$", "$>" then
> fmt ("the members are $< $1!name($1!age),$>", rs)
>would return
> "the members are smith(23), wilson(37), dobbs(18) "
>
>Of course I don't care much about the exact syntax used in
>the format string, just that it has these capabilities in general.
>
>Anyone know of anything like this (preferably free)? Several
>hours of web searching did not turn up anything. And yes, this
>is an Access question (sort of :-), at least in the sense that I
>will be using this in an Access application.


Nov 13 '05 #5
Thanks again Tom.
I don't agree wsprintf doesn't support multiple substitutions. Sorry, I should have chosen my words more carefully. I meant
a single format specifier being applied to a multivalued argument
(like an array or recordset) and expanding into a multiple strings,
one for each element of the argument, not, as I think you (justifiably)
took it, multiple format specifiers applied respectively to multiple
arguments.

It looks like I will have to write something myself. Yes, I will
look into some kind of HTML'ish or XML'ish syntax -- hopefully
I can find some VB code that will aid in parsing it. To date, I have
been using an extended printf-style syntax but things are becoming
to complicated to push that much further. I have discovered
that doing even simple parsing in VB is not much fun.
I am still a little surprised that this kind of thing has not already
been written for, for example, generating html pages from
templates. I guess with ASP and VBscript et.al. there is not
much need.

"Tom van Stiphout" <no*************@cox.net> wrote in message news:32********************************@4ax.com... On Tue, 8 Mar 2005 11:59:41 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

I don't agree wsprintf doesn't support multiple substitutions. but it
probably can't be used for other reasons anyway. I now seem to
remember you cannot call cdecl API functions from VBA period.

If you embark on writing your own, look into ParamArray option of the
Function statement. It is VBA's way to support cdecl calling
convention.
I also like the Array function, because it allows you to put as many
elements as you need into a single variant. In our company we have
written a code library that extensively uses Array(Array(...)) and
passes it around as a single variant.

If you write your own and have to decide on a syntax, try sticking
closely to something that already exists. Think regular expressions,
or the name/value pairs in a querystring, or HTML as examples of
standard syntax that perhaps could be used.

-Tom.
Thanks Tom, but besides the calling problems, wsprintf() does
only single variable subsitution, it does not do the iterative and
conditional template-style substitutions i need.

I can find functions that will do what I'm asking for in every other
modern language I've looked at (python, perl, java, c/c++, ruby)
and i'll bet if i looked hard, I could find one written in fortran!!
Why not VB??!!

"Tom van Stiphout" <no*************@cox.net> wrote in message news:gi********************************@4ax.com...
On Mon, 7 Mar 2005 08:23:22 -0700, "Stuart McGraw"
<sm********@zzfrii.remoovAllZZs.com> wrote:

There is the Windows API function wsprintf, but you may have a hard
time calling a "C calling convention" function from VBA.

-Tom.
>I am looking for a VBA "format" or "template" function, that is,
>a function that takes a format string and a varying number of
>arguments, and substitutes the argument values into the format
>string as specified in the format string. For example
> fmt("this is $1 format string. arg2 is $2", "short", 3)
>would return the string
> "this is short format string. arg2 is 3"
>
>Now, the above is easy to write and I have done so. What I want
>is something similar that also has iterating and conditional abilities.
>For example if 'rs' is a recordset (or array variable) with 3 records
>and the "iterate" format spec is "<$", "$>" then
> fmt ("the members are $< $1!name($1!age),$>", rs)
>would return
> "the members are smith(23), wilson(37), dobbs(18) "
>
>Of course I don't care much about the exact syntax used in
>the format string, just that it has these capabilities in general.
>
>Anyone know of anything like this (preferably free)? Several
>hours of web searching did not turn up anything. And yes, this
>is an Access question (sort of :-), at least in the sense that I
>will be using this in an Access application.

Nov 13 '05 #6
"DFS" <no****@DFS.com> wrote in message news:qM*******************@fe07.lga...
Stuart McGraw wrote:
I am looking for a VBA "format" or "template" function, that is,
a function that takes a format string and a varying number of
arguments, and substitutes the argument values into the format
string as specified in the format string. For example
fmt("this is $1 format string. arg2 is $2", "short", 3)
would return the string
"this is short format string. arg2 is 3"

Now, the above is easy to write and I have done so. What I want
is something similar that also has iterating and conditional
abilities. For example if 'rs' is a recordset (or array variable)
with 3 records and the "iterate" format spec is "<$", "$>" then
fmt ("the members are $< $1!name($1!age),$>", rs)
would return
"the members are smith(23), wilson(37), dobbs(18) "

Of course I don't care much about the exact syntax used in
the format string, just that it has these capabilities in general.

Anyone know of anything like this (preferably free)? Several
hours of web searching did not turn up anything. And yes, this
is an Access question (sort of :-), at least in the sense that I
will be using this in an Access application.


Stuart,

Since you specified the functionality so well, and you've written your own
functions, I'm wondering why you're having a difficult time writing this
one?

This may be what you're looking for:
Public Function fmt(funcType as String, rs as Recordset) as String

fmt = ""
if funcType = "iterate" then
if rs.recordcount = 0 then
fmt = "no members found"
else
do until rs.eof
fmt = fmt & rs("name") & "(" & rs("Age") & "), "
rs.movenext
loop
fmt = Trim(fmt)
fmt = "the members are " & left(fmt,len(fmt)-1)
endif
endif

End Function


Thanks for your posting. But I'm afraid I wasn't clear enough.
Your function will indeed generate the output I want but it will
generate *only* that output. To change the output I have to
change the function. What I want is a function that doesn't
need to be changed but instead generates it's output by
substituting values into place holders in a format string that
is an argument to the function. So to change the output
string, I just change the format string argument, not the code.

Specifically, I need to generate (small) RTF formatted documents
to be displayed in an RTF ActiveX control. There are many
slightly different styles of document. Each document style has
several places in it where data will be substituted. In some places
I need to display tables or lists, where the amount of data
is variable. In other places the data displayed may depend
on some other piece of data. Writing a a dozen or more
similar separate functions, with code that intermixes VB and
little snippets of rtf and the data, will be a maintenance nightmare.
(I know because that is what I am doing now! :-(

Instead, I want all of these styles stored as template strings in
an Access table. To display one, I will retrieve it, give it to the
template formatter routine (the thing I am looking for!), along
with the data to be inserted into it, and write the output to the
RTF control. I can change the format of a document by changing
the RTF in the database, with no changes to code.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
1
by: sebastian | last post by:
Hi, I'd like to specialize a template function that contains a template parameter. In Example i have the following function declared: .... template < int i > static stupid_object&...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
5
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this...
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.