Replace a controls property with a variable

darbid

Registered User.
Local time
Today, 02:14
Joined
Jun 26, 2008
Messages
1,426
I am dealing with unbound controls (textbox and comboboxes) and would like to add the flexibility of being able to refer to either the "text" or "value" property.

Why do I get that this is an invalid reference to the property value 2455

Code:
 dim valuetype as string
 valuetype = "Value"
 Me.txt_date.Properties(valuetype)
I thought this was the full form of what I can do and that what I have above is a short version

Code:
Forms("formname").Controls("controlname").Properties("propertyname")
 
did you know...value is the default property so you can just say
Me.txt_date
to get the value.

as for the error, when you type:
Me.txt_date.Properties
then add the first parenthesis:
Me.txt_date.Properties(
look at the intellisense - it's looking for an index, not a string.

getting the index can be a bit tricky because the Properties property (Me.txt_date.Properties) is a collection and you have to loop through the Properties collection to find the property within the collection that you want, then figure out what to do with it from there.
 
look at the intellisense - it's looking for an index, not a string.
Actually you can use a string in there OR an index number, just like you can when you use a recordset object - rst("FieldNameHere") or rst(0)
 
I believe you'll need to just use:
Code:
Function TestFunction(strFormName As String, strControlName As String, strUse As String) As Variant

Select Case strUse
Case "Text"
   TestFunction = Forms(strFormName).Controls(strControlName).Text
Case "Value"
   TestFunction = Forms(strFormName).Controls(strControlName).Value
End Select
End Function
 
Thank you to you both. That is bad news Bob. The funny thing is when I loop through the property names of my text box both TEXT and VALUE are not there.
 

Users who are viewing this thread

Back
Top Bottom