Must SetFocus! May not SetFocus!

G

Guest

I have built an ADP which includes a form that contains bound & unbound
controls, some of which are updated by event routines.

When I switch the form from Design View to Form View, I get:

"Run-time error '2110': ... can't move the focus to the control txtRanks."

If I then click <Debug> I find that execution is halted at the line

txtRanks.SetFocus

and that this line executes with no problem using F5 or F8. The form is then
OK until the next time I switch to Design View and back again.

If I take out the offending line, I instead get the error

"Run-time error '21850': You can't reference a property or method for a
control unless the control has the focus."

Sounds like the warden in Cool Hand Luke ("Why's this hole here? / Why is
this hole filled up?").

How do I get past this?
 
R

ruralguy via AccessMonster.com

What version of Access and where are you using the txtRanks.SetFocus code?
What else are you trying to do in this code? Can you post all of the code in
the event?
 
G

Guest

Thanks for the reply.

I'm not sure how much code I should burden you with. Let me know if the
following is insufficient.

(BTW: I'm using Access 2003.)


Private Sub Form_Load()

If strParameters = "" Then Call SetNames ': assigns public string
variables

' Repopulate the list of input tables in the database ...

Call ReadParameters ': reads a lookup table into an ADODB
RecordSet
' , and assigns public variables

Call Init_Controls ': assigns values to unbound controls

End Sub
'

Sub Init_Controls()

Call Update_Table_List(True) ': populates a combo box

txtInputTableFilter.SetFocus
txtInputTableFilter.Text = strInputTableFilterAvail

txtRanks.SetFocus
txtRanks.Text = strRanksAvail

grpCustomerType.Value = CustomerType

End Sub
'

Private Sub txtRanks_LostFocus()

' Must use this instead of _Change event!

strRanksAvail = txtRanks.Text

End Sub
'
 
R

RoyVidar

Allen_N said:
Thanks for the reply.

I'm not sure how much code I should burden you with. Let me know if
the following is insufficient.

(BTW: I'm using Access 2003.)


Private Sub Form_Load()

If strParameters = "" Then Call SetNames ': assigns public
string variables

' Repopulate the list of input tables in the database ...

Call ReadParameters ': reads a lookup table into an ADODB
RecordSet
' , and assigns public
variables

Call Init_Controls ': assigns values to unbound
controls

End Sub
'

Sub Init_Controls()

Call Update_Table_List(True) ': populates a combo box

txtInputTableFilter.SetFocus
txtInputTableFilter.Text = strInputTableFilterAvail

txtRanks.SetFocus
txtRanks.Text = strRanksAvail

grpCustomerType.Value = CustomerType

End Sub
'

Private Sub txtRanks_LostFocus()

' Must use this instead of _Change event!

strRanksAvail = txtRanks.Text

End Sub
'

I'm guessing you're a VB programmer, not Access VBA programmer ;)

In Access VBA, one will usually refer to the .Value property of
controls, not the .Text property, the latter which also requires the
control to have focus.

Most often, we will also qualify through usage of Me (representing the
form class)

strRanksAvail = Me!txtRanks.Value

or just

strRanksAvail = Me!txtRanks

since .Value is the default property (and without any .SetFocus).
 
R

ruralguy via AccessMonster.com

Apply Roy's suggestions. He knows of what he speaks. :)
Allen_N said:
Thanks for the reply.
[quoted text clipped - 74 lines]
I'm guessing you're a VB programmer, not Access VBA programmer ;)

In Access VBA, one will usually refer to the .Value property of
controls, not the .Text property, the latter which also requires the
control to have focus.

Most often, we will also qualify through usage of Me (representing the
form class)

strRanksAvail = Me!txtRanks.Value

or just

strRanksAvail = Me!txtRanks

since .Value is the default property (and without any .SetFocus).
 
G

Guest

RoyVidar said:
I'm guessing you're a VB programmer, not Access VBA programmer ;)

Shows, does it?
In Access VBA, one will usually refer to the .Value property of
controls, not the .Text property, the latter which also requires the
control to have focus.

Thanks for the tip.
Most often, we will also qualify through usage of Me (representing the
form class)

I do use Me, usually. Sometimes I even pass it as a subprogram argument so
that multiple forms and reports can run common code.

-- Allen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top