Question

How to create a function dealing with text boxes and having a variable for the text box property

Asked by: darbid73

I have a form with unbound textboxes and comboboxes.
I want to create a function where I can alternate between two properties.
The two properties are "text" and "value"

I thought this was the theory

Forms("formname").Controls("controlname").Properties("propertyname")

and thus in a short form the below should work.

dim valuetype as string
 valuetype = "Value"
 Me.txt_date.Properties(valuetype)

I get invalid reference to the property value - 2455

Could someone point me in the right direction please.

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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-28 at 11:39:31ID24852187
Tags

MsAccess 2003

,

VBA

Topics

Access Coding/Macros

,

Access Forms

,

Microsoft Access Database

Participating Experts
3
Points
500
Comments
21

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Variable --> PropertyName
    I have a file like this: "PropertyName1",PropValue "PropertyName2",PropValue etc... I also have a Class: Class.PropertyName1 Class.PropertyName2 etc... As i don't know the order of lines written in the file, i need to assign the PropValue (the 2nd field...
  2. Report Design Theory and Practice
    I need to create a report based on a table that has a primary key, 5 native fields and 12 foreign keys. I need more granualar control over how the report behaves than HideDuplicates and IsVisible will give me. The primary key need not be printed visibly but the other sixtee...
  3. revert unbound combobox to original data using BeforeUpd…
    i have an unbound combo box on an unbound form. on the BeforeUpdate event i want the user to confirm the data change using a messagebox if the user selects 'No', then i want the combo box to revert back to it's original data. normally i would do this using: Cancel=true ctl....

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: GRayLPosted on 2009-10-28 at 12:09:10ID: 25687009

This from VB Editor Window - Help

Text Property
See AlsoApplies ToExampleSpecificsYou can use the Text property to set or return the text contained in a text box or in the text box portion of a combo box. Read/write String.

expression.Text
expression    Required. An expression that returns one of the objects in the Applies To list.

Remarks
You can set the Text property to the text you want to display in the control. You can also use the Text property to read the text currently in the control.

You can set or read this property only by using a macro or Visual Basic.


Note  To set or return a control's Text property, the control must have the focus, or an error occurs. To move the focus to a control, you can use the SetFocus method or GoToControl action.


While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.

Note that Value is not one of the enumerated Properties of any control.  It is intrinsic to that control.  The value 2455 is the Error Number of the error message, not a property value.

 

by: Helen_FeddemaPosted on 2009-10-28 at 13:32:28ID: 25687962

Why not just use Value?  I think that should do; no need to sometimes select Text and sometimes Value, since these are unbound controls.

 

by: ldunscombePosted on 2009-10-28 at 14:31:20ID: 25688563

Text boxes don't have a property called "valuetype".

Try
dim valuetype as string
 valuetype = "Value"
 Me.txt_date = valuetype

Will enter the word value in the text box.

Not sure what you are trying to achieve here but if it's what I think you are trying to do, then you are trying to specify that the text box should contain either a string value or a number value.  If this is the case,  then you can't.  The data type of the text box is determined by the Field Type in the underlying control source table.

You can change how the text box displays the data ie "Long Date, Short Date, Currency etc"  But it is the table that determines the data type.

Hope this helps

Leigh

 

by: ldunscombePosted on 2009-10-28 at 14:36:35ID: 25688607

Sorry if the control source of txt_date is in fact a date field in the underlying table as the name suggests then the code I posted will produce an error.  ie The value you entered is not valid for this field.

Wont Work !
dim valuetype as string
 valuetype = "Value"
 Me.txt_date = valuetype

Should work
dim valuetype as Date
valuetype = #1/1/2009#
 Me.txt_date = valuetype
Should enter the date 1/1/2009 in the field

Leigh

 

by: darbid73Posted on 2009-10-28 at 14:48:36ID: 25688708

ldunscombe I am not sure you understand what I am attempting here.

Hi Helen, for unbound controls I believe I must use the .text property because I am checking the contents of the control with the . Onchange event. For this . Value does not update till the control loses focus.  But then later when all controls are filled I want to use .value cause .Text only works if the control has focus

 

by: ldunscombePosted on 2009-10-28 at 15:14:16ID: 25688869

OK Now I understand

in the change event of your code

use  me.txt_Date.text

and when you refer to it later use

me.txt_date

 

by: darbid73Posted on 2009-10-28 at 22:23:55ID: 25690858

ldunscombe I am note sure where you get date again.

But I want to use Me.txtbox.text and Mee.txtbox.Value in the same function, something like this

Where Valuetype would equal sometimes .value and sometimes .text


Me.txt_date.Properties(valuetype)

 

by: ldunscombePosted on 2009-10-29 at 03:27:30ID: 25692081

<ldunscombe I am note sure where you get date again.>
I am really, really confused now. your question specified  "Me.txt_date.Properties(valuetype)"
it's the name of your control "txt_date" is where I got date from. You also have made no reference to a function or exactly what you are trying to achieve.  I am trying to help with "Best Guesses" from the information you have provided.  If you can be more clear about what you are trying to do I am "Certain" that myself or another contributor to this site will be able to help you out.

The quality of the answers you get will be proportional to the quality of the problem description you provide.

Leigh.






Me.txt_date.Properties(valuetype)

 

by: ldunscombePosted on 2009-10-29 at 03:36:46ID: 25692153

Extremely Sorry,  Re-read the question and noticed "I want to create a function".  My Apologies.

but what do you want it to do ?

Do you have any code that you are starting with ?  that you could post ?

Sorry Again.

I won't be happy till we get this sorted for you.

Leigh

 

by: darbid73Posted on 2009-10-29 at 03:38:48ID: 25692162

Thanks Leigh

So you know how you use something like this

Form - Control - Property

Me.mytextboxname.text

Well what I want to do is I want to be more flexible with this.

For example the below example means that I can set  "str_FormName" to ANY form I have with that textbox and I can use the one line many many times.  I can then throw this in a function and the function could be used for all my forms.

Form - Control - Property

Forms(str_FormName).mytextboxname.text


So now I want to be able to do the same thing but with the PROPERTY


Me.mytextboxname.??????????

I believe it is difficult because I want to use Value and Text and because Value is the default but it should still work.





 

by: ldunscombePosted on 2009-10-29 at 03:58:31ID: 25692267

I still don't understand

If you are passing it to a function then as you know the valuue you pass is subject to the event you call it from.

for example if you are calling a function from the on change event of the control itself you would use something like
MyFunction(me.textboxname.text)
or
if calling from any other event or another control
MyFunction(me.textboxname)

????

Leigh

 

by: darbid73Posted on 2009-10-29 at 04:19:40ID: 25692386

I did not realise my question was so hard.

I agree I could pass the whole control to the function but that would not work as the function has many controls in it.

I was trying to keep this on a semi theoretical level but I think you are dying to see the function, so an example is below.  The below is a start BUT IS NOT FINISHED in other ways other than this question.

Once again the form has all UNBOUND controls.
I want to update another section of the form with every change to a control.
Thus as far as I know I must use .text for this.
Also the control must have focus - thus the test with the IF for the control with focus. (this will have to be changed if we do get Value to work here as well)

Thus .text cannot be used to check all the controls so then I want to use value.

I am tending towards not worrying about it and just creating two functions.



 
Private Function updateemailtext() As String
 
 
Dim mycontrol As Control
Set mycontrol = Screen.activecontrol
 
If mycontrol.Name = "txt_licensee" Then
If Len(Nz(Me.txt_licensee.text)) > 0 Then
    str_licensee = Me.txt_licensee.text
Else
    str_licensee = "[empty licensee]"
End If
End If
 
If mycontrol.Name = "txt_vfile" Then
If Len(Nz(Me.txt_vfile.text)) > 0 Then
    str_vfile = " (" & Me.txt_vfile.text & ") "
Else
    str_vfile = ""
End If
End If
 
end function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:

Select allOpen in new window

 

by: ldunscombePosted on 2009-10-29 at 04:59:36ID: 25692624

Now I think I know where your going,

Your function is referring to the active control so all you need to do is check if it is bound or unbound.

ie

Dim mycontrol As Control
Set mycontrol = Screen.activecontrol

if len(mycontrol.controlsource) < 1 then
 ' The control is unbound and you need to use the text property.
else
' The control is a bound control. use the value property
end if

Are we getting close ???

Leigh


 

by: darbid73Posted on 2009-10-29 at 05:14:35ID: 25692723

You are right there are probably 1000 ways to write my function for testing what is in each control.  But that is not the question. In fact your last suggestion (which is a good one) avoids the question. :-)

The question is how to use a variable in the place of the Property Name of a control.

I know how to do this and use it quite often for Forms and Control Names but until now have not been successful with the PROPERTY NAME.

 

by: GRayLPosted on 2009-10-29 at 15:44:29ID: 25698963

Just so we are aware of the subtle but distinctive difference, open a form myForm with a text box tbxLName, bound to a table Names.  The text box should now be displaying the last name of the first record - say Smith.  Now, click on the textbox, and change just the last letter to 't' - Smitt - now type Alt+F11 and in the Immediate pane type:

? Forms!myForm!tbxLName.Value
Smith
? Forms!myForm!tbxLName.Text
Smitt

 

by: darbid73Posted on 2009-10-30 at 01:50:33ID: 25701050

Hello Ray,

(re your comment in another thread - I would prefer fishing to doing this VBA stuff any day)

Thanks for pointing out the subtle difference we are dealing with here.  I hope that by explaining what I understand we might be able to move on to my question or you can pick me up on why I am incorrect.

I have unbound text boxes.  The textboxes need to be updated with each change made.  For example imagine a form and at the top is a textbox labeled  "Place of Holidays" at the bottom of the form is the text  "We went on holidays to [space] and we went fishing"

Now what I am trying to do is that as the person types in the textbox "Place of Holidays" the below text is updated. So if in "Place of Holidays" the user types a "K" then the text below would now be  "We went on holidays to K and we went fishing"

So back to the theory - I am using the onChange event for each textbox.  When you check the .value and .text of this text box as you type, the .value does not change with each keystroke but the .text does. .Value will only change after the control is saved.  Thus this means that to do what I want to do I have to use the .text property.  However and annoyingly a control must have the focus to read the .text property, thus anywhere else in the code when I want to get all the contents of the textboxes I cannot use .text I have to use .Value.

As said before I have now just made two functions one with the text property and one with the value property, but I would like to know if it is still possible to combine these two functions into one by passing the property name to the function.

 

by: GRayLPosted on 2009-10-30 at 09:02:40ID: 25704283

When the focus is on another control, you can momentarily set it back to txtPlaceOfHolidays, read the .text and return to the control that had the focus:

strCurrControl = Forms!frmName.ActiveControl
Forms!frmName.txtPlaceOfHolidays.SetFocus
strText = Forms!frmName.txtPlaceOfHolidays.Text
Forms!frmName.Controls(strCurrControl).SetFocus

That any help?

 

by: darbid73Posted on 2009-10-30 at 09:34:19ID: 25704617

Thanks for the suggestion - yes you are right setting the focus on each box would work while going through them all, but not be so good when someone is actually typing in the box as focus would be taken away after each keystroke. ie each time the onChangeEvent happens.

You realise you are using an example by way of the controls which I would like to do with the properties. :-)

Forms!frmName.Controls(strCurrControl).SetFocus

I would love to be able to use this

Forms!frmName.txt_mytextbox.Properties(strmyproperty)

 

by: GRayLPosted on 2009-10-30 at 10:18:35ID: 25705031

Forms!frmName!txt_myTextBox.Properties(strMyProperty) works when strMyProperty = "Text".  However, as there is not an enumerated Property named "Value", if that is what you want then use Forms!frmName!txt_myTextBox.Value.

Here's what I've been able to come up with:

strMyProp = "Value"
myProp = "forms!frmName!!ttx_myTextBox" & IIF(strProp="Text",".Properties('Text')",".Value")
? eval(myProp)
1671791  ' - stored value
'  In here you need the code to make sure the textbox has the focus.
strMyProp = "Text"
? eval(myProp)
1671792 - edited value

 

by: darbid73Posted on 2009-11-03 at 00:32:45ID: 31647151

My testing show that the first sentance above of GrayL is the answer to my specific question.

ie

"Forms!frmName!txt_myTextBox.Properties(strMyProperty) works when strMyProperty = "Text".  However, as there is not an enumerated Property named "Value", if that is what you want then use Forms!frmName!txt_myTextBox.Value."


Thank you GrayL

 

by: GRayLPosted on 2009-11-03 at 07:28:44ID: 25729847

Thanks, glad to help.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...