Replace key pressed in listbox cell entry

I am trying to intercept and replace a key typed in a listbox cell but there appears to be no way to do this.

In other words, I’m trying to apply a “mask” to the data entry in a cell. Anyone know how to do this.

Use the CellKeyDown event.

Tim - I am using the CellKeyDown event to determine WHEN to insert the picture clause, but it doesn’t seem possible to replace the key they typed with the picture code - in my case, it’s a hyphen.

If you return TRUE, then nothing gets updated in the cell, if you try to use the following and return FALSE

me.cell(row,column) = me.cell(row,column) + “-”

the key you pressed still gets entered in the cell. Never do see the hyphen

You want to intercept the “key” and replace it:

If (Asc(Key) = 9 Or Asc(Key) = 3 Or Asc(Key) = 13) Then // enter or tab was typed

In that, I’m trapping the Return, Enter, and Tab keys. You just replace the Asc(Key) = value key = “-”

Or am I missing what you’re asking?

Hi there,

This question comes up every so often; as well as intercepting and stopping what you don’t want (e.g. anything except 0-9 for numeric fields) do take care with people copying and pasting values, and also allow things like backspace, delete, left and right arrows and so on.

My personal preference is to validate it in the TextChange event as well as having something in KeyDown.


So here is the solution:

The mask I am trying to apply is as follows: 9999-9999

So they can enter 4 numbers, then an auto insert hyphen, then 4 more numbers. Here is my code in an “else if” that traps the 5th character entered and replaces it with a hyphen and then appends the 5th character onto it.

This code is placed in the CELLKEYDOWN event, of course, there are other “else ifs” to trap for enter keys, arrow keys, backspace, etc.

ElseIf column = 0 and IsHyphen and me.Text.Len = nHyphenLoc - 1 then
  me.ActiveCell.Text = me.ActiveCell.Text + "-" +key
  Return True

How do you mark a thread as answered?

Top right of each post there is a tick mark :slight_smile: