VBA Hide User form but retain data entered into it

MattB

I'm back again with what I hope is a fairly easy question.

I'm attempting to create a user form in VBA. The user will enter certain bits of information into the form, and then close the form. I'd like the user form to retain the data entered after it is closed by the user. I'm treating it as a class module, since techinically they are, or at least that is how I understand it. Here is the code I'm using:

In the main sub that displays the user form:

Sub NonACATMemo()

Dim UserInput As MemoReasons
Set UserInput = New MemoReasons
UserInput.Show

... And then in the user form itself to close it...

Private Sub UserForm_Terminate()
MemoReasons.Hide
End Sub

I also call this sub from a command button on the form. The issue I'm running into is that when I use this method, I get an error "Run-time error '402': Must close or hide topmost modal form first." If I use unload me, when I try to get data out of the form it is cleared and I get a "server not available" error or something to that effect.

So, any ideas on hiding a user form but retaining the data inside?

Final couple of notes: This is the only user form in the project, and here is an example of how I'm trying to get data out of it using the Public Property Get method:

Debug.Print UserInput.EmailFlag
Debug.Print UserInput.ContraFirm
Debug.Print UserInput.MemoReason

Well, I'm all ears if anyone has any suggestions.

David Zemens

I've not seen this approach before. Normally, I would just instantiate the form by:

MemoReasons.Show

Indeed the _Terminate() event is wiping out the data held in the form. So the solution is to not call the _Terminate() event from the button-click. Instead, simply hide the form, e.g.:

Sub ShowMemoReasons()
'In a normal code module, this calls the form
' could be run from the macros menu or attached to
' a shape/button/etc on the worksheet.

MemoReasons.Show

End Sub

Put these in the MemoReasons code module:

Private Sub CommandButton1_Click()  '<-- Rename to handle your button's click event

    MemoReasons.Hide  '## Hides the form but does not release it from memory

End Sub
Private Sub UserForm_Terminate()
'Any events pertaining to the termination of the form object
' otherwise, all form control data will be wiped out when
' this object releases from memory

End Sub

After you do these, if you use the button to HIDE the form, you can call the ShowMemoReasons() and it should re-display the form, while preserving data that was previously entered in the form.

If you use the red "X" button or some other event triggers the Terminate event, you will lose the form data. There are ways to do validation and prevent this with the QueryClose event if necessary.

Update

I don't think you need to Dim an instance of the user form (an exception would be if you will be potentially displaying multiple forms at the same time). Otherwise, declaring UserInput as a public variable is redundant and confusing.

Inicidentally, this is why you're getting the error: Must close or hide topmost modal form first. If you must implement it this way, instead of doing MemoReasons.hide you should use Me.Hide.

As long as you are only displaying one instance of the form, you can always refer to MemoReasons.property because MemoReasons is a public object, just like ThisWorkbook or ActiveWorksheet, etc.

Instead, you should be able to refer to this object (MemoReasons is an object) in any subroutine, for example create another one that is not called from the previous subs. Run the sub to show the form, enter in some data, and then hide the form. With the form hidden, then run this subroutine, and you should see the resulting data from the form.

Sub Test2()
    Debug.Print MemoReasons.EmailFlag
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check if Data was entered by the User or by a macro [VBA]

From Dev

Excel VBA - Convert string entered in user form to number

From Dev

VBA in Microsoft Access 2013 mark checkbox if data is entered in linked form

From Dev

Orbeon Form Builder- Saving User entered data in mySQL

From Dev

VBA - Creating a Data Sheet from a User Form

From Dev

Validating User form Data using excel vba

From Dev

functions, forms, and user entered data

From Dev

Retain Data Recieved from Database on HTML form

From Dev

Hide Formula Results Until Data Is Entered

From Dev

Display the data entered in a form before submitting the form

From Dev

program to read data from a text file and display the output in abbreviated form with all punctuation marks as entered my the user

From Dev

what is a recommended way to redisplay a form with user-entered data after an exception?

From Dev

what is a recommended way to redisplay a form with user-entered data after an exception?

From Dev

VBA - Access - updating table with new data from user form

From Dev

Data entered in the form is not being sent to the Oracle Database

From Dev

Save data entered in a form with CKEditor to MySQL database

From Dev

Data entered in the form is not being sent to the Oracle Database

From Dev

How to determine data type entered by user?

From Dev

String split issue with user entered data

From Dev

Python Tkinter - processing data entered by a user

From Dev

PHP Form with 'Quantity' dropdown to insert that many rows of the data entered into form

From Dev

vba save user form contents

From Dev

Excel VBA hide the form control buttons

From Dev

VBA - Copy a template worksheet and rename with user entered text

From Dev

VBA - Copy a template worksheet and rename with user entered text

From Dev

How to pass a user entered form value from html to javascript?

From Dev

How to pass a user entered form value from html to javascript?

From Dev

Updating the user entered value from a php form to the database

From Dev

how to display the values entered by a user in form in a servlet program on tomcat?

Related Related

  1. 1

    Check if Data was entered by the User or by a macro [VBA]

  2. 2

    Excel VBA - Convert string entered in user form to number

  3. 3

    VBA in Microsoft Access 2013 mark checkbox if data is entered in linked form

  4. 4

    Orbeon Form Builder- Saving User entered data in mySQL

  5. 5

    VBA - Creating a Data Sheet from a User Form

  6. 6

    Validating User form Data using excel vba

  7. 7

    functions, forms, and user entered data

  8. 8

    Retain Data Recieved from Database on HTML form

  9. 9

    Hide Formula Results Until Data Is Entered

  10. 10

    Display the data entered in a form before submitting the form

  11. 11

    program to read data from a text file and display the output in abbreviated form with all punctuation marks as entered my the user

  12. 12

    what is a recommended way to redisplay a form with user-entered data after an exception?

  13. 13

    what is a recommended way to redisplay a form with user-entered data after an exception?

  14. 14

    VBA - Access - updating table with new data from user form

  15. 15

    Data entered in the form is not being sent to the Oracle Database

  16. 16

    Save data entered in a form with CKEditor to MySQL database

  17. 17

    Data entered in the form is not being sent to the Oracle Database

  18. 18

    How to determine data type entered by user?

  19. 19

    String split issue with user entered data

  20. 20

    Python Tkinter - processing data entered by a user

  21. 21

    PHP Form with 'Quantity' dropdown to insert that many rows of the data entered into form

  22. 22

    vba save user form contents

  23. 23

    Excel VBA hide the form control buttons

  24. 24

    VBA - Copy a template worksheet and rename with user entered text

  25. 25

    VBA - Copy a template worksheet and rename with user entered text

  26. 26

    How to pass a user entered form value from html to javascript?

  27. 27

    How to pass a user entered form value from html to javascript?

  28. 28

    Updating the user entered value from a php form to the database

  29. 29

    how to display the values entered by a user in form in a servlet program on tomcat?

HotTag

Archive