using screen object to reference active control

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I am using the below procedure to attempt to write to a blank text box.
Basically, I have 10 command buttons, labelled 1 thru 9 (and one with a 0)
and I want to use the procedure to write to the text box in focus. So what I
have is -- i click the command button labelled 3 and I want it to write the
numeral 3 to the text box in focus, then i select 7 and I want it to write
the 7 directly after the three --- and on


Private Sub cmdRnd5_Click()
Dim ctlThis As Control
Set ctlThis = Screen.ActiveControl
Me!ctlThis.Text = Me!ctlThis.Text & "" & cmdRnd5.Caption
End Sub

I get a run time 2465 error -- Access can't find the field ctlThis referred
to in my expression.

Any help on where I am discombobulated.

thanks
 
I am using the below procedure to attempt to write to a blank text box.
Basically, I have 10 command buttons, labelled 1 thru 9 (and one with a 0)
and I want to use the procedure to write to the text box in focus. So what I
have is -- i click the command button labelled 3 and I want it to write the
numeral 3 to the text box in focus, then i select 7 and I want it to write
the 7 directly after the three --- and on

Private Sub cmdRnd5_Click()
Dim ctlThis As Control
Set ctlThis = Screen.ActiveControl
Me!ctlThis.Text = Me!ctlThis.Text & "" & cmdRnd5.Caption
End Sub

I get a run time 2465 error -- Access can't find the field ctlThis referred
to in my expression.

Any help on where I am discombobulated.

thanks

Once you click the command button the active control has become the
previous control.
Does this work for you?

Private Sub cmdRnd5_Click()
' set focus to the previous control
Screen.PreviousControl.SetFocus
' which then becomes the active control
Me.ActiveControl = Me.ActiveControl & " " & Me!cmdRnd5.Caption
End Sub

Note.. The default property for a control is it's Value property, so
you do not need to specify it.
A Control's Text property is the value typed into the control before
being saved. Once you leave the control to click the command button,
it's saved, so it's now the control's Value property you wish to add
to.
 
When you set a reference to a contro you don't use Me! to refer to it: it's
simply ctlThis once it's been instantiated.

However, as Fred pointed out, once you click on the command button, it gets
focus.
 
Kevin said:
I am using the below procedure to attempt to write to a blank text
box. Basically, I have 10 command buttons, labelled 1 thru 9 (and one
with a 0) and I want to use the procedure to write to the text box in
focus. So what I have is -- i click the command button labelled 3 and
I want it to write the numeral 3 to the text box in focus, then i
select 7 and I want it to write the 7 directly after the three ---
and on


Private Sub cmdRnd5_Click()
Dim ctlThis As Control
Set ctlThis = Screen.ActiveControl
Me!ctlThis.Text = Me!ctlThis.Text & "" & cmdRnd5.Caption
End Sub

I get a run time 2465 error -- Access can't find the field ctlThis
referred to in my expression.

Any help on where I am discombobulated.

thanks

If you were always going to be "typing into" the same text box, this
question would be simpler. However, even as it is, you can probably use
a common function in all 10 of your command buttons. The function might
be something like this:

'---- start of function code -----
Private Function TypeButtonCaption()

Dim strDigit As String

strDigit = Me.ActiveControl.Caption

With Screen.PreviousControl
' Let's only do this if the previous control
' is a text box.
If .ControlType = acTextBox Then
.SetFocus
.Text = .Text & strDigit
.SelStart = Len(.Text)
End If
End With

End Function

'---- end of function code -----

Then you'd set the OnClick property of each command button to

=TypeButtonCation()
 
Back
Top