Roll items in list problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I combined Allen Browne's roll dates code and Ken Snell's spin button code to
roll between items in a combo box. This works fine (many thanks to both). I
have code in the AfterUpdate event to enable the appropriate conttrols and
set the focus based on the contents of this combo box. If I use the + or -
keys to change the selection the After Update code is ignored and the focus
moves to the next control instead. How can I correct this?

Here are both codes:
Private Sub MileageDescription_KeyPress(KeyAscii As Integer)
Dim lngCboItem
On Error Resume Next
Select Case KeyAscii
Case 43 ' Plus key
KeyAscii = 0
lngCboItem = Me.MileageDescription.ListIndex + 1
If lngCboItem <= Me.MileageDescription.ListCount - 1 Then _
Me.MileageDescription.Value =
Me.MileageDescription.ItemData(lngCboItem)
Case 45 ' Minus key
KeyAscii = 0
lngCboItem = Me.MileageDescription.ListIndex - 1
If lngCboItem >= 0 Then Me.MileageDescription.Value =
Me.MileageDescription.ItemData(lngCboItem)
Me.MileageDescription.Value =
Me.MileageDescription.ItemData(lngCboItem)
End Select
End Sub

Private Sub MileageDescription_AfterUpdate()
On Error GoTo Err_MileageDescription_AfterUpdate
With Me
Select Case .MileageDescription
Case "State Line"
.State.Enabled = True
.State.SetFocus

Case "Fuel"
.Gallons.Enabled = True
.Gallons.SetFocus
.Amount.Enabled = True

Case "Stop"
.StopPurpose.Enabled = True
.StopPurpose.SetFocus
.Location.Enabled = True
.Location.Requery

Case "Trip End"
.Location.SetFocus
.Location.Enabled = True
.Location.Requery

Case "Wrecker"
.State.Enabled = True
.State.SetFocus
End Select

.State_Cover.Visible = Not .State.Enabled
End With


Exit_MileageDescription_AfterUpdate:
Exit Sub

Err_MileageDescription_AfterUpdate:
MsgBox Err.Description
Resume Exit_MileageDescription_AfterUpdate
End Sub

Thanks in advance.
 
Yes, if you change the value of a control programmatically (as that code
does), the AfterUpdate event does not fire.

You could solve that by declaring a module-level variable to track that it
changed, and then testing it in the Exit event of the control.

1. In the General Declarations section of your form's module (top, with the
Option statements):
Dim mbChanged As Boolean

2. In the Enter event of the control:
mbChanged = False

3. In the KeyPress event, add the line:
mbChanged = True

4. In the Exit event of the control:
If mbChanged Then
Call MileageDescription_AfterUpdate
mbChanged = False
end If
 
Thanks so much. That worked fine, not that I ever had any doubts. Now
though, if an item is selected from the keyboard, when you tab out an error
message appears saying Access connot move the focus to the specific control.
You say OK and the focus is where it should be. I tried adding an Else
statement to the Exit event to Call the AfterUpate event with no results. Is
there a way to correct this?
Thanks again.
 
Try adding this line to the end of the control's AfterUpdate code:
mbChanged = False
That should stop it executing twice.

If that doesn't work, you could try the LostFocus event instead of Exit.
 
I gather Access is trying to set focus twice which is causing the error
message. I've tried both suggestions along with several others of my own but
nothing has worked. Do you have any other ideas?
Thanks.
 
You can stop the code from executing twice, if you set the variable to False
as soon as it has run, and then run it again only if the variable is true.
 
Thanks Allen! I got it worked out. I moved the mbChanged = False to the
beginning of the after update code and that solved the problem.

Thanks again!
 
Back
Top