Ask the MultiValued Visual
Basic Expert - #12B
(as published in Spectrum
magazine Nov/Dec 1998)
To email your questions to "Ask the MultiValued VB
Expert", click here.
Copyright © 1996-98 Caduceus Consulting. All rights reserved.

Fast Field Load and Update
Dear
MV/VB Expert:
I'm
used to having a 4GL that quickly created reasonably useful data
entry and retrieval screens for me in my character-based
applications. I find VB more tedious, in that whenever I read in
an item from my MultiValue host, I have to go through and
populate all of my text boxes and other controls by hand. Each
attribute must be extracted and its value assigned to the
appropriate control (sometimes with a conversion). Then I have to
write code to reverse it all when I save the screen. Any ideas on
how this might be done more efficiently?
An
excellent question! I'll share with you all a technique that I
have developed for fast implementations and prototyping of
application screens. Note that I don't necessarily recommend this
in all situations, for two reasons: (1) it involves hard-coding
of attribute numbers, which is rarely a good idea; and (2) it
makes somewhat unorthodox use of a property in Visual Basic.
All that
being said, here's how it works:
- Read
the entire item (using whatever middleware will do this
for you).
- Call
a standard load routine to go through all controls on the
form and load values as appropriate.
- Allow
user to make changes, etc.
- Call
a standard save routine to go through all controls on the
form and update values of the item.
- Write
the item back to your MultiValue host.
Assuming
that steps 1, 3, and 5 are relatively straightforward, let's look
at steps 2 and 4.
Going
Through All the Controls on a Form
By now,
you probably know that a control is simply an element
(usually visible) on a form (or window), such as a TextBox,
ListBox, Label, etc. You also likely know that every control has
properties that can be referenced using the name of the control
and the name of the property, as in txtCustName.Text or txtCustNumber.Enabled. What you may not know is
that you can declare a variable to be a 'Control', and then load
an entire control (properties and all) into it. By loading many
different controls into the same variable, one at a time, you can
execute the same code on many different controls.
In this
case, the "many different controls" are all the
controls on the form, which we get from its Controls
collection. In technical terms, a collection is a
group of objects of the same class. Visual Basic
has built -in functionality to step through each member of a
collection. Here is the code, assuming that it is in a General
Subroutine of the form in question:
Dim FormCtl As Control
For Each FormCtl In Me.Controls
. . '
<work with FormCtl.properties>
Next FormCtl
Note
that Me.Controls is the Controls
collection of the current form (referenced by the keyword
"Me").
Loading
and Updating Values as Appropriate
Now that
we have a way of looping through all the controls on a form, we
need to associate the relevant text boxes and labels with
particular attributes of the item that we are working with. For
this, I use a property in Visual Basic called DataField. This
property is normal used to map controls to fields of a JET or SQL
type database, but I have hijacked it for my own use (thus the
caveat earlier). It can be as simple as typing the attribute
number into the DataField property of the text box at design
time, using the properties window. Assuming that we have a
general routine to extract and replace attributes (if you don't,
see my other article in this issue), the load and update routines
might be:
Sub FastLoad(Item As String)
Dim FormCtl As Control
For Each FormCtl In Me.Controls
. . Attr$ = FormCtl.DataField
. . If Attr$ <> "" Then
. . .
. FormCtl =
Extract(Item, Attr$)
. . End If
Next FormCtl
End Sub
Sub FastUpdate(Item As String)
Dim FormCtl As Control
For Each FormCtl In Me.Controls
. . Attr$ = FormCtl.DataField
. . If Attr$ <> "" Then
. . .
. Item = Replace(Item,
Attr$, (FormCtl))
. . End If
Next FormCtl
End Sub
Note
that in the load routine, I assign a value to FormCtl, without
the ".Text" property specified. Using this syntax means
that FormCtl could also be a label control (the .Text and
.Caption are default properties). The same applies to (FormCtl)
in the update routine.
A
More Sophisticated Version
Given
the simplicity of the above code, it is easy to imagine that you
could make your routines a lot smarter. For instance, a syntax
for DataField values could be developed that supported
multivalued data, conversions, multiple items, etc. The TypeName
function or TypeOf keyword could be used to determine the type of
each control so that loading and updating can be customized to
each control type. Rather than force you to reinvent the wheel, I
invite all readers to download the code
for a more sophisticated version of these fast load and
update routines and try them out for yourself. Enjoy!

To email your questions to "Ask the MultiValued VB
Expert", click here.
Copyright © 1996-98 Caduceus Consulting. All rights reserved.
Added: August 21, 1998.
Return to Caduceus
Consulting Home Page