14 Replies Latest reply on Aug 15, 2011 2:43 PM by pisupportduke

    How to save information on a userform?

    pisupportduke

      Hello,

       

      First I would like to ask the question, is it possible to save information on a user form? I am not looking to archive the information but I want the information to remain in the text boxes and the option buttons still selected when I reopen a program. Below is my form.5367.q12.gif

       

       

       

      What I am trying to do is when I exit the form I want the fields to remain filled when the form is opened up again I want to be able to review the information I or another person entered. Basically, I want the information to remain on the form until the equipment is returned back to service and the information is no longer needed. Is this something that can be done? Please advise.

       

      -Alex

        • Re: How to save information on a userform?
          Ahmad Fattahi

          You can store the current status of the objects (buttons, text boxes, etc.) in a simple file of your own format. You can update this file on every change event (for more security in case of an abnormal termination) or on the close of your form (more efficient). You can then load your objects with the saved information from last time on start up.

            • Re: How to save information on a userform?
              pisupportduke

              Ahmad,

               

              I am not really sure where to go on this. Right now when I select an equipment status on my form and then exit, the radio button does not stay selected when I reload the form. Do you have an example of how I would code the save button so that the information in my text boxes and radio button will remain entered even if the top corner "X" is clicked or the exit button is clicked.

               

              Thanks,

               

              Alex

                • Re: How to save information on a userform?
                  Ahmad Fattahi

                  Here is a flavor of what you can do. As you see there are events associated with objects such as BeforeClose in this example. These methods are called when that certain event happens. You can include the appropriate code in there so whenever your display starts up you load up the status of the display from file and whenever it closes you save the status to the file.

                   

                  1273.Form-events.jpg

                   

                  You can read more about events in general from any text on VB.

                    • Re: How to save information on a userform?
                      pisupportduke

                      Ahmad,

                       

                      Do you think you can elaborate more? I tried to do more research, but it seems like VB and VBA are more different than I thought. I understand how to achieve the load event procedure, but I do not know what to use for the code. I understand I will need to have to configure a file to save to. This will give me the ability to run the program anywhere and the same file of information will display until changed. To my understanding all of my coding will be with-in the save button correct? It would include the destination of where the file will be saved and the commands for saving the text to each text and combo box manually. Is that correct?

                       

                      Thanks,

                       

                      Alex

                        • Re: How to save information on a userform?
                          Ahmad Fattahi

                          That's right. So, you use these "events" which are triggered automatically when you close and open your form. There are several events associated with any objects as you can see in my previous post. You would include the code to save the data on your form in the BeforeClose event. You would include to load the information from the file in your Open event.

                           

                          To work with the files in VBA please see this page. It explains the file operations in VBA along with several examples you could use.

                            • Re: How to save information on a userform?
                              pisupportduke

                              Ahmad,

                               

                              I understand how to save the file in a txt format, but I am still not understanding how to save information on a form so it will reappear when opened. For example, if I entered 8-21-11 in the expected in service date on my form when I click to reload the form I want to see the 8-21-11 in the expected in service date. So is there a different way to save a form or do I just need to change the format of the file being saved?

                               

                              -Alex

                                • Re: How to save information on a userform?
                                  andreas

                                  Alex, there is no simple way to save a form AFAIK - as Ahmad mentioned you will have to save the status of your controls in your form to a file of your own format on closing, and on opening the form you load that file and preset your controls accordingly.

                                    • Re: How to save information on a userform?
                                      pisupportduke

                                      Andreas, so you are saying the best way is to save the information as a txt file then when the form reloads have the load txt file event piggy backed on the open form event correct?

                                        • Re: How to save information on a userform?
                                          dtakara

                                          An alternative to writing to/read from text files is to write to/read from the NamedValues collection of the ProcessBook display where the UserForm is embedded.

                                           

                                          Here is some sample code that would be included in the display code:

                                           
                                          Sub store_value(Name As String, value As String)
                                              If ThisDisplay.NamedValues(Name) Is Nothing Then
                                                  ThisDisplay.NamedValues.Add Name, value
                                              Else
                                                  ThisDisplay.NamedValues(Name).value = value
                                              End If
                                              
                                          End Sub
                                          
                                          Function get_value(Name As String) As String
                                              If Not ThisDisplay.NamedValues(Name) Is Nothing Then
                                                  get_value = ThisDisplay.NamedValues(Name).value
                                              Else
                                                  get_value = ""
                                              End If
                                          End Function
                                          

                                           

                                           

                                          And here is some sample code that would be used in the UserForm code:

                                           
                                          Private Sub UserForm_Activate()
                                              UserForm1.TextBox1.Text = ThisDisplay.get_value("Texto1")
                                              UserForm1.TextBox2.Text = ThisDisplay.get_value("Texto2")
                                          End Sub
                                          
                                          Private Sub UserForm_Terminate()
                                              ThisDisplay.store_value "Texto1", UserForm1.TextBox1.Text
                                              ThisDisplay.store_value "Texto2", UserForm1.TextBox2.Text
                                          End Sub
                                          

                                           

                                            • Re: How to save information on a userform?
                                              pisupportduke

                                              Daniel,

                                               

                                              The example worked great! I was able to get all of my text boxes to remain filled after the close of the program. I really appreciate all of the help from Vcampus support.

                                               

                                              -Alex

                                                • Re: How to save information on a userform?
                                                  dtakara

                                                  Alex,

                                                   

                                                  I'm glad to know you figured out how to keep the text boxes filled. Thanks for letting us know!

                                                    • Re: How to save information on a userform?
                                                      pisupportduke

                                                      This information is saved to the userform temporarily and the processbook itself needs to be saved also in order to keep the information there upon reopening the program. Here is the code for my save button. Will I use the store_value function or the save as function?

                                                       

                                                      ThisDisplay.store_value ThisDisplay.MySymbol & "Texto1", UserForm1.TextBox1.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Texto2", UserForm1.TextBox2.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Texto3", UserForm1.TextBox3.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Texto4", UserForm1.TextBox4.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Texto5", UserForm1.TextBox5.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Name1", UserForm1.ComboBox1.Text
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Green1", UserForm1.OptionButton1.value
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Yellow1", UserForm1.OptionButton2.value
                                                          ThisDisplay.store_value ThisDisplay.MySymbol & "Red1", UserForm1.OptionButton3.value

                                                       

                                                       -Alex