Today, I thought I’d simply share a few bits of code for things developers often need to do with Text boxes, and a few that I had fun creating just for the sake of seeing how it could be done!
Things like:
- Positioning the Cursor
- Selecting Text
- Inserting Text
- Retrieving Content
- And More…
Fundamental Concept
I wanted to clarify one aspect of working with Text Boxes before diving into the code.
Whenever we wish to know where/what a user has selected within a Text Box, the position of the cursor within the text in a Text Box, …, we need to capture that information before they move away from that control.
Once they move to another control, that information is lost, setting the focus back will not return it back to the previous state. This is why you will see in certain examples below I utilize the On Lost Focus event to capture such information, save it to a TempVar (could just as easily be a global variable or some other storage medium), and then call/use that information in other events (command button click for instance).
Retrieve the Content of a Text Box
Private Sub cmd_GetAll_Click()
Me.txt_GetAll.SetFocus
Debug.Print Me.txt_GetAll.Text
End Sub
Retrieve the Selected Text from a Text Box
This one is a little trickier and we have to employ the Lost Focus event to identify what is selected.
Private Sub txt_InsertText_LostFocus()
TempVars!txt_InsertTextCurrentPosition = Me.txt_InsertText.SelStart 'Push to a TempVar, could be a Global Var if wanted
End Sub
Then use any other event to retrieve this information to extract the word
Private Sub cmd_GetSelected_Click()
Debug.Print TempVars!SelectedText 'Retrieve the TempVar value
End Sub
Move the Cursor to the Beginning of the Text Box
Private Sub cmd_GotoStart_Click()
Me.txt_GotoStart.SetFocus
Me.txt_GotoStart.SelStart = 0
End Sub
Move the Cursor to the End of the Text Box
Private Sub cmd_GotoEnd_Click()
Me.txt_GotoEnd.SetFocus
Me.txt_GotoEnd.SelStart = Len(Me.txt_GotoEnd.Text)
End Sub
Selecting Text (Words) in a Text Box
Selecting All the Text (Words) in a Text Box
Private Sub cmd_SelectAll_Click()
If Len(Me.txt_SelectAll & vbNullString) = 0 Then Exit Sub
Me.txt_SelectAll.SetFocus 'In reality the next 2 lines are not even required typically.
Me.txt_SelectAll.SelStart = 0
Me.txt_SelectAll.SelLength = Len(Me.txt_SelectAll)
End Sub
Selecting the 1st Text (Word) in a Text Box
Private Sub cmd_SelectFirstWord_Click()
Dim lPosition As Long
If Len(Me.txt_SelectAll & vbNullString) = 0 Then Exit Sub
Me.txt_SelectAll.SetFocus
Me.txt_SelectAll.SelStart = 0
lPosition = InStr(Me.txt_SelectAll, " ")
If lPosition > 0 Then
Me.txt_SelectAll.SelLength = lPosition - 1
Else
Me.txt_SelectAll.SelLength = Len(Me.txt_SelectAll)
End If
End Sub
Selecting the Last Text (Word) in a Text Box
Private Sub cmd_SelectLastWord_Click()
Dim lPosition As Long
If Len(Me.txt_SelectAll & vbNullString) = 0 Then Exit Sub
Me.txt_SelectAll.SetFocus
lPosition = InStrRev(Me.txt_SelectAll, " ")
If lPosition > 0 Then
Me.txt_SelectAll.SelStart = lPosition
Else
Me.txt_SelectAll.SelStart = 0
End If
Me.txt_SelectAll.SelLength = Len(Me.txt_SelectAll) - lPosition
End Sub
Selecting the Next Text (Word) in a Text Box
Once again, this scenario requires the use of the Lost Focus event to identify the cursor’s current position since it cannot be ascertained once you exit the Text Box control.
Private Sub txt_SelectAll_LostFocus()
TempVars!CurrentPosition = Me.txt_SelectAll.SelStart
End Sub
Private Sub cmd_SelectNextWord_Click()
Dim lCurPosition As Long
Dim lPositionStart As Long
Dim lPositionEnd As Long
Dim sText As String
lCurPosition = TempVars!CurrentPosition
Me.txt_SelectAll.SetFocus
sText = Right(Me.txt_SelectAll.Text, Len(Me.txt_SelectAll.Text) - lCurPosition)
lPositionStart = InStr(sText, " ")
Me.txt_SelectAll.SelStart = lCurPosition + lPositionStart
sText = Right(Me.txt_SelectAll.Text, Len(Me.txt_SelectAll.Text) - (lCurPosition + lPositionStart))
lPositionEnd = InStr(sText, " ")
If lPositionEnd = 0 Then
Me.txt_SelectAll.SelLength = Len(sText)
Else
Me.txt_SelectAll.SelLength = lPositionEnd - 1
End If
End Sub
Selecting the Previous Text (Word) in a Text Box
Once again, this scenario requires the use of the Lost Focus event to identify the cursor’s current position since it cannot be ascertained once you exit the Text Box control.
Private Sub txt_SelectAll_LostFocus()
TempVars!CurrentPosition = Me.txt_SelectAll.SelStart
End Sub
Private Sub cmd_SelectPreviousWord_Click()
Dim lCurPosition As Long
Dim lPositionStart As Long
Dim lPositionEnd As Long
Dim sText As String
lCurPosition = TempVars!CurrentPosition
Me.txt_SelectAll.SetFocus
sText = Left(Me.txt_SelectAll.Text, lCurPosition)
lPositionStart = InStrRev(sText, " ") 'Beginning of the current word, we have to go back 1 more time
If lPositionStart > 0 Then
sText = Left(Me.txt_SelectAll.Text, lPositionStart - 1)
lPositionStart = InStrRev(sText, " ")
End If
Me.txt_SelectAll.SelStart = lPositionStart
sText = Right(Me.txt_SelectAll.Text, Len(Me.txt_SelectAll.Text) - lPositionStart)
lPositionEnd = InStr(sText, " ")
If lPositionEnd = 0 Then
Me.txt_SelectAll.SelLength = Len(sText)
Else
Me.txt_SelectAll.SelLength = lPositionEnd - 1
End If
End Sub
Inserting Text into a Text Box
Insert Text at the Beginning of the Text Box
Private Sub cmd_InsertAtBeginning_Click()
Const sTextToInsert = "Some New Text"
Me.txt_InsertText.SetFocus
Me.txt_InsertText = sTextToInsert & Me.txt_InsertText.Text
End Sub
Insert Text at the End of the Text Box
Private Sub cmd_InsertAtEnd_Click()
Const sTextToInsert = "Some New Text"
Me.txt_InsertText.SetFocus
Me.txt_InsertText = Me.txt_InsertText.Text & sTextToInsert
End Sub
Insert Text at the Curosr’s Position Within the Text Box
Once again, this scenario requires the use of the Lost Focus event to identify the cursor’s current position since it cannot be ascertained once you exit the Text Box control.
Private Sub txt_InsertText_LostFocus()
TempVars!txt_InsertTextCurrentPosition = Me.txt_InsertText.SelStart
End Sub
Private Sub cmd_InsertAtCursor_Click()
Dim lCurPosition As Long
Dim sString As String
Dim sPrefix As String
Dim sSuffix As String
Const sTextToInsert = "Some New Text"
lCurPosition = TempVars!txt_InsertTextCurrentPosition
Me.txt_InsertText.SetFocus
sString = Me.txt_InsertText.Text
sPrefix = Left(sString, lCurPosition)
sSuffix = Right(sString, Len(sString) - Len(sPrefix))
Me.txt_InsertText = sPrefix & sTextToInsert & sSuffix
End Sub
Demo Database
Feel free to download a 100% unlocked copy of a sample database (accdb) I have put together by using the link provided below:
Download “Playing Around With Microsoft Access Text Boxes” Text-Boxes.zip – Downloaded 3976 times – 33.00 KB
Notice About Content/Downloads/Demos
Disclaimer/Notes:
If you do not have Microsoft Access, simply download and install the freely available runtime version (this permits running MS Access databases, but not modifying their design):Microsoft Access 2010 Runtime
Microsoft Access 2013 Runtime
Microsoft Access 2016 Runtime
Microsoft 365 Access Runtime
In no event will Devhut.net or CARDA Consultants Inc. be liable to the client/end-user or any third party for any damages, including any lost profits, lost savings or other incidental, consequential or special damages arising out of the operation of or inability to operate the software which CARDA Consultants Inc. has provided, even if CARDA Consultants Inc. has been advised of the possibility of such damages.