When "X" is not equal to "X" (1 Viewer)

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
I have the following code snippet that refuses to do as it is bid.
The idea is to catch an inadvertent "setfocus" to a control that already has focus.
From the screenshots it is obvious that the control name and the string "str" are the same yet the "Then" clause is still activated.

Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then Me.Controls(str).SetFocus
End Sub

Screenshot_56.jpg


Screenshot_57.jpg
 

June7

AWF VIP
Local time
Today, 01:56
Joined
Mar 9, 2014
Messages
5,472
How are you calling this Sub? Show that code.
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
Okay, but what event?
In the case that a query only returns one result, the combobox waits to be updated,
In this case I call combo_afterupdate which has the effect of the afterupdate event being triggered in the call and, it seems, again after "lostfocus"
The idea being if there is only one response, there is no requirement for user input. so go to the next combobox.
I am using this sub in two forms, one of which is required to work in two directions, see below.


Code:
Private Sub cboGenus_AfterUpdate()
    getTaxon
    Me.cboSpecies.Requery
    isInFocus "cboSpecies"
    If Nz(Me.cboSpecies.ItemData(1), "") > "" Then
        Me!cboSpecies.Dropdown
    Else
        cboSpecies_AfterUpdate
    End If
End Sub

Private Sub cboGenus_GotFocus()
    Me.cboGenus.BackColor = RGB(218, 255, 94)
    If Nz(sGenus, "") > "" Then
        Me.cboGenus = sGenus
        cboGenus_AfterUpdate
    Else
        If Me.cboGenus.ItemData(1) > "" Then
            Me.btnExit.Visible = True
            Me.btnReturn.Visible = False
            Me!cboGenus.Dropdown
        Else
            Me.cboGenus = Me.cboGenus.ItemData(0)
            Me.btnReturn.Visible = True
            cboGenus_AfterUpdate
        End If
    End If
End Sub


With this form if "Common name" is selected the input goes from common name to genus to species to collections.
if "botanical" is selected it goes from genus to species to common and collections.
Screenshot_58.jpg
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:56
Joined
May 7, 2009
Messages
19,243
it is highlighted, but will not be executed?

try using the long form:

Private Sub isInFocus(byval str As String)
If Screen.ActiveControl.Name <> str Then
Me(str).Setfocus
End If
End Sub
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
it is highlighted, but will not be executed?
It's highlighted because I was stepping through to get the screen shots.
I have tried the long version, same result but I haven't tried "by val"
The main problem is that it set's focus if the string and control name are the same or not.
It's as if the "If" statement is being ignored.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
I do not think so. Put a debug in Arnold's version and show it. Not sure what you think this does. If it is already the active control it does nothing. This code is useless.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:56
Joined
May 7, 2009
Messages
19,243
With this form if "Common name" is selected the input goes from common name to genus to species to collections.
if "botanical" is selected it goes from genus to species to common and collections.
you can add a Tab control (with 2 tabs and tab removed).
switching tab when a common or botanical is clicked.
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
I do not think so. Put a debug in Arnold's version and show it. Not sure what you think this does. If it is already the active control it does nothing. This code is useless.
That's precisely what it's supposed to do.
If the active control is the same as the control having setfocus applied then nothing happens, if not the new control becomes the active control.
You might need to read the entire post, it's all explained there.
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
you can add a Tab control (with 2 tabs and tab removed).
switching tab when a common or botanical is clicked.
Sounds good but the rowsource for the controls has to be changed to correct for the different inputs.

Code:
Private Sub btnBot_Click()
    sBtn = "Bot"
    Me.btnBot.BackColor = RGB(218, 255, 94)
    Me.btnCom.BackColor = RGB(180, 199, 231)
    ClearIt
    Me.cboGenus.RowSource = "QCommonG"
    Me.cboSpecies.RowSource = "QCommonS"
    Me.cboCommon.RowSource = "QCommonC"
    Me.cboGenus.Requery
    Me.cboGenus.SetFocus
End Sub

Private Sub btnCom_Click()
    sBtn = "Com"
    Me.btnCom.BackColor = RGB(251, 199, 231)
    Me.btnBot.BackColor = RGB(180, 199, 231)
    ClearIt
    Me.cboGenus.RowSource = "QCommonGC"
    Me.cboSpecies.RowSource = "QCommonSC"
    Me.cboCommon.RowSource = "QCommonCC"
    Me.cboCommon.Requery
    Me.cboCommon.SetFocus
    Me!cboCommon.Dropdown
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
If the active control is the same as the control having setfocus applied then nothing happens, if not the new control becomes the active control.
You might need to read the entire post, it's all explained there
Again it does nothing.
If CtrlX has focus do nothing else set focus to CtrlX
is simply
Set focus to ctrlX

So why the check?
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
Again it does nothing.
Read the code.
there is no "Else" clause involved.

Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then Me.Controls(str).SetFocus
End Sub

Case1. str = "cboSpecies"
me.activecontrol.name = "cbospecies".
if me.activecontrol.name <> str then nothing happens. >>cbospecies remains the active control.

Case2. str = "cboTables"
me.activecontrol.name = cbospecies.
if me.activecontrol.name <> str then me.controls(str).setfocus. >>cbotables becomes the active control
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
Read the code.
there is no "Else" clause involved.
I am not saying there is an Else I am saying the logic makes no sense.

This code is equivalent to simply setting the focus to the string.
Code:
Private Sub isInFocus(str As String)
     Me.Controls(str).SetFocus
End Su

Case 1.
Case1. str = "cboSpecies"
me.activecontrol.name = "cbospecies".
'if me.activecontrol.name <> str then nothing happens.
if the active control is cboSpecies then cboSpecies gets(maintains) focus
>>cbospecies has focus already and gets focus(nothing happens) remains the active control.

Case2. str = "cboTables"
me.activecontrol.name = cbospecies.
'if me.activecontrol.name <> str then me.controls(str).setfocus.
If cboTables does not have focus then
>>cbotables becomes the active control

If for some reason you think setting the focus to something that already has focus will trigger an event you are wrong.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
I see this check all the time
if Ctrl.visible = True then ctrl.visible = false
which is simpler as
ctrl.visible = false

I want the control to be invisible. If it is invisible already then fine, if not make invisible. the check is a waste of time. It does nothing.
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
I see this check all the time
if Ctrl.visible = True then ctrl.visible = false
which is simpler as
ctrl.visible = false

I want the control to be invisible. If it is invisible already then fine, if not make invisible. the check is a waste of time. It does nothing.
That is your interpretation. It is not what I have written.
In plain English.
If the input to the sub, str, is the same as the name of the active control then the active control stays active.
If the input to the sub, str, is NOT the same as the name of the active control then the active control changes to str.

Maybe you like this better;

Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name = str Then
        Exit Sub
    Else
        Me.Controls(str).SetFocus
    End If
End Sub

Which is the really long way of writing

Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then Me.Controls(str).SetFocus
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
f the input to the sub, str, is the same as the name of the active control then the active control stays active.
If the input to the sub, str, is NOT the same as the name of the active control then the active control changes to str.

And again it is simply the same as

Code:
Private Sub isInFocus(str As String)
     Me.Controls(str).SetFocus
End Sub

if the input to the sub, str, is the same as the name of the active control then the active control stays active.
If the input to the sub, str, is NOT the same as the name of the active control then the active control changes to str.

Can you explain how that is not the same?

Pass in "cboSpecies"
If cboSpecies has focus it still has focus
if cboSpecies does not have focus it now has focus

The if check does nothing.
 

John Sh

Member
Local time
Today, 19:56
Joined
Feb 8, 2021
Messages
410
Pass in "cboSpecies"
If cboSpecies has focus it still has focus
if cboSpecies does not have focus it now has focus

The if check does nothing.
Read what you have just written, you have proven yourself wrong.

<<<if cboSpecies does not have focus it now has focus>>>

or "if cbotables had focus, cbospecies now has focus" which is exactly the purpose of the sub.

The setfocus command will be applied, if, and only if, the currently active control is not the same as the new control to be made active.
QED.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
The setfocus command will be applied, if, and only if, the currently active control is not the same as the new control to be made active.
Do you think there is a difference between the active control and the control with focus?
What do you think happens if you set focus to a control that already has focus? If you think the answer is something besides "nothing" then you are wrong.
Do you want help with you problem, or do you want to tell us why we are wrong even though your code clearly is not working for you?
If you want we can help rewrite your code to make it work, but telling us that the sky is not blue is not going to help.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:56
Joined
May 21, 2018
Messages
8,529
Also the point of writing this out like this
Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name<> str Then
        Me.Controls(str).SetFocus
    End If
End Sub

was so that you can prove to yourself, like @arnelgp showed, your assumptions are wrong that the if check is not working. Not that it is a better construct
Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then
        debug.print "Code is firing. Active control " & me.activecontrol.name & " Passed string " & str
        Me.Controls(str).SetFocus
    End If
End Sub

now show a case where the if check fails. Need to learn how to debug your own code. This is one way to prove an if is working as expected.
 
Last edited:

Users who are viewing this thread

Top Bottom