Solved

saving forms at runtime

Posted on 2000-03-02
7
277 Views
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.
thanks
0
Comment
Question by:Sankar030999
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:wsh2
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 <> "" _
        Then
            strControlName = "/" & strControlName
        End If
        strControlIndex = ""
        strControlIndex = Format(objControl.Index, "00")
        If strControlIndex <> "" _
        Then
            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


0
 

Author Comment

by:Sankar030999
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.
thanks
0
 
LVL 14

Expert Comment

by:wsh2
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", _
   intLastEntry
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







0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Sankar030999
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

a.propertyname=b
a.propertyname=c

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.
thanks
0
 
LVL 14

Accepted Solution

by:
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) _
Then
   With objForm
     Write #1, .Name, "", .Left, .Top, .Width, .Height
   End With
Else
   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 _
   Then
      If strControl = "" _
      Then
         objForm.Move (sngLeft, sngTop, sngWidth, sngHeight)
      Else
         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
Loop
..
..
..
Exit Sub
:TagError
   Msgbox ("An Error occurred processing this line")
   Resume Next
End Sub

0
 
LVL 14

Expert Comment

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

From...

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

To..

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

     
0
 

Author Comment

by:Sankar030999
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now