Rewriting repeated blocks of code for combo boxes

thebionicredneck

Registered User.
Local time
Today, 21:05
Joined
May 9, 2013
Messages
16
Hi everyone,

I just recently started programming in MS Access. I have some forms with lots of combo boxes. I do not want users to type anything in the boxes, but to only select stuff that is contained in the drop down menus.

I found this useful code and I am using it in my combo boxes on the key down event to do what I want.

Private Sub cmb_a_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> vbKeyTab And KeyCode <> vbKeyReturn Then KeyCode = 0
End Sub

Private Sub cmb_b_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> vbKeyTab And KeyCode <> vbKeyReturn Then KeyCode = 0
End Sub

Private Sub cmb_c_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> vbKeyTab And KeyCode <> vbKeyReturn Then KeyCode = 0
End Sub

The issue is because I have so many combo boxes, is there a better way to rewrite the code so I don't have to repeat the same code in 20 places?



Thank you.
 
So you aren't wanting to just have the LIMIT TO LIST property set to YES and then don't allow new values? It is easier.
 
Hi Bob,

I used that, but it allows people to enter data then it does the validation afterwards. I would rather prefer users not to be able to enter anything at all.
 
Actually, this one is a little easier…

In the Property sheet for the On Key Down event of the Combo Boxes:-

=CancelKeyDown()


In a standard module:-
Code:
Public Function CancelKeyDown()
    
    DoCmd.CancelEvent

End Function

Chris.
 
Thanks to Chris and Bob. Chris's final answer gives me exactly what I need and is much easier
 

Users who are viewing this thread

Back
Top Bottom