Class: XMLSettings in Category XML Files : XML Files from Total Visual SourceBook

Save and read application settings to and from an XML File with VB6 and VBA.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CXMLSettings class.
SourceFile Property Get the path and name of the XML source file.
Class_Initialize Initialize Initialize the class.
Class_Terminate Terminate Clean up opened objects when the class is terminated.
GetNodes Method Return array of nodes for the specified section.
GetNodeValue Private Create an array of child node values for the specified node.
GetSetting Method Get a setting entry from the document.
LoadXML Method Load an XML document.
SaveXML Method Save an XML document.
WriteSetting Method Write a setting entry to the document.
AddRootNode Private Create a default Root Document node.
FileExists Method Determine if a file name exists.
mxmlDocument_OnDataAvailable Private Event raised by the XML parser when data is available.
mxmlDocument_OnReadyStateChange Private Event raised by the XML parser when the Ready State changes.
' To use this example:
' 1.  Create a new form.
' 2.  Create the following command buttons: cmdSaveSettings, cmdGetSettings
' 3.  Paste the entire contents of this module into the new form's module.

' This example assumes that the sample files are located in the folder named by the following constant
Private Const mcstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"

Private Sub cmdGetSettings_Click()
  ' Comments: Retrieve values from the XML file

  Dim objSettings As CXMLSettings

  Set objSettings = New CXMLSettings

  objSettings.SourceFile = mcstrSamplePath & "Settings.xml"
  objSettings.LoadXML

  ' Read the values from the XML file
  Debug.Print objSettings.GetSetting("Section1", "Entry1")
  Debug.Print objSettings.GetSetting("Section1", "Entry2")
  Debug.Print objSettings.GetSetting("Section1", "Entry3")
  Debug.Print objSettings.GetSetting("Section2", "Entry1")
  Debug.Print objSettings.GetSetting("Section2", "Entry2")
  Debug.Print objSettings.GetSetting("Section2", "Entry3")
  Debug.Print "Created", objSettings.GetSetting("Section3", "Created")
  Debug.Print "Last Read", objSettings.GetSetting("Section3", "LastRead")

  ' Update the XML file to save when it was last read
  objSettings.WriteSetting "Section3", "LastRead", Now()
  objSettings.SaveXML

  Set objSettings = Nothing
End Sub

Private Sub cmdSaveSettings_Click()
  ' Comments: Save values to the XML file

  Dim objSettings As CXMLSettings

  ' Create the class object
  Set objSettings = New CXMLSettings

  ' Specify the name of the XML file to create
  objSettings.SourceFile = mcstrSamplePath & "Settings.xml"
  ' Add an item to the XML file
  objSettings.WriteSetting "Section1", "Entry1", "Section1Entry1Value"
  ' Save the file to disk
  objSettings.SaveXML

  ' Add other items to the XML file
  objSettings.WriteSetting "Section1", "Entry2", "Section1Entry2Value"
  objSettings.SaveXML
  objSettings.WriteSetting "Section1", "Entry3", "Section1Entry3Value"
  objSettings.SaveXML
  objSettings.WriteSetting "Section2", "Entry1", "Section2Entry1Value"
  objSettings.SaveXML
  objSettings.WriteSetting "Section2", "Entry2", "Section2Entry2Value"
  objSettings.SaveXML
  objSettings.WriteSetting "Section2", "Entry3", "Section2Entry3Value"
  objSettings.SaveXML
  objSettings.WriteSetting "Section3", "Created", Now()
  objSettings.SaveXML
  objSettings.WriteSetting "Section3", "LastRead", ""
  objSettings.SaveXML

  ' Close the object
  Set objSettings = Nothing
End Sub

Private Sub Form_Load()
  Me.cmdGetSettings.Caption = "Get Settings"
  Me.cmdSaveSettings.Caption = "Save Settings"
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS