×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Resetting Userform Default Text Value Via VBA

Resetting Userform Default Text Value Via VBA

Resetting Userform Default Text Value Via VBA

(OP)
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.



Loren

RE: Resetting Userform Default Text Value Via VBA

Are you talking Text and Value properties of the TextBox on the UserForm?
Because UserForm does not have Text or Value properties.


---- Andy

There is a great need for a sarcasm font.

RE: Resetting Userform Default Text Value Via VBA

(OP)
Yes. I am looking at the textbox properties in a userform.

RE: Resetting Userform Default Text Value Via VBA

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:

Me.txtMyTextBox.Value = "My New Value"


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.

RE: Resetting Userform Default Text Value Via VBA

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

RE: Resetting Userform Default Text Value Via VBA

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.

RE: Resetting Userform Default Text Value Via VBA

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

RE: Resetting Userform Default Text Value Via VBA

>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

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

RE: Resetting Userform Default Text Value Via VBA

(OP)
I think writing to the document properties looks like my path forward. Thank you to everyone for the assistance and code example.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close