Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VBA: Saving data in a userForm

Posted on 2000-02-23
6
1,136 Views
Last Modified: 2009-07-29
I've noticed that you can save data in controls placed directly on a word document: i.e. text placed in a textBox control on a word document is saved along with the document so that it is still there when the document is reopened.  Like regular Visual Basic, data put into a control on a userForm aparently is not saved when the form is unloaded: i.e. the default properties of the control are set when the form is reloaded regardless of what the user put into the control earlier.  Is there any way to save the inputs on a user form without creating a separate data file or putting the data on the word document itself?  If a separate data file is required, is there any way to encapsulate this data with the word document so the user sees just one (word document) when opening the project?
0
Comment
Question by:SteveW
6 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 2551541
Hi SteveW,

This is for a seperate textfile form where you can load and save data for the formproperties...

http://support.microsoft.com/support/kb/articles/Q197/0/21.ASP?LNG=ENG&SA=ALLKB&FR=0

But you mentioned this was not what you were looking for...

Leaves another possibility open, that of the bookmarks. You could put some hidden bookmarks into the document, where you store data for formfields, and retrieve them from.

If you would like a sample, i'll write one...

HTH:O)Bruintje
0
 
LVL 22

Expert Comment

by:ture
ID: 2553694
SteveW,

Just an idea...

If it's only a few values you wish to remember, and if the values should be saved for the current user/machine, you might find it easy enough to use the registry.

The parameters (all strings) are:
Application, Section, Key and Setting (only when setting the value). Here's an example:

'Write to registry
n = "Ture Magnusson",
SaveSetting "TuredataInvoicing", "OrderForm", "CustomerName", n

'Read from registry
n = GetSetting("TuredataInvoicing", "OrderForm", "CustomerName")

Ture Magnusson
Karlstad, Sweden
0
 
LVL 5

Expert Comment

by:vboukhar
ID: 2564314
Other way - to store your information into custom document properties (like data in TextBox1 in my example):

Private Sub UserForm_Initialize()
On Error Resume Next
' if custom property doesn't exist yet
Me.TextBox1.Text = ThisDocument.CustomDocumentProperties("TB1")
On Error GoTo 0
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
On Error Resume Next
ThisDocument.CustomDocumentProperties.Add "TB1", False, msoPropertyTypeString, Me.TextBox1.Text
ThisDocument.CustomDocumentProperties("TB1") = Me.TextBox1.Text
On Error GoTo 0
End Sub

Hope it helps.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:ture
ID: 2615528
SteveW,

Even better than CustomDocumentProperties is to use document variables. They can contain more data (up to ~60 K strings) and they don't show up when right-clicking a document to see it's properties.

Sub UseDocumentVariables()
  'Create new variable if not already present
  On Error Resume Next
  ActiveDocument.Variables.Add Name:="invoicenumber"
  On Error GoTo 0
 
  'Set value of variable
  ActiveDocument.Variables("invoicenumber").Value = 36500
 
  'Display value of variable
  MsgBox ActiveDocument.Variables("invoicenumber").Value
End Sub

/Ture
0
 
LVL 22

Accepted Solution

by:
ture earned 200 total points
ID: 2615545
Upgrading to answer.
0
 

Expert Comment

by:BegnrSteve
ID: 2628491
Maybe, I don't understand the original question corrrectly, but it sounds as if SteveW was asking about templates which is an easy and completely successful way for the user to use the form and input data to be saved as a document for later retrieval and editing or whatever.  Maybe, SteveW will come back to tell me that this is not an option.  

You folks have provided sufficient answers to his question and the solutions are certainly excellent.  Good discussion here.   ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn date into age 17 32
Date Formatting on Userform Print 5 26
SKip past fields with no data 6 11
Cost allcocation ... 10 21
Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question