TempVars Value Error

TempVars Value Error

You cannot Always omit “Value”

I often see people are advised they do not need to refer to a Control’s Value implicitly like this:-

Dim strValue As String
strValue = cboCurrent.Value

This advice is given because “Value” is the default, therefore it is unnecessary to append it, So you could write the above like this:-

Dim strValue As String
strValue = cboCurrent

However this is not entirely true in Every case!

I noticed this “Unexpected behaviour” a few years back, when I tried to convert one of the Northwind database’s macro’s into a VBA routine. Some of the Northwind macros save information into TempVars. The conversion routine used the combobox name only, omitting the Value property and resulted in an Error.

I would guess it’s not widely known that by omitting the “Value” property when saving to TempVars causes the TempVar to assume you are trying to save an object to it, as most VBA programmers avoid useTempVars, and would seldom experience this problem.

You can reproduce this behaviour yourself:-

Try and add the value of a combobox to a TempVar with this Code:-

TempVars.Add "CurrentUserID", cboCurrent

You will trigger the following error:-

Error 32538 —  TempVars can only store data. They cannot store Objects.

This is because the TempVar thinks you are trying to assign an object to it (the combobox) and not the value of the object (the combobox Value).

What’s interesting is, if you assign the combobox value to a string variable, (you can do this without appending the control name with the “Value” property) then you assign the string variable to the TempVar and it works fine:-

Private Sub Command0_Click()
Dim strValue As String

strValue = cboCurrent

MsgBox " >>> " & strValue

TempVars.Add "CurrentUserID", strValue
End Sub

TempVars Value Error

Video 1 (2:31)

Video 1 (2:31)

New Blog about “VALUE HERE:-
http://www.niftyaccess.com/value/

… …

Leave a Comment