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

Storing values entered in a textbox for later use

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi again!

I feel like im the only one posting new threads.

So far ive managed to get help with everything ive asked for, and thats really good.

Okay to be clear and precise!

Im using a userform in word to collect data, about the user.
I know word has some features but ive explored them and they arent enough.

So my requirment.

- Store data entered in a textbox, so that next time the userform is opened it will still be typed in the textbox.
 
Have a look at the Variables collection of the Word.Document object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

That depends on what you mean by "next time"

1. User goes from your UserForm1 to UserForm2, and then goes back to UserForm1 – which is the next time.
2. User is in UserForm1, exits the application, and next week goes back to it “next time”

Two different scenarios: 1 you would use a variable in your application (check the Scope of variables), 2 you may want to use a database, entry in the registry, some kind of file (text file?) to store your information.


Have fun.

---- Andy
 
Type in: "document variables" (no quotes) into Help, in the VBE. NOT Help in Word, Help when you have the VBE open.

Click the Variable Object item. There are examples there that will give you a good start to do what you want.

Basically, you want to put the value of the textbox into a variable when you close the userform. You also want to GET the variable value when you open the userform (put this into the _Initialize event of the userform), and put that value into the textbox.

Gerry
 
That depends on what you mean by "next time"

1. User goes from your UserForm1 to UserForm2, and then goes back to UserForm1 – which is the next time.
2. User is in UserForm1, exits the application, and next week goes back to it "next time"

Next time = Solution 2. Will be closing the form, and opening later.

Two different scenarios: 1 you would use a variable in your application (check the Scope of variables), 2 you may want to use a database, entry in the registry, some kind of file (text file?) to store your information.

I ahve figured out how to use an excel file to store my values in the first three cells, but it takes like 10 seconds before that is done and the macro starts up.

Is saving in a .txt file faster?

Thanks for the answers.

Adding code tomorrow =)
 
Is saving in a .txt file faster?"

Probably yes.

However, going by the original question, using a document variable would in fact be best of all, as no other file needs to be opened. The value is stored in the document.

From Document Variable Help:
Represents a variable stored as part of a document. Document variables are used to preserve macro settings in between macro sessions.
Does that not sound like what you want?
Code:
Sub Userform_Initialize()
On Error Resume Next
  If ActiveDocument.Variables("Name").Value <> "" Then
      Textbox1.Text = ActiveDocument.Variables("Name").Value
  End If
End Sub
If the variable has a value (text), then the code puts that text into the textbox. The on error is for when you first start - the variable does not exist. Unless of course you write some other procedure to do so.
Code:
Sub WhateverClosesTheUserform()
Dim num As Long
[COLOR=red]' for error trapping, good to check if
' variable exists...[/color red]
For Each aVar In ActiveDocument.Variables
    If aVar.Name = "Name" Then num = aVar.Index
Next aVar
If num = 0 Then  [COLOR=red]' it does NOT exist
    ' so add it and give it the textbox value[/color red]
    ActiveDocument.Variables.Add Name:="Name", _
         Value:= Textbox1.Text
Else  [COLOR=red]' does exist, so give it the textbox value[/color red]
    ActiveDocument.Variables(num).Value = Textbox1.Text
End If
End Sub

Gerry
 
Hmm im using three textboxes to store my variables.

I cant really get it to work.
It only says that the variable already exits..

This is what is does when the userform initializes

Sub readdocumentvariables()

On Error Resume Next
If ActiveDocument.Variables("handlaggarnamn").Value <> "" Then
TextBox6.Text = ActiveDocument.Variables("handlaggarnamn").Value
End If
On Error Resume Next
If ActiveDocument.Variables("adress").Value <> "" Then
TextBox7.Text = ActiveDocument.Variables("adress").Value
End If
On Error Resume Next
If ActiveDocument.Variables("telefon").Value <> "" Then
TextBox9.Text = ActiveDocument.Variables("telefon").Value
End If

End Sub

--What happens when i close it--

Sub onuserfromclose()

Dim num As Long
' for error trapping, good to check if
' variable exists...
For Each avar In ActiveDocument.Variables
If avar.Name = "handlaggarnamn" Then num = avar.Index
If avar.Name = "adress" Then num = avar.Index
If avar.Name = "telefon" Then num = avar.Index

Next avar

If num = 0 Then ' it does NOT exist
' so add it and give it the textbox value
ActiveDocument.Variables.Add Name:="handlaggarnamn", Value:=TextBox6.Text
ActiveDocument.Variables.Add Name:="adress", Value:=TextBox7.Text
ActiveDocument.Variables.Add Name:="telefon", Value:=TextBox9.Text

Else ' does exist, so give it the textbox value
ActiveDocument.Variables(num).Value = TextBox6.Text
ActiveDocument.Variables(num).Value = TextBox7.Text
ActiveDocument.Variables(num).Value = TextBox9.Text

End If


End Sub

Hmm I can understand that
 
Brute force method:
Code:
Sub readdocumentvariables()
On Error Resume Next
TextBox6.Text = ActiveDocument.Variables("handlaggarnamn").Value & ""
TextBox7.Text = ActiveDocument.Variables("adress").Value & ""
TextBox9.Text = ActiveDocument.Variables("telefon").Value & ""
End Sub

Sub onuserfromclose()
On Error Resume Next
ActiveDocument.Variables.Add Name:="handlaggarnamn", Value:="6"
ActiveDocument.Variables.Add Name:="adress", Value:="7"
ActiveDocument.Variables.Add Name:="telefon", Value:="9"
ActiveDocument.Variables("handlaggarnamn").Value=TextBox6.Text
ActiveDocument.Variables("adress").Value=TextBox7.Text
ActiveDocument.Variables("telefon").Value=TextBox9.Text
End Sub

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

Part and Inventory Search

Sponsor

Back
Top