Re: How to insert a '*' in certain controls when Enter is pressed.



Here's a more generic version, that allows you to insert whatever characters you want at the cursor in the active control:
http://allenbrowne.com/func-InsertChar.html

You would call this one like this:
Call InsertAtCursor(vbCrLf & "*")

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Allen Browne" <AllenBrowne@xxxxxxxxxxxxxx> wrote in message
news:O3SmRX2TIHA.1208@xxxxxxxxxxxxxxxxxxxxxxx
Paste the function below into a module.

Then call it like this from sample text box named Text0:

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
Call StarEnter(Me.Text0, KeyCode, Shift)
End Sub

Function StarEnter(ctl As Control, KeyCode As Integer, Shift As Integer)
'Purpose: Replace Enter keystroke with CrLf and asterisk.
Dim strPrior As String 'Text before the cursor.
Dim strAfter As String 'Text after the cursor.
Dim lngLen As Long 'Number of characters
Dim iSelStart As Integer 'Where cursor is.

'Ignore keystrokes other than Enter; ignore if Shift, Alt or Ctrl pressed.
If (KeyCode = 13) And (Shift = 0) Then
With ctl
lngLen = Len(.Text)
'SelStart can't cope with more than 32k characters.
If lngLen <= 32764 Then
'Remember characters before cursor.
iSelStart = .SelStart
If iSelStart > 1 Then
strPrior = Left$(.Text, iSelStart)
End If
'Remember characters after selection.
If iSelStart + .SelLength < lngLen Then
strAfter = Mid$(.Text, iSelStart + .SelLength + 1)
End If
'Destroy the keystroke.
KeyCode = 0
'Assign prior characters, star, enter, and later characters.
.Text = strPrior & vbCrLf & "*" & strAfter
'Put the cursor back where it as, after the CR, LF, and *.
.SelStart = iSelStart + 3
End If
End With
End If
End Function

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"ThomasAJ" <ThomasAJ@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1A9AE702-4FAB-4C8A-8BDC-CE7BFE66FC06@xxxxxxxxxxxxxxxx
Thanks for that.

Any chance of more help.

I am using very clunky code that is not working.
eg the new line is being created at the start of the text, also when the
enter key is pressed at the end of some text the code falls over.

My code is:

MiscDetails.Text = Mid$(MiscDetails, 1, MiscDetails.SelStart) & "*" &
Mid$(MiscDetails, MiscDetails.SelStart + 1, Len(MiscDetails) -
MiscDetails.SelStart + 1)

Apart from the fact it is not working I bet there is a more specific
function I can use as well.

--
Regards
Tom


"Allen Browne" wrote:

Use SelStart to determine where the cursor is.

"ThomasAJ" <ThomasAJ@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:934DF817-8DEF-4451-B546-99396E3052B4@xxxxxxxxxxxxxxxx
> There could be many other NEW LINES either before or after the > location of
> where the NEW new line is called for. In other words the '*' must be
> inserted
> AFTER where the NEW new line is created.
>
> The end result is to look like:
> * blah blah1
> * blah blah2
> * blah blah3
> * blah blah4
>
> -- > Regards
> Tom
>
>
> "Minton M" wrote:
>
>> On Jan 4, 7:41 pm, ThomasAJ <Thoma...@xxxxxxxxxxxxxxxxxxxxxxxxx>
>> wrote:
>> > An important user wants to have a '*' inserted at the beginning of >> > a
>> > NEW LINE
>> > in certain controls when the ENTER key is pressed.
>> >
>> > OK so in the Form_KeyDown event I can determine if the enter key >> > has
>> > been
>> > pressed, and I also know the name of the ActiveControl. The >> > controls in
>> > question have the NewLine property set to YES.
>> >
>> > So how to insert a '*' at the beginning of the new line. I have >> > tried
>> > some
>> > silly code in the Form_KeyDown event but I think it is the wrong >> > place
>> > to go
>> > changing control text.
>> >
>> > Also bear in mind that the ENTER key may have been pressed in the
>> > middle of
>> > existing text and not just at the end.
>> >
>> > --
>> > Regards
>> > Tom
>>
>> In the control's keypress event, use Replace to search for the >> newline
>> character and replace with a * followed by a new line. Don't forget >> to
>> use the Text property of the Textbox rather than the value property.


.