User Form Remembers Last Data Input

Hi All,

I have a custom user form that autopopulates to a document.  I'd like the userform to remember the last data input selected or inputted into its Combo and Text boxes.  Is this possible?

Example: Userform has Text box for contact.  The user types in "Joe Smith" and clicks OK and it's populated into the document.  The next time the template Userform is opened, "Joe Smith" is already displayed in the Userform Textbox.

Currently, the userform is populating the document properly but it would be great if the userform displayed the last data typed the next time it is run.

TIA!


ghinjerseyAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
UserForm1.Hide

You can show it again with

UserForm1.Show vbModal
 or
UserForm1.Show vbModeless
0
 
Patrick MatthewsConnect With a Mentor Commented:
ghinjersey said:
>>Is this possible?

Yes.  I typically do it by using SaveSetting to write the values to the Registry before I shut down the UserForm,
and GetSetting to retrieve the values from the Registry when I load the UF.

The VBA help file has the complete syntax for those statements.
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
If you hide the form instead of unloading it, the data will be retained during the lifetime of the host application session.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ghinjerseyAuthor Commented:
How can I hide it?
0
 
Patrick MatthewsConnect With a Mentor Commented:
How long do you need to "remember" the values?  Graham's method will successfully retain the values
within a session, but then those values evaporate once the userform unloads.  If you need to retain those
values across multiple sessions, I think the Registry is a pretty good way to go.

But it all comes down to what your needs are.
0
 
ghinjerseyAuthor Commented:

I think I just need to hide it because the Word template is being intrgraded with another Program for Mail Merging.

Can I just put this code:

"UserForm1.Hide

You can show it again with

UserForm1.Show vbModal
 or
UserForm1.Show vbModeless"

Into the Form Code or do I need to add it to the Module Code and add buttons.  I'm extremely new at this.


0
 
GrahamSkanConnect With a Mentor RetiredCommented:
You can put the
UserForm1.Hide (or Me.Hide) in the Useform code.

If you want the user to hide it, a Button on the form is a good idea.

Since the form is not active, you will need to reshow the form from some code external to it, probably from some code in a code module. It rather depends on how your application works and when you need it to be seen again.
0
 
ghinjerseyAuthor Commented:
So I put:

Private Sub btnCancel_Click()
    btnOkClicked = False
    Me.Hide
End Sub

Private Sub btnOK_Click()
    btnOkClicked = True
    Me.Hide
End Sub

Private Sub btnMerge_Click()
    btnMergeClicked = True
    Me.Hide
End Sub

Private Sub btnShow_Click()
    btnShowClicked = True
    Me.Hide
End Sub

Into the Form Code and Added the Buttons to the User Form.

Now I'm in the Document Form and my current code is:

If btnOkClicked = True Then
        Bookmarks("bkContact").Contact.Text = frmQuestionaire.txtContact
        Selection.EndKey wdStory
        Options.DisplayGridLines = False
        With ActiveDocument.ActiveWindow.View
            .ShowBookmarks = False
            .ShowAll = False
        End With
        Else
        ActiveDocument.Close wdDoNotSaveChanges
    End If
End Sub

Does anyone know where to btnMerge and btn Show code?
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
You need the variables to be declared as Public and addressed like this:

If UserForm1.btnOkClicked  = True Then
0
 
GrahamSkanConnect With a Mentor RetiredCommented:
If you show the form modally, you could put the Show before variables are needed:
UserForm1.Show vbModal
 
If UserForm1.btnOkClicked = True Then
        Bookmarks("bkContact").Contact.Text = frmQuestionaire.txtContact
        Selection.EndKey wdStory
        Options.DisplayGridLines = False
        With ActiveDocument.ActiveWindow.View
            .ShowBookmarks = False
            .ShowAll = False
        End With
        Else
        ActiveDocument.Close wdDoNotSaveChanges
    End If
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.