Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

saving forms at runtime

Posted on 2000-03-02
Last Modified: 2010-05-02
i want to save the form details (properties of all controls) at runtime. i.e after the user changes some properties of the controls.
how can i do this.
Question by:Sankar030999
  • 4
  • 3
LVL 14

Expert Comment

ID: 2579160
Wow.. that can be a lot of properties.. Ok here goes.. <smile>.

The following code iterates through the Forms and Controls collections saving all the form/control dimensions out to an INI File.. Yes, it is a bit of a mess, but at this hour at night, I really don't feel like a rewrite.. please forgive my inexactitude.

An important VB function to look at is  TypeName(objcontrol).. which returns a character string letting you know the Control type. If this was a really complete procedure, then a select case would be done on the TypeName(objControl) and a full list of ALL its type properties would be delineated and written to disk. Additionally, please note that I append the Form/Control Index to the Group Name so as to handle arrays.

<---- Code ---->

' The cIniBomx.KeyPtrData property writes the INI statement out to disk.

Dim objControl As Control
Dim strControlIndex As String
Dim strControlName As String
Dim objform As Form
Dim strFormName As String
Dim strGroupName As String
Dim intIndex As Integer
For Each objform In Forms
    strFormName = objform.Name
    cIniBomX.GroupName = strFormName
    cIniBomX.KeyName = "Caption"
    cIniBomX.KeyPtrData = objform.Caption
    cIniBomX.KeyName = "Height"
    cIniBomX.KeyPtrData = objform.Height
    cIniBomX.KeyName = "Left"
    cIniBomX.KeyPtrData = objform.Left
    cIniBomX.KeyName = "Top"
    cIniBomX.KeyPtrData = objform.Top
    cIniBomX.KeyName = "Width"
    cIniBomX.KeyPtrData = objform.Width
    cIniBomX.KeyName = "ScaleHeight"
    cIniBomX.KeyPtrData = objform.ScaleHeight
    cIniBomX.KeyName = "ScaleLeft"
    cIniBomX.KeyPtrData = objform.ScaleLeft
    cIniBomX.KeyName = "ScaleTop"
    cIniBomX.KeyPtrData = objform.ScaleTop
    cIniBomX.KeyName = "ScaleWidth"
    cIniBomX.KeyPtrData = objform.ScaleWidth
    cIniBomX.KeyName = "ControlCount"
    cIniBomX.KeyPtrData = objform.Controls.Count
    strFormName = objform.Name
    For Each objControl In objform.Controls
        strControlName = objControl.Name
        If strControlName <> "" _
            strControlName = "/" & strControlName
        End If
        strControlIndex = ""
        strControlIndex = Format(objControl.Index, "00")
        If strControlIndex <> "" _
            strControlIndex = "." & strControlIndex
        End If
        strGroupName _
            = strFormName _
            & strControlName _
            & strControlIndex
        cIniBomX.GroupName = strGroupName
        cIniBomX.KeyName = "TypeName"
        cIniBomX.KeyPtrData = TypeName(objControl)
        cIniBomX.KeyName = "Height"
        cIniBomX.KeyPtrData = objControl.Height
        cIniBomX.KeyName = "Left"
        cIniBomX.KeyPtrData = objControl.Left
        cIniBomX.KeyName = "Top"
        cIniBomX.KeyPtrData = objControl.Top
        cIniBomX.KeyName = "Width"
        cIniBomX.KeyPtrData = objControl.Width
    Next objControl
Next objform


Author Comment

ID: 2583232
thanks for the effort. but i dont think that really solves my problem. i want to update the properties the all the controls in only one form (and not all the forms as u interpreted).
and when i open the form again, i need the controls to use the updated values

for eg, assume that i change the font of a text box. so next time i open the form, the text box should use the updated font and not the original one. like this i want to use some properties in the controls and not all of them.

but i understand that writing the details onto a temporary file and retrieving them is the right way. so please tell me how to do it. also what is "cIniBomx". u have used in the code and how are u using this to write onto the ini file. u havent explained that.
LVL 14

Expert Comment

ID: 2583277
cINIBomx.. I have all the INI file read/lookup/write code in a seperate class module of that name. The purpose of the snippet was to demonstrate how to iterate through the forms and control collection.. to get the data.
I'm happy to hear that you do NOT want to save everything.. just selected items.. <smile>.

Now, in saving the items.. you really have two choices.. the first being reading/writing each of the values from/to a disk file (such as an INI).. or second, storing the entries into the registry and using them from there.

At this moment, I'm going to leave reading / writing to disk a file up to you.. its a basic we all have to do.. and one if you don't already know how to do you, you should gut out and learn.

That brings us to number two.. using the registry. Please read the following three MSDN treatises.. and you should be well on your way to becoming an expert.. <smile>.

1. Creating or Saving Application Settings

You can use the SaveSetting statement to save a new value for a registry key stored in your application's registry location. For example, you could add code to the Form_Unload event in the application's main form in order to preserve settings at shutdown, or in the Form_Unload event on an Options dialog box to update user preferences.

Use the following syntax for the SaveSetting statement:

SaveSetting appname, section, key, value

The following code saves new values for the Backup and LastEntry keys in the Startup section of the registry for an application named "RegCust." This code assumes that the variables strDate and intLastEntry contain the new values.

Private Sub Form_Unload(Cancel As Integer)
   SaveSetting "RegCust", "Startup", "Backup", strDate
   SaveSetting "RegCust", "Startup", "LastEntry", _
End Sub

If an entry for the application "RegCust" or any of these sections or keys don't exist in the Software/Microsoft section in the registry, this code will create it.

2. Retrieving Application Settings..

You can use the GetSetting and GetAllSettings functions to retrieve registry values stored in your application's registry location. For example, your application can retrieve registry settings to recreate its condition at the time it was closed.

One Setting at a Time
To retrieve a single registry setting, use the following syntax for the GetSetting function:

GetSetting(appname, section, key[, default])

The following code retrieves the value of the LastEntry key in the "RegCust" application's Startup section, and displays the value in the Immediate window.

Private Sub Form_Load()
   Dim intLastEntry As Integer
   intLastEntry = GetSetting("RegCust", "Startup", _
   "LastEntry", "0")
   Debug.Print intLastEntry
End Sub

Note that you can use the optional parameter, default, to set the value returned by Visual Basic when there is no value listed in the registry for the specified key.

Multiple Settings
To retrieve a list of registry keys and their values, use the following syntax for the GetAllSettings function:

GetAllSettings(appname, section)

The following code retrieves a two-column list of registry keys and their values in the "RegCust" application's Startup section, and displays the results in the Immediate window.

Private Sub Form_Load()
   Dim avntSettings As Variant
   Dim intX As Integer
   avntSettings = GetAllSettings("RegCust", "Startup")
   For intX = 0 To UBound(avntSettings, 1)
      Debug.Print avntSettings(intX, 0), _
      avntSettings(intX, 1)
   Next intX
End Sub

3. Deleting Application Settings

You can use the DeleteSetting statement to delete a registry key, section, or an application's registry location. For example, you may want to delete all registry information for an application when the application is uninstalled.

Use the following syntax for the DeleteSetting statement:

DeleteSetting(appname, section, key)

The following code deletes the LastEntry key in the "RegCust" application's Startup section.

Private Sub cmdDelKey_Click()
   DeleteSetting "RegCust", "StartUp", "LastEntry"
End Sub

The following code deletes the "RegCust" application's entire Startup section of the registry.

Private Sub cmdDelSection_Click()
   DeleteSetting "RegCust", "StartUp"
End Sub

The following code deletes the entire registry location for the "RegCust" application.

Private Sub cmdUnInstall_Click()
   DeleteSetting "RegCust"
End Sub

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 2584853
i just saw your comment. but i guess the registry handling is tooo much for my application. i think i'll stick on to the ini stuff.
infact i followed your earlier idea of storing the details onto a file in the disk.
i declared a user defined datatype containing 3 data items. one to store the name of the control and other 2 to store 2 properties of the control respectively. the code is given below.

Private Type myrecord
    cname As Control
    cleft As Integer
    ctop As Integer
End Type
Dim myvar As myrecord

Open App.Path + "\datafile" For Output As #1 Len = Len(myvar)
For Each objControl In Form1.Controls
    Write #1, objControl.Name; objControl.Left; objControl.Top
Next objControl
Close #1

while retriving the information, i open the file in input mode and do it as follows.

Input #1, a,b,c

a contains the name of a control. but since it is a string, i'm unable to use it for setting the other properties like
i need to do it as follows


this is what i need to achieve. how can i do it. i tried redirecting the string data to a var of type control. but it dosent work.
kindly help.
LVL 14

Accepted Solution

wsh2 earned 200 total points
ID: 2585135
In reestablishing your properties, you have to reverse the procedure that you saved them with.. <sigh>.

<----- Save Routine ----->
xSaveSettings (Form1)
xSaveSettings (Form1, Form1.Combo1)
xSaveSettings (Form1, Form1.Listview1)
xSaveSettings (Form1, Form1.Treeview1)

<----- xSaveSettings Function ----->
Private Sub xSaveSettings _
(Byval objForm as Form,
Optional Byval objControl as Variant)

If IsMissing(objControl) _
   With objForm
     Write #1, .Name, "", .Left, .Top, .Width, .Height
   End With
   With objControl  
     Write #1, objForm.Name, .Name, .Left, .Top, .Width, .Height
   End With
End If

<----- Restore Snippet ----->
Dim strForm As String
Dim strControl As String
Dim sngLeft As Single
Dim sngTop As Single
Dim sngWidth As Single
Dim sngHeight As Single
Dim objForm As Form
Dim objControl As Control

On Error Goto TagError

Do Until EOF(#1)
   Input #1, strForm, strControl, sngLeft, sngTop, sngWidth, sngHeight

   Set objForm = Nothing
   Select Case strForm
      Case "Form1"
         Set objForm = Form1
      Case "Form2"
         Set objForm = Form2
      Case "Form3"
         Set objForm = Form3
      Case Else
         Msgbox ("Form Error")
   End Select

   If NOT objForm Is Nothing _
      If strControl = "" _
         objForm.Move (sngLeft, sngTop, sngWidth, sngHeight)
         Select Case strControl
         Case "Combo1"
            Set objControl = objForm.Combo1
         Case "Listview1"
            Set objControl = objForm.Listview1
         Case "Listview2"
            Set objControl = objForm.Listview2
         Case Else
            Msgbox ("Control Error")
         End Select
         objForm.ObjControl (sngLeft, sngTop, sngWidth, sngHeight)
      End If
   End If
Exit Sub
   Msgbox ("An Error occurred processing this line")
   Resume Next
End Sub

LVL 14

Expert Comment

ID: 2585148
Oooops.. Please change the Restore snippet line to read


objForm.ObjControl (sngLeft, sngTop, sngWidth, sngHeight)


objForm.ObjControl.Move (sngLeft, sngTop, sngWidth, sngHeight)


Author Comment

ID: 2590064
thanks very much wsh.
this code worked very fine. i just had to make some modifications.
i know i troubled u a lot. anyway thanks alot.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 61
VBA/SQL - Connect to SQL server and pull data 4 118
passing a value with stream reader AFTER a ";" 3 74
VB6 ListBox Question 4 48
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question