Send characters to combobox

S

swas

Hi,

I have a non-updateable datasheet subform. I am wanting to catch the subform
keypress and direct these to a combobox on the parent as if the keypress was
to the combobox directly.

I don't want to use sendkeys as all comments caution its use, and the
combobox may not have the focus anyway (I could ensure it does).

Using the following in the subform:

Private Sub Form_KeyPress(KeyAscii As Integer)

If (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123)
Then
'Alpha key so send to parent combo

Me.Parent.cboFiller = Nz(Me.Parent.cboFiller) & Chr(KeyAscii)

End If

'Prevent subform receiving keypress
KeyAscii = 0

End Sub

The ascii keys are received by the combo, but not displayed nor trigger
combobox events (Before / after update etc...). I understand these events may
not fire if the changes are from code, but not sure on the best way to
achieve the result I want, ideally including auto expand, enter etc...

Thoughts appreciated.


swas
 
D

Damon Heron

I think your keypress event will have to be in a control in the subform.
For instance, if you had a notes field,
Private Sub Notes_KeyPress(KeyAscii As Integer)
If (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123)
Then
'Alpha key so send to parent combo
Me.Parent.cboFiller.SetFocus 'needs to receive focus
Me.Parent.cboFiller.Text = Nz(Me.Parent.cboFiller) & Chr(KeyAscii)

End If
'Prevent subform receiving keypress
KeyAscii = 0
End Sub

Next question, though, is why-o-why do you want to put keystrokes in a
combobox???

Damon
 
S

swas

Damon,

Thanks for the reply.

The subform datasheet is dynamically built on a crosstab and not editable.
The parent form allows edits, then updates the subform data, hence I am
attempting to create the appearance of directly editing the datasheet. If I
do it the other way around, ie. keep focus on parent, then I need to manage
using arrow keys / tabs to move around the subform by the user. Catch 22.

I use conditional formatting to highlight the selected datasheet cell if
focus moves to the parent (So you know which cell is being worked on), but if
I programatically shift datasheet focus around from the parent in code, or
manual mouse click, the new cell gets highlighted, then the original cell,
then settles on the new cell again - which looks yuck. My thought was to keep
the focus in the datasheet...

I will play with echo off perhaps, or other more elegant suggestions? It's
one of those situations that a little more thought may reveal a simpler
approach.

Thanks

swas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top