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!

Code to insert text before the text is inserted in a textbox 1

Status
Not open for further replies.

chukesgirl

Technical User
Mar 24, 2011
3
US
Hello Everyone!

I am new to Word VBA and I need help with some code. I am using Word 2007 on WinXP, if that helps. I have a template with a userform. For one of the textboxes, I want to insert some text before the value of the textbox is entered. Right now the way I have it set up, if the user types a number in the textbox, it will appear as the sample below. I tried putting the text directly into the textbox, but it displays even when there is no number entered which I don't want because there isn't always going to be a number so that field should be blank.

Sample:
John Doe
111111

My textboxes are coded like this:
.Bookmarks ("name").Range.Text=txtname
.Bookmarks ("clientno").Range.Text=txtclientno


I want to have the words Client number appear before the 111111.

John Doe
Client number 1111111

Is there a simple way to do this?
 
Code:
.Bookmarks ("clientno").Range.Text="Client number " & txtclientno

the & simply joins 2 pieces of text together.

hth

Ben

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
Adding the & solved my initial problem and I am glad it was someting very simple. However, now, I need some help with not having the words "Client number" NOT display in the document if no number is entered in the textbox.
 
So to put it another way:
if the client number has not been entered (ie txtclientno="") then set your text to "" else set your text to what we had before end if.

Hope that helps.

B.

Code:
if txtclientno="" then
.Bookmarks ("clientno").Range.Text=""
else
.Bookmarks ("clientno").Range.Text="Client number " & txtclientno
end if

----------------------------------------------
Ben O'Hara
David W. Fenton said:
We could be confused in exactly the same way, but confusion might be like Nulls, and not comparable.
 
.Bookmarks ("clientno").Range.Text = IIf(Trim(txtclientno & "") = "", "", "Client number " & txtclientno)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top