Visual Basic Combo API
Pure VB: Saving Combo Settings Between Application Runs
     
Posted:   Monday October 01, 2001
Updated:   Monday December 26, 2011
     
Applies to:   VB3, VB4-16, VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

This is a straightforward demo showing how, using only VB code, you can save the current selections made from a set of combos between application sessions. 

The principle is simple - a User Defined Type is declared with a single Integer variable for each combo on the form whose last selection you want to save.  The UDT is written and read to a data file, with each variable in the UDT holding the last-selected value for each combo.

On load, the UDT is read as a Random Access file, and the index values are set in each of the combos on the form. On unload, the current values are saved, again using the UDT and the Random Access file once the current index has been assigned to the UDT variables.

The Random Access file approach is also a viable method to save the actual contents of the combo between sessions, however doing so would be easier if one did not attempt to save both the combo data, and the last selection, in the same file.

 BAS Module Code
None.

 Form Code
Add three combo boxes (Combo1, Combo2, Combo3) and a command button (Command1) to a form, along with the following code:

Option Explicit
         

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Type ComboIndexSelections
    C1 As Integer
    C2 As Integer
    C3 As Integer
   '...etc for as many combos as needed
End Type


Private Sub Form_Load()

   With Combo1
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
      .AddItem "d"
      .AddItem "e"
      .AddItem "f"
      .AddItem "g"
   End With
   
   With Combo2
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
      .AddItem "d"
      .AddItem "e"
      .AddItem "f"
      .AddItem "g"
   End With
   
   With Combo3
      .AddItem "a"
      .AddItem "b"
      .AddItem "c"
      .AddItem "d"
      .AddItem "e"
      .AddItem "f"
      .AddItem "g"
   End With


  'Load existing data. If file does not
  'exist, it is created, and the values
  'will = 0 and the first list item will 
  'be selected)
   Dim hFile As Integer
   Dim CIndex As ComboIndexSelections

   hFile = FreeFile
   Open "comboindex.dat" For Random Access Read As #hFile Len = Len(CIndex)
       Get #hFile, 1, CIndex
   Close #hFile
   
   Combo1.ListIndex = CIndex.C1
   Combo2.ListIndex = CIndex.C2
   Combo3.ListIndex = CIndex.C3
   
End Sub


Private Sub Command1_Click()
   
   Unload Me
   
End Sub


Private Sub Form_Unload(Cancel As Integer)

   Dim hFile As Integer
   Dim CIndex As ComboIndexSelections
   
   CIndex.C1 = Combo1.ListIndex
   CIndex.C2 = Combo2.ListIndex
   CIndex.C3 = Combo3.ListIndex
   
   hFile = FreeFile
   Open "comboindex.dat" For Random Access Write As #hFile Len = Len(CIndex)
       Put #hFile, 1, CIndex
   Close #hFile
   
End Sub
 Comments
 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter