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
 
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
 
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
 

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