By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

Simple (I think) scope question

P: 4
Hi, I have just started trying to use Access to add some functionality to a form I was given. I have no experience with this language and am having some difficulty accessing text from another textbox.

As an example, I tried making a simple form that combines two strings from different text boxes and put it into a third with a button click, but I get an error, specifically "Run-time error '2185': You can't reference a property or method for a control unless the control has the focus."

Here's the code I wrote to control the three text boxes(txtFirstName, txtLastName and txtFullName) and the button(stringButton):
Option Compare Database

Public Sub stringButton_Click()

txtFullName.Text = txtFirstName.Text & " " & txtLastName.Text

End Sub
I don't know how to make these objects visible outside their own subroutines since I don't have a say as to where they're created. My only language experience is JAVA so I'm at a loss here. Any help would be greatly appreciated.

May 22 '07 #1
Share this Question
Share on Google+
6 Replies

P: 294
Ok. I think this will do what you want. I can explain anything if you want me to. You can use the SetFocus command to get the job done.

Expand|Select|Wrap|Line Numbers
  1. Public Sub Helping_Click()
  3. Dim var1, var2  As String
  4.      txtFirstName.SetFocus
  5.          var1 = txtFirstName.Text
  6.      txtLastName.SetFocus
  7.          var2 = txtLastName.Text
  8.      txtFullName.SetFocus
  9.          txtFullName.Text = var1 & " " & var2
  11. End Sub
Hope This Helps!
C. Cox
May 22 '07 #2

P: 4
It does indeed, Thank you very much
May 22 '07 #3

P: 294
It does indeed, Thank you very much
You are welcome. There may be a simpler way, but that is what I thought of right away.
May 22 '07 #4

Expert 2.5K+
P: 3,532
There is indeed a much simpler way! You simply change the Text property to the Value property.

txtFullName.Value = txtFirstName.Value & " " & txtLastName.Value

As you found out, for the Text property to work the control has to have focus. The Value property doesn't require this! And actually, because Value is the default property for textboxes, you can make it even simpler:

txtFullName = txtFirstName & " " & txtLastName

To be honest, I think the only reason the Text property is still around is to make Access backward compatible. Value is obviously much more flexible!
May 22 '07 #5

P: 294
Thats what I was wondering. I had never used the .text property until I saw this post. And I could not remember how I managed to set text box values in the past. That is what I did, thanks for the tag-team. :-)
May 22 '07 #6

P: 675
The text property has uses beyond backward compatability. It allows the program to monitor the entry of text data while the entry is in progress.

You might code as below and eliminate the stringButton altogether.
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtFirstName_Change()
  2.     txtFullName = txtFirstName.Text & " " & txtLastName
  3. End Sub
  5. Private Sub txtLastName_Change()
  6.     txtFullName = txtFirstName & " " & txtLastName.Text
  7. End Sub
May 25 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.