Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resetting Userform Default Text Value Via VBA

Status
Not open for further replies.

LorenUCT

Programmer
Aug 6, 2019
3
US
Hello,

I am trying to write a sequence generating word program that uses a userform to select the unit configuration and set points. Once the user has completed their work and generated the sequence, I need to reset the default text values in the userform properties so that when the file is closed and re-opened the unit configuration form is exactly as they left it. I know how to manually set the default text values in the properties of the userform but I need to do it via VBA for this project.

TextToChange_rsy3xa.png


Loren
 
Are you talking [tt]Text[/tt] and [tt]Value[/tt] properties of the TextBox on the UserForm?
Because UserForm does not have [tt]Text[/tt] or [tt]Value[/tt] properties.


---- Andy

There is a great need for a sarcasm font.
 
Yes. I am looking at the textbox properties in a userform.
 
You are referring to the Value property of the TextBox in Design mode. You can change its Value in Run mode in VBA code by:
[tt]
Me.txtMyTextBox.Value = "My New Value"[/tt]

but I don't think you can save it because "My New Value" is just in the memory of your computer.
Unless you write this Value into a database, or just a simple text file, and retrieve it when your UserForm is open.


---- Andy

There is a great need for a sarcasm font.
 
You may consider storing settings in a different way, either as custom document properties if they have to be linked to document, or in registry (secure vba section, functions GetSetting, SaveSetting, DeleteSetting, GetAllSettings) for user values.

combo
 
That also depends if your app will be accessed from the same computer or from different computers. If from many computers, the registry way may not be sufficient (am I right, combo?)


---- Andy

There is a great need for a sarcasm font.
 
Registry settings are valid for single user on single computer.
Returning to initial OP post, as Andy pointed, default properties of userform objects can be set in design mode and stay stored in VBA project.

combo
 
>I don't think you can save it

Au contraire …

For example, assuming we are working in Excel, and the form we are talking about is called "UserForm2" and has a textbox on it

Code:
[blue][COLOR=green]' Assumes we have a userform called UserForm2 with a TextBox control on it[/color]
Public Sub Example()
    Unload UserForm2 [COLOR=green]' make sure form is unloaded[/color]
    
    Application.VBE.ActiveVBProject.VBComponents("UserForm2").Designer.Controls("TextBox1").Value = "whatever you like"
    
    Application.DisplayAlerts = False
    
    ActiveWorkbook.Save [COLOR=green]' assumes workbook has been saved at some point previously. If not uncomment and use next line instead
    'ActiveWorkbook.SaveAs "fullfilename.xlsm", xlOpenXMLWorkbookMacroEnabled [/color]
    
    Application.DisplayAlerts = True
End Sub[/blue]

 
I think writing to the document properties looks like my path forward. Thank you to everyone for the assistance and code example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top