Enter text on clicking commandbutton

james_IT

Registered User.
Local time
Today, 17:23
Joined
Jul 5, 2006
Messages
208
Hi all

Can someone point me in the right direction for code to go on the onclick event of a command button to insert some text (e.g. "yourtextgoeshere") into a text box (e.g. txtbox) on a form (e.g. frmtest).

And btw, I want to be able to add to the text that is already in the text box, not replace it because I already tried On click, TxtBox = "yourtextgoeshere"

thanks
 
Last edited:
James,

Forms!frmTest!txtBox = Forms!frmTest!txtBox & vbCrLf & "Your text goes here."

That will preserve the existing text, add a carriage-return/line-feed, then
add the static text.

Wayne
 
Two points. If you're writing code in a form and referencing a control on the same form, instead of using the syntax

Forms!frmTest!txtBox

it's much simpler to use the Me contruct, i.e.

Me.txtBox

It's easier to write and easier to read.

Also, you should check to see if there's any data already in the textbox before adding your text to it.
Code:
If Not IsNull(Me.txtBox) Then
  Me.txtBox = Me.txtBox & vbCrLf & "Your text goes here."
Else
  Me.txtBox = "Your text goes here."
End If
If there's already data in it, you add to it, inserting a line feed first with vbCrLf. But if the box is empty, you simply add the text without the line feed, otherwise you'd end of with a blank line in the textbox then your text.
 
Just remove the vbCrLf part (It would just be):

Me.txtBox = Me.txtBox & "Your text goes here."
 
Just remove the vbCrLf part (It would just be):

Me.txtBox = Me.txtBox & "Your text goes here."


What about BACKSPACE? I understand that this is not simply VbBackspace as this enters the character "8". How do I delete the last character in a textbox?
 
What about BACKSPACE? I understand that this is not simply VbBackspace as this enters the character "8". How do I delete the last character in a textbox?

Me.TextBoxNameHere = Left(Me.TextBoxNameHere,Len(Me.TextBoxNameHere)-1)
 
Probably one of those sneaky people who live in a state where the rainfall averages 10 inches a month! :D
 
Probably one of those sneaky people who live in a state where the rainfall averages 10 inches a month! :D

YE... ENGLAND (same weather).

I actually posted and instantly realized the answer so edited the post, unfortunately you were to quick for me and answered before it disappeared!
 

Users who are viewing this thread

Back
Top Bottom