How to get data from an unbound control? Why doesn't SetFocus work?

R

raylopez99

I've been able to get data for one textbox from a bound control but
not an unbound control (except by using global variables as per the
below). What am I missing?

Keeps returning a Null value, see the below.

RL

PS--Baz, if you're reading this, don't bother replying--you're fired.
Your boss speaking.

Private Sub Text19_Click() 'I want to get information from Textbox1
(unbound) from textbox Text19
Dim Str04 As String
Textbox1.SetFocus 'textbox must have focus to get value or .Text
property
If Me.Textbox1.Text <> Null Then
Str04 = Me.Textbox1.Text 'Never is triggered! Even if I type stuff in
Textbox1, it's always Null here
Else
Str04 = GlobalString + "GlobalStringNotSetFocus!" '<--Else statement
always triggered--i.e., Global Variable
End If
Text19.SetFocus
Text19.Text = Str04
End Sub

'''''''''''''''''''''

FROM THE HELP STUFF:
SetFocus Method
See AlsoApplies ToExampleSpecificsThe SetFocus method moves the focus
to the specified form, the specified control on the active form, or
the specified field on the active datasheet.

expression.SetFocus
expression Required. An expression that returns one of the objects
in the Applies To list.

Remarks
You can use the SetFocus method when you want a particular field or
control to have the focus so that all user input is directed to this
object.

In order to read some of the properties of a control, you need to
ensure that the control has the focus. For example, a text box must
have the focus before you can read its Text property.

Other properties can be set only when a control doesn't have the
focus. For example, you can't set a control's Visible or Enabled
properties to False (0) when that control has the focus.

You can also use the SetFocus method to navigate in a form according
to certain conditions. For example, if the user selects Not applicable
for the first of a set of questions on a form that's a questionnaire,
your Visual Basic code might then automatically skip the questions in
that set and move the focus to the first control in the next set of
questions.

You can move the focus only to a visible control or form. A form and
controls on a form aren't visible until the form's Load event has
finished. Therefore, if you use the SetFocus method in a form's Load
event to move the focus to that form, you must use the Repaint method
before the SetFocus method.

You can't move the focus to a control if its Enabled property is set
to False. You must set a control's Enabled property to True (-1)
before you can move the focus to that control. You can, however, move
the focus to a control if its Locked property is set to True.

If a form contains controls for which the Enabled property is set to
True, you can't move the focus to the form itself. You can only move
the focus to controls on the form. In this case, if you try to use
SetFocus to move the focus to a form, the focus is set to the control
on the form that last received the focus.


Tip

You can use the SetFocus method to move the focus to a subform, which
is a type of control. You can also move the focus to a control on a
subform by using the SetFocus method twice, moving the focus first to
the subform and then to the control on the subform.

Example
The following example uses the SetFocus method to move the focus to an
EmployeeID text box on an Employees form:

Forms!Employees!EmployeeID.SetFocus
 
B

Baz

raylopez99 said:
I've been able to get data for one textbox from a bound control but
not an unbound control (except by using global variables as per the
below). What am I missing?

Keeps returning a Null value, see the below.

RL

PS--Baz, if you're reading this, don't bother replying--you're fired.
Your boss speaking.

Still struggling I see, hotshot. How long did it take you to write that
pile of crap?

LOL!
 
L

Linq Adams via AccessMonster.com

Where to begin?

Drop the

Textbox1.SetFocus and Textbox1.Text

simply use

Textbox1.Value (or since Value is the default property for textboxes, simply
use Textbox1) .Value deosn't require that the control has focus to reference
it.

Instead of

If Me.Textbox1.Text <> Null Then

use

If Not IsNull(Me.Textbox1) Then

And while

Str04 = GlobalString + "GlobalStringNotSetFocus!"

works, because of Backward Compatibilty, it really should probably be


Str04 = GlobalString & "GlobalStringNotSetFocus!"

since you're joining two strings
 
B

Bob Quintal

@e4g2000hsg.googlegroups.com
:
I've been able to get data for one textbox from a bound control
but not an unbound control (except by using global variables as
per the below). What am I missing?
The basic knowledge that textbox1.text is not the property to use
for what you are doing.
The basic knowledge that any comparison with a null will never
return True

Private Sub Text19_Click()
If NOT IsNull(Me.Text19.Value Then
Me.Text19.Value = Me.TextBox1.value
Else
Me.Text19.Value = "I'm a wanker"
End If
end sub

The above can be reduced to a single statement by using the Nz()
function to
Me.Text19.Value = Nz(Me.TextBox1.value,"I'm a wanker")

Q

Keeps returning a Null value, see the below.

RL

PS--Baz, if you're reading this, don't bother replying--you're
fired. Your boss speaking.

Private Sub Text19_Click() 'I want to get information from
Textbox1 (unbound) from textbox Text19
Dim Str04 As String
Textbox1.SetFocus 'textbox must have focus to get value or .Text
property
If Me.Textbox1.Text <> Null Then
Str04 = Me.Textbox1.Text 'Never is triggered! Even if I type
stuff in Textbox1, it's always Null here
Else
Str04 = GlobalString + "GlobalStringNotSetFocus!" '<--Else
statement always triggered--i.e., Global Variable
End If
Text19.SetFocus
Text19.Text = Str04
End Sub

'''''''''''''''''''''

FROM THE HELP STUFF:
SetFocus Method
See AlsoApplies ToExampleSpecificsThe SetFocus method moves the
focus to the specified form, the specified control on the active
form, or the specified field on the active datasheet.

expression.SetFocus
expression Required. An expression that returns one of the
objects in the Applies To list.

Remarks
You can use the SetFocus method when you want a particular field
or control to have the focus so that all user input is directed to
this object.

In order to read some of the properties of a control, you need to
ensure that the control has the focus. For example, a text box
must have the focus before you can read its Text property.

Other properties can be set only when a control doesn't have the
focus. For example, you can't set a control's Visible or Enabled
properties to False (0) when that control has the focus.

You can also use the SetFocus method to navigate in a form
according to certain conditions. For example, if the user selects
Not applicable for the first of a set of questions on a form
that's a questionnaire, your Visual Basic code might then
automatically skip the questions in that set and move the focus to
the first control in the next set of questions.

You can move the focus only to a visible control or form. A form
and controls on a form aren't visible until the form's Load event
has finished. Therefore, if you use the SetFocus method in a
form's Load event to move the focus to that form, you must use the
Repaint method before the SetFocus method.

You can't move the focus to a control if its Enabled property is
set to False. You must set a control's Enabled property to True
(-1) before you can move the focus to that control. You can,
however, move the focus to a control if its Locked property is set
to True.

If a form contains controls for which the Enabled property is set
to True, you can't move the focus to the form itself. You can only
move the focus to controls on the form. In this case, if you try
to use SetFocus to move the focus to a form, the focus is set to
the control on the form that last received the focus.


Tip

You can use the SetFocus method to move the focus to a subform,
which is a type of control. You can also move the focus to a
control on a subform by using the SetFocus method twice, moving
the focus first to the subform and then to the control on the
subform.

Example
The following example uses the SetFocus method to move the focus
to an EmployeeID text box on an Employees form:

Forms!Employees!EmployeeID.SetFocus
 
M

Marshall Barton

C'mon guys, lighten up. It's easier, quicker and more
productive to just ignore things than to keep bashing each
other.
 
R

raylopez99

C'mon guys, lighten up.  It's easier, quicker and more
productive to just ignore things than to keep bashing each
other.
--

Thanks Linq and Bob--I'll try this and it explains why I was getting
weird error messages like "2185" - "you can't reference a ... conntrol
when the control lacks focus" for stuff like "If (XYZ <> Null)"

RL
 
R

raylopez99

Where to begin?

Thanks! Everything seems to be working again after I adopted your
convention. Yesterday was a day wasted because of the below, which
IMO should never have been compiled by Visual Basic for Access if it
was decent.

RL

'CORRECT:

If Not IsNull(Me.StockSymbol) Then
Str002G = Me.AcctID.Value
If Not IsNull(Me.AcctID) Then
Str001G = Me.StockSymbol.Value
End If
End If

'replaces (WRONG SYNTAX, gives obscure runtime errors)

If (Me.StockSymbol.Text <> Null And Me.AcctID.Text <> Null ) Then

... Me.StockSymbol.Text = ...

End If
 

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