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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Combobox value automatically based on keypress or keydown event?

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
I am able to do this while I step through the code using either KeyPress or KeyDown events on the combo-box. However, if I just run it while looking a the form, it does not run correctly, it just enteres a "1" as the text value instead of as the ID (column 1)....

Here's the code:
Code:
Private Sub cboAcctFrom_KeyDown(KeyCode As Integer, Shift As Integer)
    If cboAcctFrom.Text = vbNullString Or cboAcctFrom.Text = "1" Then
        DoEvents
        If KeyCode = 49 Then 'number 1
            cboAcctFrom = 2
            cboAcctFrom.Undo
        End If
    End If
End Sub

Private Sub cboAcctFrom_KeyPress(KeyAscii As Integer)
    If cboAcctFrom.Text = vbNullString Or cboAcctFrom.Text = "1" Then
        DoEvents
        If KeyAscii = 49 Then 'number 1
            cboAcctFrom = 2
        End If
    End If
End Sub

I've had both of these work, separately of course, when stepping through, but not when I just let it run.

Thanks for any suggestions/info,





"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
What, in plain language, are you trying to accomplish here?



The Missinglinq

Richmond, Virginia

The Devil's in the Details!
 
Basically, I'm trying to use a keyboard shortcut (although it's really just one key) to select a value for the combo box.

So when I hit [TAB] to go to the combo box, I have to type out at least 6 characters to select the value. So I thought I could try to fix it to where I could type just a 1, and have the computer recognize that I'm using the most commonly used "account".

I know it probably seems silly and overkill, but I want to try to do it. I've not had time to look at it since about the time I posted this question, though. Maybe I'll get time this weekend.

I suppose the reason why it won't currently work the way I want is because as soon as I'm entering keystrokes, it's sending the values, and doesn't look at the value until the next event fires, which would be leaving the control? Well, I assumed it was 1 event per keypress, but this has me thinking that maybe it's a little different.. must think about it some more, test with other ideas...

If I ever get it sorted out, I'll post back with the solution. [smile]


"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
The main issue is that these events are just handlers for your specific code, and after your code is invoked the default handler for the control is run. So you need to eat the keypress if you take action on it, else the default handler will also try and use it ...

e.g. KeyCode=0 or KeyASCII=0
 
I have not been able to look at this since January - oh my, can't believe it's been that long. I'll try to get to it again this weekend, perhaps, and report back. Sorry for the delay.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top