Skip Navigation Links.
Using GetSetting and SaveSetting to restore previous text values
Language(s):Visual Basic 6.0
Category(s):Registry

Automatically save and restore all text box values.

' Using GetSetting and SaveSetting to restore previous text values.
'
' Jon Vote 

' 02/2002

' Automatically save and restore all text box values using GetSetting and SaveSetting.
'
' 1) Create a new project. Form1 will be created by default.
' 2) Add a label to the top of the form.
' 2) Add some text boxes to the form.
' 4) Paste the following code into the declarations section of Form1:

' --- Begin code for Form1 ---

'Saving and Restoring Text Box Settings
'
'Author: Jon Vote 
'        Idioma Software Inc. 
'        www.idioma-software.com 
'
'Date:   02/02
'
'(c) 2002 Idioma Software Inc. 


Option Explicit

Private Sub Form_Load()
  
  Me.Caption = "Save/Restore Text Boxes Example"
  Label1.Caption = "Add some text to the text boxes. Close the form and re-run."
  Label1.AutoSize = True
  Call GetTextBoxes(Me)

End Sub

 
Private Sub Form_Unload(Cancel As Integer)
  Call SaveTextBoxes(Me)
End Sub

' 6) From the menu select Project|Add Module. Module1 will be created by default
' 7) Paste the followin code into Module1:

' --- Begin code for Module1 ---

Option Explicit

Public Sub GetTextBoxes(frmForm As Form)
  
  Dim ctlControl As Control
  
  'Loop through each control on the form
  For Each ctlControl In frmForm.Controls
  
    'Save the value if a TextBox
    If TypeName(ctlControl) = "TextBox" Then
      ctlControl.Text = GetSetting(GetAppName(frmForm), GetSectionName(ctlControl), "Text", ctlControl.Text)
    End If
    
  Next ctlControl
  
End Sub

Public Sub SaveTextBoxes(frmForm As Form)

  Dim ctlControl As Control
  
  'Loop through each control on the form
  For Each ctlControl In frmForm.Controls
  
    'Save the value if a TextBox
    If TypeName(ctlControl) = "TextBox" Then
      SaveSetting GetAppName(frmForm), GetSectionName(ctlControl), "Text", ctlControl.Text
    End If
    
  Next ctlControl

End Sub

'Appends Program Name and Form Name
Private Function GetAppName(frmForm As Form) As String
  GetAppName = App.Path & "\" & App.EXEName & "_" & frmForm.Name
End Function

'Appends Control Name + Index if an array
Private Function GetSectionName(ctlControl As Control) As String
  
  Dim varIndex As Variant
  
  On Error Resume Next
  
  varIndex = ctlControl.Index
  If varIndex = "" Then
    GetSectionName = ctlControl.Name
  Else
    GetSectionName = ctlControl.Name & "(" & varIndex & ")"
  End If
   
End Function

' --- End code for Module1 ---


This article has been viewed 4823 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.