"rkc" <rk*@rochester.yabba.dabba.do.rr.bomb> wrote in message
news:w3*******************@twister.nyroc.rr.com...
Brian Wilson wrote:
I know many of you must have bought the Access Developer's Handbook and
used the Tagged Values class, so perhaps I can ask someone to check
something. On page 303, it describes the Add method of the class, and
claims it returns the TaggedValue item that was added. However, as far
as I can see, it does not provide a return value (despite being a
function rather than a sub) so cannot work as advertised.
Am I right in my assumption?
Is there any point in a function - why not a sub? The TaggedValue class
is so simple (just two properties) that I wonder whether anyone would
need:
Set tv = tvs.Add("FullName", "Brian Wilson")
Since there isn't much you could do with the tv object afterwards.
Finally, if I did want to make it work as advertised, would I just add
this as the final line of code?
Set Add = tv
Here is the code in question:
Public Function Add(Tag As String, _
Value As String) As TaggedValue
Dim tv As TaggedValue
' If the tag already exists, remove
' it so you can set the new value.
Call Remove(Tag)
' Create the new TaggedValue object, set its
' properties, and add it to the collection.
Set tv = New TaggedValue
tv.Tag = Tag
tv.Value = Value
mcolItems.Add tv, Tag
End Function
I have never owned a copy of the Developer's Handbook so I am not
familiar with the full context of that function. With that said, you
are correct that in order for the function to actually return anything
you would have to set Add = tv.
It looks like the actual purpose of the procedure is to add a tv object
to the mcolItems collection that is defined some where else. Like you
it baffles me why a taggedvalue object is even needed since on the surface
it doesn't appear to add any capability that isn't already
available with a VBA.Collections value and key parameters.
Thanks for that (and I hope you won't be too dissapointed to learn I am not
the Brian Wilson of Beach Boys fame).
The general idea is to provide a structured way to build up paired values
which might be useful for many things, but one specific example is passing
multiple values to forms.
Without any class coding, you might typically write:
Dim strOpenArgs As String
strOpenArgs="FormColour=Red;FormCaption=Hello;Form Mode=ReadOnly"
DoCmd.OpenForm "frmMyForm", , , , , acDialog, strOpenArgs
Then in the form's open event, you write code to parse these parameters and
do something useful with them.
Using these tagged values (or KeyValues as my re-write will call them), you
could do something like:
Dim kvs As KeyValues
Set kvs = New KeyValues
kvs.Add "FormColour", "Red"
kvs.Add "FormCaption", "Hello"
kvs.Add "FormMode", "ReadOnly"
DoCmd.OpenForm "frmTest", , , , , acDialog, kvs.Text
' Code in the form's open event
Dim kvs As KeyValues
Dim kv As KeyValue
Dim strOpenArgs As String
strOpenArgs = Nz(Me.OpenArgs, "")
If Len(strOpenArgs) > 0 Then
Set kvs = New KeyValues
kvs.Text = strOpenArgs
If kvs.Exists("FormCaption") Then
Me.Caption=kvs.Item("FormCaption").Value
End If
...
etc
...
Set kvs = Nothing
End If
You can see it does pretty much the same thing, but allows a more structured
approach and allows
If kvs.Exists("FormCaption") Then ...
To check whether I've passed a particular parameter to a form in the
openargs
Anyway, I wonder if other people have any better ways to do this - but I
might post a new subject for this.