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

Insert Text Snippets Into Fields? 1

Status
Not open for further replies.

bowfonz

IS-IT--Management
Jul 13, 2004
52
0
0
US
I have an Access form with many text fields.

Is there a way to insert small pieces of repetitious text into the field at the cursor position?

For example, I'm typing data into the field and I need to add a short string of text then continue entering data and maybe add that same string again. And I need to do this in most of the text boxes on the form so I'd like to have a button on the form or some sort of keystroke that will insert that string at the cursor.

Thanks
-Tom
 
You could use the _KeyPress event of your text boxes and then check the length of the field. If the length matches where you want to insert then you can do so. This example addes in the text upon the fourth key stroke in the field.

Private Sub txtField1_KeyPress(KeyAscii As Integer)
If Len(txtField1.Text) = 3 Then
txtField1.Text = txtField1.Text & "_inserted_text_"
End If
End Sub

Hope this helps.
 
That would be cool, except that the snippets don't go in the same place every time.

It's a web database so the snippets I'm talking about are things like "<br>" and "&nbsp;" and "&trade;".

A button on the form or a keystroke would work best I think. I thought about using one of your lines (txtField1.Text = txtField1.Text & "_inserted_text_") on a buttonclick event except that how does the button know what field I'm in?

Is there some object like current field? Then I could do something like Current_Field.Text = Current_Field.Text & "<br>" in the buttonclick event.

Do the fields have focus when my cursor is in it? maybe that could be used somehow?

Thanks
-Tom
 
So you wish to be entering text in text box and then click a button to insert text in the box you were just in...but have that text inserted be varied?

Are there many text fields where you wish to have this happen? or is it just one field?

Here is why...If your cursor is in the text box then it has the "focus". As soon as you move your mouse to click a button your text box loses the focus and the button becomes the ActiveControl. The ActiveControl property will return the current control. This will not work with a command button approach. Plus you'll have to click off some button when you want to insert some text and how do you plan to identify what string gets inserted when you click the button?

I have an idea...(just an idea)...

What about using a hot key combo to insert the text when you are physically typing in the field?? This code will execute if the SHFT+CTRL+ALT+F10 keys are pressed. I think you could use a modified form of this (for ease of entry) to insert text 'as needed'. You can set up a combo of SHFT+CTRL+B to insert '<br>', SHFT+CTRL+N to insert &nbsp;" etc...

'---
Private Sub txtText1_KeyDown(KeyCode As Integer, Shift As Integer)
Dim intShiftDown As Integer, intAltDown As Integer
Dim intCtrlDown As Integer, intDown As Integer
Dim sInsertText As String

'use SHFT+CTRL as hot keys only!
intShiftDown = (Shift And acShiftMask) > 0
intCtrlDown = (Shift And acCtrlMask) > 0
'intAltDown = (Shift And acAltMask) > 0

'assign string based on third hot key
Select Case KeyCode
Case vbKeyN
sInsertText = "<br>"
Case vbKeyB
sInsertText = "&nbsp;"
End Select


If intShiftDown And intCtrlDown And sInsertText <> "" Then
'insert the text and move the cursor ahead
txtText1.Text = txtText1.Text & sInsertText
txtText1.SelStart = Len(txtText1.Text)
End If


End Sub
 
That works!
It's good because it uses keystrokes which is quick.

The only problem I see is that I have to put this code on every field on the form. There are 10 fields that will use this code. So if I need to add a snippet, I need to add it in 10 places. I wish there was a way to have the code in one place and work in all 10 fields.

Nah, nevermind. Beggars can't be choosers. This works just fine, it's exactly what I was trying to do. Thanks jtseltmann!

-Tom
 
I hear you there. YOu could adjust the sInsertText to a function so that you only need to maintain the "text inserts" in one place.

You could also change the specific field references in the last section to:
If intShiftDown And intCtrlDown And sInsertText <> "" Then
Me.ActiveControl.Text = Me.ActiveControl.Text & sInsertText
Me.ActiveControl.SelStart = Len(Me.ActiveControl.Text)
End If

Then you can just cut and past the code to each control with no modifications...and the function controls the actual string assigned (one place to update)

Just a thought.
 
Uh oh, just when I thought all was right with the world.
I just realized that this won't work the way it is.

The text string needs to insert at the cursor.
This code is appending to the end of the field.
Don't know why I didn't see that before.

The way I enter into these fields is typically to cut and paste large chunks of text and then break it up with spaces and line breaks. So the <br> and the &nbsp; have to go in where the cursor is.

Otherwise I believe this is the way to go. The Me.ActiveControl thing works and I can turn the string variable into a function.

I really do appreciate your help on this, jtseltmann.

-Tom
 
Tom,
Give this a shot. It should work as you wanted...
'---
Dim intShiftDown As Integer, intAltDown As Integer
Dim intCtrlDown As Integer, intDown As Integer
Dim sInsertText As String, pos As Integer

'use SHFT+CTRL as hot keys
intShiftDown = (Shift And acShiftMask) > 0
intCtrlDown = (Shift And acCtrlMask) > 0
'intAltDown = (Shift And acAltMask) > 0

'assign string based on third hot key
Select Case KeyCode
Case vbKeyN
sInsertText = "<br>"
Case vbKeyB
sInsertText = "&nbsp;"
End Select

If intShiftDown And intCtrlDown And sInsertText <> "" Then
pos = Me.ActiveControl.SelStart
Me.ActiveControl.Text = Left(Me.ActiveControl.Text, pos) & sInsertText & Mid(Me.ActiveControl.Text, pos + 1, Len(Me.ActiveControl.Text) - pos)
Me.ActiveControl.SelStart = Len(Me.ActiveControl.Text)

End If
 
Yes! This is it.
I did make a minor change at the end to leave the cursor at the end of the inserted text. Works good in case I want a double break like <br><br>.

If intShiftDown And intCtrlDown And sInsertText <> "" Then
pos = Me.ActiveControl.SelStart
Me.ActiveControl.Text = Left(Me.ActiveControl.Text, pos) & sInsertText & Mid(Me.ActiveControl.Text, pos + 1, Len(Me.ActiveControl.Text) - pos)
Me.ActiveControl.SelStart = Me.ActiveControl.SelStart + pos + Len(sInsertText)
End If

This will work just great. This is exactly what I needed.
Thanks very much jtseltmann for all your help!

-Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top