keeping focus on combobox?

sierra467

Registered User.
Local time
Today, 18:18
Joined
Aug 1, 2005
Messages
66
I have 2 comboboxes (cboTeacher1 and cboTeacher2) and I do not want them to contain the same value. If the same value is chosen, I want a messagebox to show the error and keep the focus in the offending combobox.

I wrote the code below for cboTeacher1 and it functions in the case that your are making a selection in cboTeacher1; however, if you click ok on the msgbox and then press tab, the mesage box is not redisplayed and the focus is switched from cboTeacher1.

I tried placing the same code also in the cboTeacher_Exit event and when tabbing out of cboTeacher1, the msgbox is displayed, but the focus will not stay in cboTeacher1, it shifts to cboTeacher2.

How do I keep the focus locked in cboTeacher1 until both cboTeacher1 and cboTeacher2 contain different values.

Code:
Private Sub cboTeacher1_BeforeUpdate(Cancel As Integer)
    If cboTeacher1.Column(1) = Me.cboTeacher2.Column(1) Then
    '   Both Primary and Secondary Teachers are the same
        'Determine if either Teacher cbo contains a null value
        If IsNull(cboTeacher1) = False Or IsNull(cboTeacher2) = False Then
        '   Alert the User of the discrepancy
            MsgBox "You can not have the same teacher's name in bowth Primary and Secondary Teacher"
            Me.SetFocus
        End If
    End If
End Sub

Thanks
 
Try canceling the update. Put Cancel = True after your messagebox.
 
Code:
Private Sub Combo1_AfterUpdate()
    
    If Me.Combo1 = Me.Combo2 Then
        MsgBox "You can not have the same teacher as both Primary and Secondary teacher"
        Me.Combo1 = Null
    End If

End Sub

the focus should remain on the first combobox.

i don't think you want a BeforeUpdate event. once you've made a selection, you are in AfterUpdate 'mode'. (i'm not sure if the = Null part is the best way to do this, but it works).
 
Last edited:
Thanks, That worked.

Is there a better place to put the code that I wrote that would handle the 2 events (BeforeUpdate and Exit)? It just seems redundent that I have to put the same code in the 2 different events.

Thanks for the help.
 
I have a form that does a similar check. If the object is left blank, they get a message box and then the offending object then recieves focus.

The code actually includes the object name as well as it makes the combo box drop down to show you need to select an item.

try this for yours.

cboTeacher1.SetFocus
cboTeacer1.dropdown

but since you are firing the event on the particular object the me. is fine. Just add the me.dropdown.

Perhaps, also put your code on the after update event as well. So, that if they choose the incorrect teacher again, the msgbox will fire once again until they select the correct one.

HTH,
If I don't make sense, just let me know and I will try to explain once again. :D
 
Ooppss to late. :D

Why not create a function that will peform the check for you, and call the function on each event you need it to check.
 
Thanks wazz,

Yes it does work, the only thing is that I would rather the old choice remain visible for the user to see their mistake instead of leaving the combobox blank. Thanks for the great reply.

Sierra
 
Selenau837,

I tried your .Dropdown suggestion and it did not really seem to do anything either. i do no know a lot about the .dropdown method, so I will have to look into that more.

Thanks.
 
sierra467 said:
I would rather the old choice remain visible for the user to see their mistake instead of leaving the combobox blank.

Sierra
you could remove
Code:
        Me.Combo0 = Null
if the user wants to leave the record but hasn't fixed the error you can add the entire code (without the msgbox perhaps) to the form's BeforeUpdate event.
 
Last edited:
sierra467 said:
Selenau837,

I tried your .Dropdown suggestion and it did not really seem to do anything either. i do no know a lot about the .dropdown method, so I will have to look into that more.

Thanks.
Welcom, well I am not sure how your combo box works, but my list only has a few items in it, and it will display them.

However, I would still try the fucntion instead of using that code on all the events. I am a huge fan of functions when it comes to validating information and has to be checked on muliple events. But, then again I am still considered a novice, so if there is a better way than that, I am eager to learn as well.

Good luck,
 
Thanks for all for a great bunch of suggestions! The one I went with was one told to me by a friend and it was so simple, I thought I would share it. I usually find progremming the solutions the esiest for me, but this time I used the Validation Rule and Validation Text Properties in the Properties list.

I used entered "<> cboTeacher1" (or could have used the expression builder)in the Validation Rule field and placed "You can not have the same teacher's name in both Primary and Secondary Teacher" in the Validation Text property field.

This handled the tabing issue, the selection issue, and "the not letting the user leave the cbo" issue if a duplicate entry wasa in cboTeacher2.

Thanks Again Everyone!!
 
sierra467 said:
Thanks for all for a great bunch of suggestions! The one I went with was one told to me by a friend and it was so simple, I thought I would share it. I usually find progremming the solutions the esiest for me, but this time I used the Validation Rule and Validation Text Properties in the Properties list.

I used entered "<> cboTeacher1" (or could have used the expression builder)in the Validation Rule field and placed "You can not have the same teacher's name in both Primary and Secondary Teacher" in the Validation Text property field.

This handled the tabing issue, the selection issue, and "the not letting the user leave the cbo" issue if a duplicate entry wasa in cboTeacher2.

Thanks Again Everyone!!

Thanks for posting how you got it fixed.
 

Users who are viewing this thread

Back
Top Bottom