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:
And call it as such:
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:
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.
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!
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!