Solved

Difference between refencing a text box directly and using it's "Text" property

Posted on 2006-06-09
5
199 Views
Last Modified: 2010-04-30
Hi all,

  Could someone please tell me the difference (if any) between referencing a text box directly as opposed to using its text property?  For example, if I have a text box called txtExample, and I want to retrieve whatever value the user has entered from this box, I can use the following code:

  Dim Example As String
  Example = txtExample

  I can also use:

  Dim Example As String
  Example = txtExample.Text

  Is there any difference between these two?  Thank you in advance.
0
Comment
Question by:potterlu
5 Comments
 
LVL 13

Accepted Solution

by:
rettiseert earned 50 total points
ID: 16870823
Hi

Most controls in VB have a default property to let you abreviate your code, if the control has a default property then you can just type the name of the control and VB will assume that you are refering to the default property.

.Text is the default property of a text box so writing txtexample or txtexample.text is the same.
0
 
LVL 1

Expert Comment

by:Apyreal
ID: 16870923
rettiseert is right. They are the same, but using the .Text property makes it easier to read and helps avoid confusion.
0
 
LVL 9

Expert Comment

by:dancebert
ID: 16871067
>.Text is the default property of a text box so writing txtexample or txtexample.text is the same.

Some former Access programers will disagree, but they're wrong.  In Access 97 when the textbox is empty vartype(me.text1.text) = 8, but vartype(me.text1) = 1  (8 is String, 1 is NULL).  In VB, both cases return 8.
0
 
LVL 3

Expert Comment

by:J_K_M_A_N
ID: 16873120
Just recently I had a problem though using the txtExample without the .text. I had a program that was for sending out email and I had a reference to the Outlook 9.0 object library and when I tried to use the following command:

oMsg.Attachments.Add txtAttachment1

It gave me an error number of -1940783101 and it said "The property does not exist. The field you want to modify is not valid for this type of item."

I had to add the .text to the end of it then it worked fine.

Just a word of warning.

J_K_M_A_N
0
 
LVL 18

Expert Comment

by:Sethi
ID: 16876718
If you do not use a ".Text" property or any default property of a control then the code execution is faster. This is what I read several years back when I started my VB programming and I have been following this practice. I tried seraching the link, but Vb.net is everywhere :-)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 43
Add and format columns in vb6 7 57
MsgBox 4 61
Export Data to Different .csv Files 26 105
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question