How To Utilize String Buffering Within A VB Component, Part 2By Doug Dean
In Part 1 we looked, from a high-level perspective, how to create a VB component that efficiently creates a large HTML TABLE based on Recordset data and passes it back to an ASP page for display. In this part we'll focus in on some of the details!
We're creating two methods within our Class. The
MethodName(), is very similar to the
method explored in the article "How To Pass a Variant Array Populated with a RecordSet From a VB
Component to an ASP File". Since the
MethodName() method is fully covered in this previous
article, I'll only focus on modifications needed for it to work with our
One parameter was added to the
variable will hold the number of fields we will be selecting in our SQL statement. It will also be the
number of TABLE record data cells that will be displayed in the browser.
The first variables we declare in the
MethodName() method will be the ones we send to the
DoBuffer() method. The first variable (
strBuffer) is a string that we'll set
to a determined length. The
MethodName() method will call the
method multiple times using
strBuffer as it's first method parameter. The
variable will be used as a memory buffer for concatenating our strings. The
defined this parameter as
ByRef rather than
ByVal so it will come back to the
MethodName() method altered.
strBuffer as type String, we dimension it to a specific length and filled
it with NULL characters.
The length you set
strBuffer to should be determined by how much buffering space you
anticipate needing. We'll set it at 65536, which is much higher than needed for this example - but
reasonable for a lengthy HTML TABLE record. Since the speed saved in avoiding using the
operant increases with the frequency in which it's used, I'll assume that you'll be using the
DoBuffer() method when you have to concatenate many strings, i.e., when your database has
enough records to make a large HTML TABLE.
The second parameter (
lngBuffer) of the
DoBuffer() method is used to keep
track of the length of the data we'll be placing into the buffer. We declare this variable in the
MethodName() method as type Long and set it to zero.
lngBuffer variable is also set as
ByRef by the
method definition. This variable value will reflect the length of the stored data within the buffer.
We need this variable, like our
strBuffer variable, to be modifiable by the
method. Thus a reference to this variable is sent rather than the value itself. It will come back to us
The third, and last, parameter we'll be sending to the
DoBuffer() method will hold the
string we want to concatenate to the data that's being held in the buffer. As we build our HTML TABLE
record, the buffer will accumulate the string fragments that will, when we're done, compose our HTML
TABLE record. The
DoBuffer() method accepts the
ByVal. This is done primarily because the strings you'll be sending to the
method will be relatively short.
The strings we'll be sending to our
DoBuffer() method will be a combination of string
literals, Integers changed to strings via
CStr(), and string data from our database.
As I mentioned previously, I'll only minimally focus on the
MethodName() method since its
basic workings are covered in another article within this series. Here, in a nut shell, is the
MethodName() code that follows the declarations covered above.
MethodName() code above basically collects data from the same database structure used
in the "How To Pass a Variant Array Populated with a RecordSet From a VB Component to an ASP File"
article. The major difference is in the assignment of the
GetRows() values to a local
Variant variable (
vRecordArray) rather than the method name as in the last article.
Once the data from the database is stored in the
vRecordArray Variant variable array,
we'll loop through it within the component rather than sending it back to the ASP file. To help us do
this, we'll store the record count of this (zero based) array in a Long variable (
Now we're ready to start constructing our HTML TABLE record with the database data. This is
accomplished with outside and inside loops. The outside loop will loop through each database record
while the inside loop will loop through each database field. Each database field value will be send to
DoBuffer() method to be added to the buffer before sending the buffer data back to
the ASP file.
The statement before the outside loop is used to send a
TR tag. I have found it best to
send a TABLE record rather than the entire TABLE so the asp file can alter the TABLE attributes easier.
Note that the inside loop indexes from
(intFieldCount - 1). This is
vRecordArray is zero based.
Then opening and closing
TD tags sandwich the database data within the inside loop. This
gives each TABLE data cell it's own record field.
In order to make the resulting HTML source code more readable, you can place a
vbCrLf anywhere in
the code. This will cause a Control-Linefeed (line break) to occur in the HTML source code. The line
break won't be displayed in the browser (but will be displayed if you go to View/Source, and, consequently,
will make the resulting easier for a developer to read).
Once the loops are complete, it's a matter of cropping out the string data from our buffer with the following statement:
Also notice that the strings we added to the buffer within the loop were sent with following structure:
Which translates into the following statement using our method and variable names:
I typically go one step further and simplify my method and variable names into something like this:
This reduces the extra text on my precious screen landscape.
In Part 3 we'll examine the ASP code needed to make our whole project come together!