When "X" is not equal to "X" (2 Viewers)

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
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.
Do you think there is a difference between the active control and the control with focus? Of course not, they are one and the same.
What do you think happens if you set focus to a control that already has focus? Obviously you cannot do that
Do you want help with you problem, I'm intrigued to know your solution.
 

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
what I wrote is this,
Private Sub isInFocus(str As String)
If Me.ActiveControl.Name = str Then
Exit Sub <<<<<<<<<<<<<<<<<<
Else <<<<<<<<<<<<<<<<
Me.Controls(str).SetFocus
End If
End Sub

Not this
Private Sub isInFocus(str As String)
If Me.ActiveControl.Name = str Then
?????????????????????
Me.Controls(str).SetFocus
End If
End Sub

You have conveniently left out
" Exit Sub
Else"

Please enlighten me with your version of a sub that will change the active control if the input string is different to the active control's name.
 

ebs17

Well-known member
Local time
Today, 13:12
Joined
Feb 7, 2020
Messages
1,946
When analyzing and coming up with a possible solution, I would start by determining where my code is at. ActiveControl and Me are variable objects; in a more extensive form a focus can shift to something completely different.
Code:
Debug.Print Me.ActiveControl.Name, Screen.ActiveControl.Name, Screen.ActiveForm.Name
Do you notice any deviations from what was expected?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:12
Joined
Feb 28, 2001
Messages
27,186
There are two problems here.

First, there is the discussion of the semantics of your code. Second, you claim that something is not working.

In strict assertoric logic, the statement A <-- A or TRUE leads to A always being true for any A. In terms of your logic flow you are asking the question "does A have focus" to be followed by forcing A to have focus. But in assertoric logic, rules of simplification state that you can remove all other elements outside of the "or TRUE" because that syntax is provably identical to A <-- TRUE. I.e. don't bother with the possible comparison. Just set it to true. There IS no difference in the result.

Which leads to the second problem. You claim that the IF doesn't work correctly. Sub-question A: How do you know? Sub-question B: Why do you care? Does the selected control NOT get focus? You would be easily able to know if some GotFocus/LostFocus events fired, I guess, but why does it matter? Is there ever a case where the code runs and focus is still in the wrong place? THEN I would be worried.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:12
Joined
May 21, 2018
Messages
8,529
Please enlighten me with your version of a sub that will change the active control if the input string is different to the active control's name.
I already did it
Code:
Private Sub isInFocus(str As String)
     Me.Controls(str).SetFocus
End Su

If the str does not equal the active control then the control matching the string name gets focus
If the str equals the active control then focus stays with the active control

What am I missing?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:12
Joined
May 7, 2009
Messages
19,243
you can also pass the control to the function (see form2).
 

Attachments

  • Database1.accdb
    436 KB · Views: 17

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
I already did it
Code:
Private Sub isInFocus(str As String)
     Me.Controls(str).SetFocus
End Su

If the str does not equal the active control then the control matching the string name gets focus
If the str equals the active control then focus stays with the active control

What am I missing?
Au contraire.
You have left out the comparison. Your code actually does nothing.
The code, below from arnelgp mirrors my code but passes the new control instead of a string.
Code:
Private Sub isInFocus(ByRef Ctl As Control)
If Not (ActiveControl Is Ctl) Then
    Ctl.SetFocus
End If
End Sub

Note the "IF" statement that is missing in your code.

Ebs17. I have run that with a msgbox many times with the correct input every time.

DocMan.
We have lost site of the original problem here, which is the If statement was not recognising when the input string and the active control were the same and attempted to setfocus to the active control.

MajP.
Please compare my original code with arnel's code and your code. You continue to remove the if statement and that certainly will not do anything.

This has been a somewhat frustrating discussion.
I thank you all for your input and especially arnegp for his slightly modified, but profoundly more elegant, code.
John
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:12
Joined
May 21, 2018
Messages
8,529
You have left out the comparison. Your code actually does nothing.
Yes my code does the exact same amount of nothing that your code does. That is the point.
 

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
you can also pass the control to the function (see form2).
Thank you.
That does exactly what I want, although I did get trapped where there was no active control.
I can't use it in a load event but it's fine in the body of the code.
I think my original code was somehow unable to distinguish the difference between control.name and a string.
Once again, my heartfelt thanks.
John
 

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
you can also pass the control to the function (see form2).
Actually it only appears to work.
It returns the contents of the control, not the name. So if I put 1 in each combo there is no change of focus.
I tried adding .name to the controls but Access says "Type mismatch".
John
 

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
you can also pass the control to the function (see form2).
I've changed the code to the following and it now works correctly, even with the same value in each combo.
You certainly put me on the right track.

Code:
Private Sub isInFocus(ByRef Ctl As Control)
    If ActiveControl.Name <> Ctl.Name Then Ctl.SetFocus
End Sub
John
 

Minty

AWF VIP
Local time
Today, 12:12
Joined
Jul 26, 2013
Messages
10,371
PMFJI, But the logic here is skewed
I think I'm with @MajP here. Shirley you can just use

Code:
Private Sub isInFocus(ByRef Ctl As Control)
     Ctl.SetFocus
End Sub

After all, if the active control has focus AND is the Control passed to the function it won't matter as it already has the focus.
And if it doesn't you just set it to the control you passed in anyway?

If you can explain to me why that doesn't work I'm all ears, or feet or toes or something...
 

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
PMFJI, But the logic here is skewed
I think I'm with @MajP here. Shirley you can just use

Code:
Private Sub isInFocus(ByRef Ctl As Control)
     Ctl.SetFocus
End Sub

After all, if the active control has focus AND is the Control passed to the function it won't matter as it already has the focus.
And if it doesn't you just set it to the control you passed in anyway?

If you can explain to me why that doesn't work I'm all ears, or feet or toes or something...
If a control has focus and you try to setfocus to that control you will get an error.
Majp insists on leaving out the "if activecontrol is not the same as new control then change the active control"
This is my original code
Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then Me.Controls(str).SetFocus   This line is critical but doesn't function as expected. it's comparing apples and       oranges. the "IF" statement is not in Majp's code.
End Sub

Which has morphed into this variation of Arnelgp's code.
Again with the critical IF statement.

Code:
Private Sub isInFocus(ByRef Ctl As Control)
    If ActiveControl.Name <> Ctl.Name Then Ctl.SetFocus   This line works because it compares apples with apples.
End Sub

I think the some of the confusion here is in the name of the sub vis "isinfocus". This is a subroutine name, not a statement of fact.
If I changed the name to "Whatsgotfocus" would that help?
 

KitaYama

Well-known member
Local time
Today, 20:12
Joined
Jan 6, 2022
Messages
1,541
If a control has focus and you try to setfocus to that control you will get an error.
How did you come to this conclusion?
You can set focus on any control as long as it's not disabled.

This code sets the focus on the active control. And the active control always has the focus.
Screen.ActiveControl.SetFocus
Does this line raise an error for you?

@MajP @Minty
Though I understand your take, I'm still interested in why the IF clause fails the validation.
The images in #1 post, shows both sides as "cmbTables", yet the Then side is executed.
 
Last edited:

John Sh

Member
Local time
Today, 21:12
Joined
Feb 8, 2021
Messages
410
How did you come to this conclusion?
You can set focus on any control as long as it's not disabled.

This code sets the focus on the active control. And the active control always has the focus.
Screen.ActiveControl.SetFocus
Does this line raise an error for you?
I stand corrected on that point, however <You can set focus on any control as long as it's not disabled.> and visible
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:12
Joined
May 21, 2018
Messages
8,529
@John Sh
A little suggestion. You come to the forum asking for help, but you seem to be putting all your energy into telling everyone trying to help that they are wrong and do not know what they are talking about. I do not get it. Instead of even contemplating what we suggest search for little ways to nick pick what we are saying to prove somehow that you know better. Everyone here is a volunteer and your arrogant condescending attitude does not really make people want to help you. You seem convinced that I do not know what I am talking about. You fail to answer any of my questions, ponder what I suggest, but just look for an argument. You can look at my thousands of post on this forum to see if I know a thing or two about Access and VBA.
If you want to be welcomed here and get good assistance, open up your mind and ears and maybe close your mouth for just a second. Spend more time trying to solve problems than arguing. It is ridiculous that It took 35 threads and half the forum members chiming in before you even tried to listen. We likely could have solved the problem if we were focused on the issue instead of you telling us we were wrong. I guarantee we can help solve any of your problems, but your are making it pretty hard for people to want to help. I will bend over backwards to solve extremely complex problems for people who are appreciative of the assistance. When you are ready lets talk the real issues.

If you have an issue with what I said you can PM me direct.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:12
Joined
May 21, 2018
Messages
8,529
Though I understand your take, I'm still interested in why the IF clause fails the validation.
The images in #1 post, shows both sides as "cmbTables", yet the Then side is executed.
@KitaYama,
As @arnelgp pointed out in the first thread that image proves nothing. That only shows an evaluation taking place and not that the if check failed.
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
One more time! The point was in this long format you could easily prove to yourself if the if check failed which I highly doubt is happening. This is where people need to learn how to debug their own code instead of arguing that they are right.

Code:
Private Sub isInFocus(byval str As String)
  If Screen.ActiveControl.Name <> str Then
     Msgbox "Active Control: " & screen.activecontrol.name & " Str: " & str
     Me(str).Setfocus
  End If
End Sub

Now if there is a case where I see the msgbox shows

"Active control: cboGenus Str: cboGenus"

then I know there is a problem, and I will shut up because I do not know what I am talking about.


I asked for this test, but @John Sh was to busy telling me I do not know what I am talking about to give it a try and prove to themself.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:12
Joined
May 21, 2018
Messages
8,529
I'm sorry. I didn't mean it that way.
My apologies.
Sorry, that was definitely not directed to you.
I asked the OP to demo the code to see if the if check really failed, but the OP could not be bothered to try it and respond.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:12
Joined
Feb 28, 2001
Messages
27,186
If a control has focus and you try to setfocus to that control you will get an error.
Majp insists on leaving out the "if activecontrol is not the same as new control then change the active control"
This is my original code
Code:
Private Sub isInFocus(str As String)
    If Me.ActiveControl.Name <> str Then Me.Controls(str).SetFocus   This line is critical but doesn't function as expected. it's comparing apples and       oranges. the "IF" statement is not in Majp's code.
End Sub

Which has morphed into this variation of Arnelgp's code.
Again with the critical IF statement.

Code:
Private Sub isInFocus(ByRef Ctl As Control)
    If ActiveControl.Name <> Ctl.Name Then Ctl.SetFocus   This line works because it compares apples with apples.
End Sub

I think the some of the confusion here is in the name of the sub vis "isinfocus". This is a subroutine name, not a statement of fact.
If I changed the name to "Whatsgotfocus" would that help?

OK, let's clarify something. If you have this code:

Code:
Private Sub isInFocus(ByRef Ctl As Control)
    If ActiveControl.Name <> Ctl.Name Then Ctl.SetFocus   This line works because it compares apples with apples.
End Sub

and activate it like this:

Code:
IsInFocus me.controls( "some name string" )

The EXACT SAME RESULT would occur if you simply used

Code:
me.controls("some name string").SetFocus

This CAN fail in one circumstance: If the running code is doing so behind the scenes because of a particular non-control-related event (such as a timer, form_error, form_open - for a form just being launched because of clicking another control on another form, etc.) then it is possible that the control named as ActiveControl.Name doesn't exist within the scope of the code being executed. However, you displayed evidence that the two referenced names were the same so that isn't likely the case here.

MajP explained things many times, and I have tried to do so myself. I find your attitude puzzling. So let's take another tactic before this devolves into a series of nasty-grams. WHY do you insist that something went wrong? HOW DO YOU KNOW?
 

Users who are viewing this thread

Top Bottom