L

Legacy 456155

Guest
Hello. When developing, I have found it convenient to use TempVars because they maintain state even when my VBA project does not. In addition, I don't have to run a load procedure every time I restart my code.

To provide a bit of validation and intellisense, I wrap them in properties in a standard module named "tv" as such:

VBA Code:
Public Property Get MyValidDate() As Date
    MyValidDate = TempVars!MyValidDate
End Property
Public Property Let MyValidDate(ByVal setValue As Date)
    If setValue > Date Then Err.Raise 513, "tv.MyValidDate (TempVar Wrapper)", "Date must be less than or equal to the current date"
    TempVars!MyValidDate = setValue
End Property

And call it as such:

VBA Code:
Sub TrySetProperty()
    On Error GoTo handler
    tv.MyValidDate = Date + 1
    Exit Sub
handler:
    MsgBox Err.Description
End Sub

To maintain and restore state between app sessions, upon closing, I loop through my tempvars and dump the values as strings into a table. All fine so far. My little problem is that when I load the tempvars when starting the app, I would like to call the property wrapper for each TempVar for validation purposes.

This is basically what I use to load my TempVars:

VBA Code:
Private Sub LoadGlobalSettings()
    With CurrentDb.OpenRecordset("SELECT * FROM Setting WHERE Scope = ""global""")
        Do Until .EOF
            TempVars(!SettingID.Value) = !SettingValue.Value
            .MoveNext
        Loop
    End With
End Sub

CallByName doesn't offer any help because I'm using a static module. Application.Run, unless I'm missing something, doesn't seem to help either because I can't call a setter.

The only alternative I can think of is to use separate setter subs. I could then use Application.Run.

VBA Code:
Public Property Get MyValidDate() As Date
    MyValidDate = TempVars!MyValidDate
End Property
Public Sub Set_MyValidDate(ByVal setValue As Date)
    If setValue > Date Then Err.Raise 513, "tv.MyValidDate", "Date must be less than or equal to the current date"
    TempVars!MyValidDate = setValue
End Sub

I could then call Public Sub Set_MyValidDate(ByVal setValue As Date) as such: Application.Run "Set_" & !SettingID.Value, !SettingValue.Value
If the value in !SettingValue.Value is not valid, an error would be raised.

This doubles the number of items in my intellisense. While this is not a deal-breaker, I would like to hear some alternatives if there are any. How do YOU maintain state? Any ideas?

Thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Maybe I'm not understanding, but if your goal is to 'retain' the original type of the variables despite converting them to strings for storage in your table, the following might get you there:

  • Create another text field in your table called something like [VarType]
  • Get the variables' types with TypeName()
  • Write those type name values to the [VarType] field
  • Read the [VarType] field in when loading your variables
  • Check the [VarType] value and use it to convert each variable back to its original type
 
Upvote 0
Thanks Jon. The benefit of using TempVars is that I don't have to run code every time I lose state and then run code thereafter. The tempvars are still in memory. Because strings can be coerced to any datatype and this is done implicitly, the properties in the tv take care of casting. I can also enforce read-only tempvars by simply not having a setter. Access has built in database properties that allow you to store variables of specific types though I rarely see anyone using it and I don't myself because the table Access utilizes is in the front end. This is "just the way I've done it" and it works, but I am not able to validate when initially loading without including all the extra fat in my intellisense list. This is not even really a problem so to speak, I'm just always willing to learn and find a better way. Every developer has his/her own tilt on doing things and many of them are right or at least sufficient though different. I am looking for a better way. Thanks for your input.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top