How can I use VBA to insert a char at the current text insertion point?

D

David Anderson

Imagine you are designing a form where the user might have to enter the name
of someone from Slovenia called Tomaz Crnej where you wish, out of courtesy,
to spell his name correctly. Both the lower case 'z' and the upper case 'C'
should therefore have a breve accent above the character.

You can enter a z with a breve by typing Alt-0158 in the Tahoma font (for
example). However, no such keyboard shortcut exists for a capital C with a
breve. For this reason, I wish to design a much simplified version of
Windows Character Map within my Access 2003 application.

However, I don't want to insert special characters into text fields or combo
boxes via copy and paste (apart from anything else, pasting into a combo box
does not trigger AutoExpand). I want to simply select the required character
in my CharMap popup box and have it automatically 'typed' into the currently
active text control on my main form - at the current position of the text
cursor.

The ActiveControl property of the main form will tell me which control is
active but I don't yet know how to simulate typing in a character to that
field. At present, all I can think of is to first force a save of any half
typed name in the Surname field, for example, before concatenating whatever
special character is selected in my CharMap popup. However, I don't know if
this will trigger AutoExpand if Surname happens to be a combo box and it
won't handle the situation where the text cursor is in the middle of a name.

Any other ideas while I continue to experiment?

David
 
A

Allen Browne

SelStart will tell you where the cursor is in the control.
SelLength will tell you how many characters are selected.

Make sure you use the Text property of the control: it's Value may not be up
to date yet.

Since these values are integers, this won't work with memos that have > 32k
characters.
 
D

David Anderson

Hi Allen,
I've never come across SelStart and SelLength before but they look like just
the things I need in this case. I was also totally unaware of the Text
property, which is also ideal for this requirement. Thanks for rapidly
pointing me in the right direction and saving me lots of time. It's much
appreciated!

David
 
M

Marshall Barton

David said:
Imagine you are designing a form where the user might have to enter the name
of someone from Slovenia called Tomaz Crnej where you wish, out of courtesy,
to spell his name correctly. Both the lower case 'z' and the upper case 'C'
should therefore have a breve accent above the character.

You can enter a z with a breve by typing Alt-0158 in the Tahoma font (for
example). However, no such keyboard shortcut exists for a capital C with a
breve. For this reason, I wish to design a much simplified version of
Windows Character Map within my Access 2003 application.

However, I don't want to insert special characters into text fields or combo
boxes via copy and paste (apart from anything else, pasting into a combo box
does not trigger AutoExpand). I want to simply select the required character
in my CharMap popup box and have it automatically 'typed' into the currently
active text control on my main form - at the current position of the text
cursor.

The ActiveControl property of the main form will tell me which control is
active but I don't yet know how to simulate typing in a character to that
field. At present, all I can think of is to first force a save of any half
typed name in the Surname field, for example, before concatenating whatever
special character is selected in my CharMap popup. However, I don't know if
this will trigger AutoExpand if Surname happens to be a combo box and it
won't handle the situation where the text cursor is in the middle of a name.


I think this kind of thing is best done using the combo
box's SelStart, SelLength and/or SelText properties. For
example if the user selects one or more characters to
replace with a character from your popup, then the SelText
property can be set to the selected character and the
character will replace the selected text. OTOH, the
SelStart and SelLength properties can be used by the Mid
function to extract the same string that's returned by the
SelText property.

If the user just puts the cursor somewhere in the combo
box's text, then SelLength will be 0 and SelStart provides
the position of the cursor.

The combo box's Text property provides the string that the
user is typing but has not yet completed (different from the
Value property) so this is the property that you need to
operate on to insert/replace characters.

A very important point to using these properties is that the
combo box needs to keep the focus throughout the entire
activitiy of the dialog form and character operation. This
pretty much requires you to use a menu, toolbar or popup
menu item to initiate the character selection dialog form.
If that gets too convoluted, then you need to save the combo
box's Text, SelStart and SelLength property values before
losing the focus and then give the focus back to the combo
box and restore the properties before inserting/replacing a
character.
 
D

David Anderson

Marsh,
Allen Browne got there before you, but you are both suggesting the same
solution - so it must be a good one! I was aware that focus was a key issue,
but was hoping that my plan to use a popup form for special character
selection would avoid any problems, a view you appear be be supporting.

What are the chances that inserting a special character in this way will
trigger the AutoExpand property of a combo box? I'm not very optimistic...

David
 
M

Marshall Barton

I don't know of a way to invoke AutoExpand. I suspect that
it requires keyboard actions.
 

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