Creating a Database

P

PRegis08

Hi. Can someone point me in the right direction? I have a userform that
collects data but I am not familiar with how to store it and retrieve it
later. I don't need to have it saved anywhere, just accessible while the
userform is open and active.

I looked into RANGE and BOOKMARKS but am unsure on how to use them properly.

Can someone help, please?
 
D

Doug Robbins - Word MVP

How about storing the data in variables in the document

ActiveDocument.Variables("varname").Value = somepieceofdata

then to retrieve

thatpieceofdatais = ActiveDocument.Variables("varname").Value

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
P

PRegis08

This works great! Thanks Doug!

If I were to assign record numbers to that data? So if ..

recordNum = 1

Variable(recordNum) = userform1.textbox1.text

And so on? Would that work?
 
G

Gordon Bentley-Mix

Document variable names are Strings, so it would be best practice to convert
the value of "recordNum" to a String using the CStr function first -
especially if you've declared it as an Integer or Long. However, Word is
fairly accommodating and will force the conversion to a String automatically;
it's just neater and (possibly) safer to do an explicit conversion.

BTW, if the value of "recordNum" is incremented sequentially somehow
(perhaps in an array?), you could save yourself a lot of coding by using a
loop structure. For example:

Private Sub SaveRecordNums()
Dim i As Integer
Dim myVariableName As String
For i = LBound(myArray) To UBound(myArray)
myVariableName = "RecordNum" & i
ActiveDocument.Variables.Add myVariableName, CStr(myArray(i))
Next i
End Sub

In this example, the code simply loops through an array (called "myArray")
starting with the first item in the array and ending with the last and
creates the document variables on the fly using a pseudo-constant "RecordNum"
and appending the value of the loop's counter to it. Just be aware that if
the document variable already exists, trying to create it again may throw an
error. In addition, note the use of the CStr function to convert the value
from the array to a String. This is because document variable values are also
stored as Strings, and the same caveat applies as with the name.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top