Still not working - ".SetFocus" on a form element and then havingthe combo box ".Dropdown"

C

CES

All,
I'm sorry for the second post, I thought I solve the problem but I didn't...

I'm trying to figure out how to .SetFocus on a control and then use the .Dropdown property to display the contents of the combo box. The problem I'm encountering is once I move focus on to the control the combo box drops down, flickers, for a moment but then rolls back up.

Once I exit the TypeOf ComboBox, I'm locking/unlocking & enabling/disabling three possible controls using the onExit event of the TypeOf control based on the selected value.

(These form fields are in their tab stop order and are all enabled/unlocked when the new record is first displayed.)
TypeOf - ComboBox
Category - ComboBox
SubCategory - ComboBox
PaymentMethod - TxtBox
FinancalAccount - ComboBox

I'm assuming the problem I'm running into, is occurring because of the function fnEnableDisableTxtBox() because it is somehow changing focus while it is executing. But even if that is so, the code below should still execute properly by setting focus on the PaymentMethod control and then calling the Dropdown property.

If anyone has a clue as to what I am doing wrong I would appreciate any help. Thanks in advance. - CES

I believe the code below will now always fail to execute the dropdown property properly.


Public Function fnEnableDisableTxtBox(controlName As String, actionToTake As String)
' Call fnEnableDisableTxtBox("NameOfTextBox")
' Will Change to opisit of Current State

Dim tmp As Control

Set tmp = Me.Controls(controlName)

If actionToTake <> "Disable" Then
tmp.Enabled = True
tmp.Locked = False
tmp.BackStyle = 1
Else
tmp.Enabled = False
tmp.Locked = True
tmp.BackStyle = 0
End If

End Function


Private Sub TypeOf_Exit(Cancel As Integer)

If Me.TypeOf.Value = 2 Then
Me.FinancalAccount.Value = ""

Call fnEnableDisableTxtBox("Category", "")
Call fnEnableDisableTxtBox("SubCategory", "")
Call fnEnableDisableTxtBox("FinancalAccount", "Disable")

ElseIf Me.TypeOf.Value > 2 Then

If Me.TypeOf.Value = 3 Then
Me.Category.Value = 3
Else
Me.Category.Value = 4
End If

Me.SubCategory.Value = ""

Call fnEnableDisableTxtBox("Category", "Disable")
Call fnEnableDisableTxtBox("SubCategory", "Disable")
Call fnEnableDisableTxtBox("FinancalAccount", "")

Else

Call fnEnableDisableTxtBox("Category", "")
Call fnEnableDisableTxtBox("SubCategory", "")
Call fnEnableDisableTxtBox("FinancalAccount", "")

End If

Me.PaymentMethod.SetFocus
Me.PaymentMethod.Dropdown

End Sub
 
A

Arvin Meyer [MVP]

Make as many post as you like to solve your problem. Here is an example of
working code. The combo doesn't flicker, it stays dropped down until I click
somewhere else. Make sure you don't have something else going on that moves
the focus away from your combo:

Private Sub Form_Current()
Me.cboCategory.SetFocus
Me.cboCategory.Dropdown
End Sub
 
P

Peter Yang [MSFT]

Hello,

It seems that PaymentMethod shall be a ComboBox instead of a TxtBox. I
tried the code on my side and it seems work fine. The focus is set to
PaymentMethod comobox and its dropdown is displayed and I changed all the
controls to combobox instead, and use value list as row source type.

It seems that the issue might be related to this specific form or related
data source. Did you try a new form, add comobox controls and try this code
again?

If possible, you could send me an email with the sample database that have
this issue. Please remove "online" from my displayed email address. I'll
send my test database to you for your reference. Thank you.

Best Regards,

Peter Yang
MCSE2000/2003, MCSA, MCDBA
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
<http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx>.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
<http://msdn.microsoft.com/subscriptions/support/default.aspx>.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

CES

Arvin said:
Make as many post as you like to solve your problem. Here is an example of
working code. The combo doesn't flicker, it stays dropped down until I click
somewhere else. Make sure you don't have something else going on that moves
the focus away from your combo:

Private Sub Form_Current()
Me.cboCategory.SetFocus
Me.cboCategory.Dropdown
End Sub

Arvin,
Thank you for your response, however if you look at the bottom of the Sub TypeOf_Exit you will note that I in fact was setting focus and using the dropdown as you suggested.

Me.PaymentMethod.SetFocus
Me.PaymentMethod.Dropdown

I think that you are correct in your assumption that somehow something is drawing focus away from the combobox named - PaymentMethod, however the way that the sub is setup all work should have been completed prior to calling the SetFocus/Dropdown because the methods are being called after the if statement for TypeOf_Exit is fully executed.
It seems to me that the only possible explanation for why this is not working is that somehow one thread of the application is doing the fnEnableDisableTxtBox and another thread is doing the SetFocus/Dropdown and because it is happening simultaneously it is causing the problem. Unfortunately my knowledge of VBA is so limited that I'm not aware of any method available that would allow the code to "Wait until all previous work done by the function has been completed".

I have also tried to SetFocus on another control and then SetFocus back to the control that I want focus set to such as:

Me.SomeOtherControalThatIsAlwayesEnabled.SetFocus
Me.PaymentMethod.SetFocus
Me.PaymentMethod.DropDown

Just for your information it doesn't matter if I in fact call the function fnEnableDisableTxtBox() or if I just use in-line code such as the code below.

If you have any additional thoughts I would greatly appreciate them. Thank you for your help. - CES


This is the original code that I started out with before I set up the function fnEnableDisableTxtBox():

Private Sub TypeOf_LostFocus()

If Me.TypeOf.Value = 2 Then

Me.Category.Enabled = True
Me.Category.Locked = False
Me.Category.BackStyle = 1

Me.SubCategory.Enabled = True
Me.SubCategory.Locked = False
Me.SubCategory.BackStyle = 1


Me.FinancalAccount.Value = ""
Me.FinancalAccount.Enabled = False
Me.FinancalAccount.Locked = True
Me.FinancalAccount.BackStyle = 0

Me.Category.SetFocus
Me.Category.DropDown

ElseIf Me.TypeOf.Value > 2 Then
If Me.TypeOf.Value = 3 Then
Me.Category.Value = 3
Else
Me.Category.Value = 4
End If

Me.SubCategory.Value = ""

Me.Category.Enabled = False
Me.Category.Locked = True
Me.Category.BackStyle = 0

Me.SubCategory.Enabled = False
Me.SubCategory.Locked = True
Me.SubCategory.BackStyle = 0

If Me.PaymentMethod.Value > "" Then

Me.FinancalAccount.Enabled = True
Me.FinancalAccount.Locked = False
Me.FinancalAccount.BackStyle = 1
End If

Me.PaymentMethod.SetFocus
Me.PaymentMethod.DropDown

Else

Me.Category.Enabled = True
Me.Category.Locked = False
Me.Category.BackStyle = 1

Me.SubCategory.Enabled = True
Me.SubCategory.Locked = False
Me.SubCategory.BackStyle = 1

If Me.PaymentMethod.Value > "" Then
Me.FinancalAccount.Enabled = True
Me.FinancalAccount.Locked = False
Me.FinancalAccount.BackStyle = 1
End If

Me.Category.SetFocus
Me.Category.DropDown
End If
End Sub
 
C

CES

Peter said:
Hello,

It seems that PaymentMethod shall be a ComboBox instead of a TxtBox. I
tried the code on my side and it seems work fine. The focus is set to
PaymentMethod comobox and its dropdown is displayed and I changed all the
controls to combobox instead, and use value list as row source type.

It seems that the issue might be related to this specific form or related
data source. Did you try a new form, add comobox controls and try this code
again?

If possible, you could send me an email with the sample database that have
this issue. Please remove "online" from my displayed email address. I'll
send my test database to you for your reference. Thank you.

Best Regards,

Peter Yang
MCSE2000/2003, MCSA, MCDBA
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
<http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx>.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
<http://msdn.microsoft.com/subscriptions/support/default.aspx>.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Peter,
Thank you for your kind offer to look at this. When I first realized I had a problem I in fact did as you suggested which is delete the forum and then start from scratch, which is how I came up with the function fnEnableDisableTxtBox.

The "push me" button will open the correct form where I am encountering a problem "formAccountLedger" once the form is opened if you tab through to the 4 control (TypeOf) make a selection and then hit tab you will run into my problem. Once again thank you ever so much for looking at this. -- CES

I've posted the MDB file at http://209.132.211.66/tmp.mdb
 
A

Arvin Meyer [MVP]

Try adding:

DoEvents

immediately before the line:

Me.Category.SetFocus

That temporarily releases control to the OS, which is not exactly your
problem, but it just may work for you. At least you will be sure that there
are no outside processes interfering.
 
C

CES

Arvin said:
Try adding:

DoEvents

immediately before the line:

Me.Category.SetFocus

That temporarily releases control to the OS, which is not exactly your
problem, but it just may work for you. At least you will be sure that there
are no outside processes interfering.
Arvin,
I'll take a look at it tomorrow and get back to you. Thanks for all of your help. - CES
 
A

antman142

CES said:
[quoted text clipped - 34 lines]
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Peter,
Thank you for your kind offer to look at this. When I first realized I had a problem I in fact did as you suggested which is delete the forum and then start from scratch, which is how I came up with the function fnEnableDisableTxtBox.

The "push me" button will open the correct form where I am encountering a problem "formAccountLedger" once the form is opened if you tab through to the 4 control (TypeOf) make a selection and then hit tab you will run into my problem. Once again thank you ever so much for looking at this. -- CES

I've posted the MDB file at http://209.132.211.66/tmp.mdb

Hello , I couldn't help but to take a crack at this one. I was able to fix
the problem. I opened the DB, left some notes in the code behind and left the
DB as it was when I opened it. But here is what I found.

1. First, validating data the way you have it setup is tricky, especially
when you are trying to SetFocus.
2. I set a Breakpoint at the first IF statement in Private Sub TypeOf_Exit
then tabbed through your form.
upon exiting the [TypeOf] field the code, of course, broke.
3. I then proceeded to step-through the code, running through all three
functions. when the code was finished with the IF statement, the next line of
code was Me.PaymentMethod.SetFocus, in which it proceeded to do. But you also
had PaymentMethod_OnEnter where you had Me.PaymentMethod.Dropdown (FYI- Me.
ActiveControl.Dropdown works the same) .

So when the code went to setfocus it had to Enter the PayentMethod field,
upon which OnEnter fired and performed the .Dropdown, then made a return trip
back to TypeOf_Exit to finish the Sub. Where it ran the final line of code Me.
PaymentMethod.Dropdown and then ended the sub. I hope that sheds some light
on why it failed.

The Workaround-
1.Comment out the two lines in the TypeOf_Exit for the setfocus and dropdown.
2.Comment out the PaymentMethod_OnEnter, the whole thing
3. Create an Event procedure for the OnGotFocus event for the PaymentMethod
field.
4.Go to the properties sheet for Both Category and SubCategory and set
TabStop = No

What happens after all that-
1.all the code in TypeOf_Exit stays within scope and exits the TypeOf field.
2.next, after exiting, the cursor now enters the next field in the tabstop
(PaymentMethod),
3.after entering PM field OnGotFocus is set and thus firing the .Dropdown to
show all the values without
any flickering. so basically you can skip the setfocus and let Access
naturally tab (or set the focus) for you.
(in retrospect you could possibly keep the OnEnter code as long as the code
in the TypeOf_Exit didn't fire)

I apologize if this response seems long winded but I hope this helps.

Antman
 

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