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

String manipulation using VBA.

Status
Not open for further replies.

rann

Programmer
Sep 13, 2001
86
US
Hi,
I have a TextBox and a command button on a form.
I have some text in the textbox. I need to update this
text when i click on the command button.

For Example:
if my Textbox had(before): Access is a database.

if i put my cursor in between "Access" and "is" in the above
text and click the command button the text needs to be updates as given below.

my textbox should display(after):Access aaaa is a database.

how is this possible.

I hope i am clear.

Thanks in Advance.
ran
 
I'm not going to write the function for you, but... here's the basics.

Use the SelStart property of your control to determine where the insertion point is. Use the Left and Mid (or Right) functions to split the string (based on the location of the insertion point), then concatenate the whole thing back together with your added text in the middle.

The SelStart property is the key to determining where the cursor is. "The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
 
There is a big gotcha here. If you put your cursor in a text box the text box has the focus but when you click a command button you no longer can derive information about the insertion point because the cursor is no longer in textbox. You may try another type event and set a module level variable when the textbox gets the focus. But there are problems with that too. If you trap the insertion point on an event properly, let's say in mintInsertPoint then it would be fairly simple to write the sub to make the changes using string parsing techniques.

Make it a function first and text the output to ensure you've got the correct insert parsing algorithm.

Private Sub ChangeText()

Dim strString As String
strString = Mid$(Me.txtYourText, 1, mintInsertPoint - 1)
strString = strString & YourInsertVariable
strString = strString & Mid$(Me.txtYourText, mintInsertPoint)
Me.txtYourName = strString

End Sub

Steve King Growth follows a healthy professional curiosity
 
Hi,
Thanks to both of you for your help.
I got it working.

Private Sub cmdTest_Click()
Dim strString As String
strString = mid$(Me.txtYourText, 1, sStart - 1)
strString = strString & &quot;<p>&quot;
strString = strString & mid$(Me.txtYourText, sStart)
Me.txtYourText = strString
End Sub
Private Sub txtYourText_Click()
sStart = Me.txtYourText.SelStart
End Sub
I declared sStart as public.

Thanks again.
ran.
 
Does Sub txtYourText_Click recognize keyboard arrow keys? Just a thought, to cover that base.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top