Connecting Tech Pros Worldwide Forums | Help | Site Map

SetFocus does not work in Access form

Newbie
 
Join Date: Sep 2009
Location: Rhode Island
Posts: 5
#1: Sep 16 '09
I have an unbound field on a form. In the onClick event I have code to populate a text field. I first setFocus to it and enable it. Both of these actions work. But when my code reaches the the line whete I set the textfieldname.text to a value, I get an error: You can`t reference a property or method unless the control has focus.

My code is:
Expand|Select|Wrap|Line Numbers
  1. txtFileName.Enabled = True
  2. txtFileName.Visible = True
  3. txtFileName.SetFocus
  4. txtFileName.Text = "some value"
The error occurs on the last line.

Expert
 
Join Date: Sep 2007
Location: VA
Posts: 416
#2: Sep 17 '09

re: SetFocus does not work in Access form


Are you able to use the Value property instead? Also have you tried the syntax Me!txtFileName.SetFocus. I haven't done much with access so those might not work, but we do have a specific forum section for these types of questions so a moderator will probably move this topic over there.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,216
#3: Sep 18 '09

re: SetFocus does not work in Access form


Quote:

Originally Posted by Veeves View Post

I have an unbound field on a form. In the onClick event I have code to populate a text field. I first setFocus to it and enable it. Both of these actions work. But when my code reaches the the line whete I set the textfieldname.text to a value, I get an error: You can`t reference a property or method unless the control has focus.

My code is:
txtFileName.Enabled = True
txtFileName.Visible = True
txtFileName.SetFocus
txtFileName.Text = "some value"

The error occurs on the last line.

Expand|Select|Wrap|Line Numbers
  1. Dim txtB As TextBox
  2.  
  3. Set txtB = Me![txtFileName]
  4.  
  5. txtB.Enabled = True
  6. txtB.Visible = True
  7. txtB.SetFocus
  8. txtB.Text = "some value"
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,725
#4: Sep 18 '09

re: SetFocus does not work in Access form


I checked the help file & I can't see why that wouldn't work.

Does this happen in all circumstances? perhaps there's something else going on we don't know about.
Newbie
 
Join Date: Sep 2009
Location: Rhode Island
Posts: 5
#5: Sep 18 '09

re: SetFocus does not work in Access form


This code was working until I added a couple of queries and changed some code in a different function. I don`t get it.
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 253
#6: Sep 18 '09

re: SetFocus does not work in Access form


Does any of those changes have an effect on your current form? If so, setting focus should be the last thing you do, to allow for all the changes to happen.

-AJ
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#7: Sep 18 '09

re: SetFocus does not work in Access form


What happens if you omit the last line of code, simply using
Expand|Select|Wrap|Line Numbers
  1. txtFileName.Enabled = True
  2. txtFileName.Visible = True
  3. txtFileName.SetFocus
Is the cursor blinking in the textbox?

Welcome to Bytes!

Linq ;0)>

BTW, your original code works just fine for me!
Newbie
 
Join Date: Sep 2009
Location: Rhode Island
Posts: 5
#8: Sep 18 '09

re: SetFocus does not work in Access form


If I omit the last line: txtFileName.Text = "some value", then I avoid the error but don`t get the textbox filled with a value.

I changed txtFileName.Text to txtFileName.Value, and this works but the textbox shows as empty instead of the text I want.

I did some investigating and found that the problem starts when I delete all the records from the table which is the record source for the form using:
CurrentDb().Execute "DELETE FROM Primary_Data"

After running this the problems with SetFocus started.
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 253
#9: Sep 18 '09

re: SetFocus does not work in Access form


try this:
Expand|Select|Wrap|Line Numbers
  1. Dim txtB As TextBox
  2.  
  3. Set txtB = Me![txtFileName]
  4.  
  5. txtB.Enabled = True
  6. txtB.Visible = True
  7. txtB.SetFocus
  8. txtB.Value= "some value"
  9. txtB.Requery
  10.  
the requery should display the value after it's been assigned, if the value still exists.

-AJ
missinglinq's Avatar
Moderator
 
Join Date: Nov 2006
Location: Richmond, Virginia USA
Posts: 3,000
#10: Sep 18 '09

re: SetFocus does not work in Access form


Quote:

Originally Posted by Veeves View Post

If I omit the last line: txtFileName.Text = "some value", then I avoid the error but don`t get the textbox filled with a value.

You didn't answer my question, though! If you omit the last line, then run the code (click the button,) does the textbox, in fact, receive the focus, i.e. does the cursor appear in the textbox?

Linq ;0)>
Newbie
 
Join Date: Sep 2009
Location: Rhode Island
Posts: 5
#11: Sep 18 '09

re: SetFocus does not work in Access form


If I omit the last line: txtFileName.Text = "some value", then the textbox does receive focus.

One time I stopped execution right after the textBox.SetFocus. All the properties had a value of :Cannot access the property of a control that does not have focus (not exactly this message but very close), with one exception, the value property had a value and I was able to display it.
Newbie
 
Join Date: Sep 2009
Location: Rhode Island
Posts: 5
#12: Sep 18 '09

re: SetFocus does not work in Access form


Yes the textbox.requery does display the value. Thanks.
Reply

Tags
access, setfocus