Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wild Combobox? 1

Status
Not open for further replies.

georgp

Technical User
Mar 28, 2002
96
US
Hi,

I have a form with several comboboxes which should only accept values from the list. I have set the NotInList event, but in addition I added these two modules, which should fire on entering the combobox and on Keypress:

Private Sub ucbo_Starter_Enter()
sOldValue = ucbo_Starter
If Len(Nz(ucbo_Starter)) <> 0 Then Exit Sub
If ucbo_Starter.Locked = True Then Exit Sub
Call fEnterControl(&quot;full composition&quot;)
End Sub

Private Sub ucbo_Starter_KeyPress(KeyAscii As Integer)
If KeyAscii = 9 Or KeyAscii = 13 Then Exit Sub
MsgBox &quot;You can not change entries in this combo box manually.&quot; & vbCr _
& &quot;Please select an item from the dropdown list.&quot;
ucbo_Starter = sOldValue
End Sub

It is my understanding that, if the user presses a key except tab or enter, e.g. &quot;b&quot;, after the message the original entry should re-appear in the combobox. However, it shows &quot;b&quot;.
If I set a breakpoint in the second module, the original entry appears as wished.
Any experience here what I'm doing wrong?

Thanks for any tip. georgp
 
Hi!

Don't think the keypress event tracks for tab or enter (use either the keydown or keyup event for that), but that's not the problem here. Before you exit the sub, you'll need to cancel the keystroke.

For keypress:
[tt]KeyAscii = 0[/tt]

Using keydown
[tt]KeyCode = 0[/tt]

Roy-Vidar
 
Roy,

competent as always... Thanks a lot. Georg
 
Roy,

I believe the code for tab, enter und esc (undo, if NewRecord) is required. In addition, I have trapped the Null case, and now I think this works quite well (let's see how it works tomorrow!!!):

Private Sub ucbo_Starter_Enter()
'Purpose: Make sure that critical fields are supplied

If ucbo_Starter.Locked = True Then Exit Sub
If Len(Nz(ucbo_Starter)) <> 0 Then
sOldValue = ucbo_Starter
Exit Sub
End If

'Check for, and focus on empty controls with higher priority
Call fEnterControl(&quot;full composition&quot;)

End Sub

Private Sub ucbo_Starter_KeyPress(KeyAscii As Integer)
'Purpose: Do not allow user to enter text

'Otherwise the msgbox pops up even in the locked state
If ucbo_Starter.Locked = True Then Exit Sub
'Allows tab, enter and esc without msgbox
If KeyAscii = 9 Or KeyAscii = 13 Or KeyAscii = 27 Then Exit Sub
MsgBox &quot;You can not change entries in this combo box manually.&quot; & Space(12) & vbCr _
& Space(8) & &quot;Please select an item from the dropdown list.&quot;, vbInformation, gsApplName & sOldValue
KeyAscii = 0
ucbo_Starter.Dropdown

End Sub

But the KeyAscii = 0 was critical. If I would not have gotten this info, I would have probably just dumped the whole thing.
Thanks again, Georg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top