vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="VB6Parse Library Reference - getsetting - Environment">
    <title>getsetting - Environment - VB6Parse Library Reference</title>
    <link rel="stylesheet" href="../../../assets/css/style.css">
    <link rel="stylesheet" href="../../../assets/css/docs-style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
    <script src="../../../assets/js/theme-switcher.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
    <script>hljs.highlightAll();</script>
</head>
<body>
    <header class="docs-header">
        <div class="container">
            <h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/environment/index.html">Environment</a> / getsetting</h1>
            <p class="tagline">VB6 Library Reference</p>
        </div>
    </header>

    <nav class="docs-nav">
        <div class="container">
            <a href="../../../index.html">Home</a>
            <a href="../../../library/index.html">Library Reference</a>
            <a href="../../../documentation.html">Documentation</a>
            <a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
            <a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
            <button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
                <span class="theme-icon">🌙</span>
            </button>
        </div>
    </nav>

    <main class="container">
        
        <article class="library-item">
            <h1 id="getsetting-function">GetSetting Function</h1>
<p>Returns a registry key setting value from the Windows registry.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">GetSetting(appname, section, key[, default])</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>appname</code> (Required): <code>String</code> expression containing the name of the application or project whose key setting is requested. On Windows, this is a subkey under <code>HKEY_CURRENT_USER\Software\VB and VBA Program Settings</code>.</li>
<li><code>section</code> (Required): <code>String</code> expression containing the name of the section where the key setting is found.</li>
<li><code>key</code> (Required): <code>String</code> expression containing the name of the key setting to return.</li>
<li><code>default</code> (Optional): Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a String containing the value of the specified registry key. If the key doesn't exist and no default is provided, returns an empty string.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>GetSetting</code> function retrieves settings from the Windows registry that were previously saved using the <code>SaveSetting</code> statement. The settings are stored in the application's subkey under:
<code>HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\section</code>
- If the registry key doesn't exist, <code>GetSetting</code> returns the default value (or "" if no default specified)
- <code>GetSetting</code> only works with the <code>HKEY_CURRENT_USER</code> registry hive
- For more advanced registry access, use Windows API functions like <code>RegOpenKeyEx</code> and <code>RegQueryValueEx</code>
- The <code>appname</code>, <code>section</code>, and <code>key</code> parameters are case-insensitive
- <code>GetSetting</code> is designed to work with <code>SaveSetting</code>, <code>DeleteSetting</code>, and <code>GetAllSettings</code>
- On non-Windows platforms, behavior may vary or be unsupported</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Application Configuration</strong>: Retrieve user preferences and application settings</li>
<li><strong>User Preferences</strong>: Load window positions, sizes, and UI state</li>
<li><strong>Recent Files</strong>: Get most recently used files or paths</li>
<li><strong>Database Connections</strong>: Retrieve connection strings and server names</li>
<li><strong>Feature Toggles</strong>: Load feature flags and experimental settings</li>
<li><strong>Localization</strong>: Get language and regional preferences</li>
</ol>
<h2 id="basic-usage-examples">Basic Usage Examples</h2>
<pre><code class="language-vbnet">&#x27; Example 1: Get a simple setting with default
Dim userName As String
userName = GetSetting(&quot;MyApp&quot;, &quot;User&quot;, &quot;Name&quot;, &quot;Guest&quot;)
&#x27; Example 2: Get window position
Dim formLeft As String
formLeft = GetSetting(&quot;MyApp&quot;, &quot;Window&quot;, &quot;Left&quot;, &quot;0&quot;)
&#x27; Example 3: Get setting without default
Dim lastFile As String
lastFile = GetSetting(&quot;MyApp&quot;, &quot;Recent&quot;, &quot;File1&quot;)
&#x27; Example 4: Get database connection
Dim connString As String
connString = GetSetting(&quot;MyApp&quot;, &quot;Database&quot;, &quot;ConnectionString&quot;, &quot;&quot;)</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<pre><code class="language-vbnet">&#x27; Pattern 1: Load form position and size
Private Sub Form_Load()
    Me.Left = CLng(GetSetting(&quot;MyApp&quot;, &quot;MainForm&quot;, &quot;Left&quot;, &quot;0&quot;))
    Me.Top = CLng(GetSetting(&quot;MyApp&quot;, &quot;MainForm&quot;, &quot;Top&quot;, &quot;0&quot;))
    Me.Width = CLng(GetSetting(&quot;MyApp&quot;, &quot;MainForm&quot;, &quot;Width&quot;, &quot;6000&quot;))
    Me.Height = CLng(GetSetting(&quot;MyApp&quot;, &quot;MainForm&quot;, &quot;Height&quot;, &quot;4500&quot;))
End Sub
&#x27; Pattern 2: Check if setting exists
Function SettingExists(app As String, section As String, key As String) As Boolean
    Dim marker As String
    marker = String$(10, &quot;X&quot;)
    SettingExists = (GetSetting(app, section, key, marker) &lt;&gt; marker)
End Function
&#x27; Pattern 3: Get with type conversion
Dim showTips As Boolean
showTips = CBool(GetSetting(&quot;MyApp&quot;, &quot;Options&quot;, &quot;ShowTips&quot;, &quot;True&quot;))
&#x27; Pattern 4: Get recent file list
Dim i As Integer
Dim recentFiles() As String
ReDim recentFiles(1 To 10)
For i = 1 To 10
    recentFiles(i) = GetSetting(&quot;MyApp&quot;, &quot;Recent&quot;, &quot;File&quot; &amp; i, &quot;&quot;)
    If recentFiles(i) = &quot;&quot; Then Exit For
Next i
&#x27; Pattern 5: Get connection info
Dim server As String, database As String
server = GetSetting(&quot;MyApp&quot;, &quot;Database&quot;, &quot;Server&quot;, &quot;localhost&quot;)
database = GetSetting(&quot;MyApp&quot;, &quot;Database&quot;, &quot;Name&quot;, &quot;MyDB&quot;)
&#x27; Pattern 6: Get user preference with validation
Dim fontSize As Integer
fontSize = CInt(GetSetting(&quot;MyApp&quot;, &quot;UI&quot;, &quot;FontSize&quot;, &quot;10&quot;))
If fontSize &lt; 8 Or fontSize &gt; 72 Then fontSize = 10
&#x27; Pattern 7: Get setting in With block
With Form1
    .BackColor = CLng(GetSetting(&quot;MyApp&quot;, &quot;Colors&quot;, &quot;Background&quot;, &quot;16777215&quot;))
End With
&#x27; Pattern 8: Conditional loading
If GetSetting(&quot;MyApp&quot;, &quot;Options&quot;, &quot;AutoSave&quot;, &quot;False&quot;) = &quot;True&quot; Then
    EnableAutoSave
End If
&#x27; Pattern 9: Get multiple related settings
Dim smtp As String, port As String, useTLS As String
smtp = GetSetting(&quot;MyApp&quot;, &quot;Email&quot;, &quot;SMTPServer&quot;, &quot;smtp.gmail.com&quot;)
port = GetSetting(&quot;MyApp&quot;, &quot;Email&quot;, &quot;Port&quot;, &quot;587&quot;)
useTLS = GetSetting(&quot;MyApp&quot;, &quot;Email&quot;, &quot;UseTLS&quot;, &quot;True&quot;)
&#x27; Pattern 10: Safe retrieval with error handling
On Error Resume Next
Dim value As String
value = GetSetting(&quot;MyApp&quot;, &quot;Config&quot;, &quot;Setting&quot;, &quot;DefaultValue&quot;)
If Err.Number &lt;&gt; 0 Then
    value = &quot;DefaultValue&quot;
    Err.Clear
End If
On Error GoTo 0</code></pre>
<h2 id="advanced-usage-examples">Advanced Usage Examples</h2>
<pre><code class="language-vbnet">&#x27; Example 1: Settings manager class
Public Class AppSettings
    Private Const APP_NAME As String = &quot;MyApplication&quot;
    Public Function GetStringSetting(section As String, key As String, _
                                     Optional defaultValue As String = &quot;&quot;) As String
        GetStringSetting = GetSetting(APP_NAME, section, key, defaultValue)
    End Function
    Public Function GetIntegerSetting(section As String, key As String, _
                                      Optional defaultValue As Integer = 0) As Integer
        Dim value As String
        value = GetSetting(APP_NAME, section, key, CStr(defaultValue))
        On Error Resume Next
        GetIntegerSetting = CInt(value)
        If Err.Number &lt;&gt; 0 Then GetIntegerSetting = defaultValue
    End Function
    Public Function GetBooleanSetting(section As String, key As String, _
                                      Optional defaultValue As Boolean = False) As Boolean
        Dim value As String
        value = GetSetting(APP_NAME, section, key, CStr(defaultValue))
        GetBooleanSetting = CBool(value)
    End Function
End Class
&#x27; Example 2: Application configuration loader
Private Sub LoadApplicationConfig()
    Dim config As New Collection
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Paths&quot;, &quot;Data&quot;, App.Path &amp; &quot;\Data&quot;), &quot;DataPath&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Paths&quot;, &quot;Export&quot;, App.Path &amp; &quot;\Export&quot;), &quot;ExportPath&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Paths&quot;, &quot;Temp&quot;, Environ$(&quot;TEMP&quot;)), &quot;TempPath&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Database&quot;, &quot;Server&quot;, &quot;localhost&quot;), &quot;DBServer&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Database&quot;, &quot;Name&quot;, &quot;AppDB&quot;), &quot;DBName&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Options&quot;, &quot;AutoBackup&quot;, &quot;True&quot;), &quot;AutoBackup&quot;
    config.Add GetSetting(&quot;MyApp&quot;, &quot;Options&quot;, &quot;BackupInterval&quot;, &quot;60&quot;), &quot;BackupInterval&quot;
    Set g_AppConfig = config
End Sub
&#x27; Example 3: Multi-user profile system
Public Function LoadUserProfile(userName As String) As UserProfile
    Dim profile As New UserProfile
    Dim section As String
    section = &quot;User_&quot; &amp; userName
    With profile
        .FullName = GetSetting(&quot;MyApp&quot;, section, &quot;FullName&quot;, userName)
        .Email = GetSetting(&quot;MyApp&quot;, section, &quot;Email&quot;, &quot;&quot;)
        .Role = GetSetting(&quot;MyApp&quot;, section, &quot;Role&quot;, &quot;User&quot;)
        .Theme = GetSetting(&quot;MyApp&quot;, section, &quot;Theme&quot;, &quot;Default&quot;)
        .Language = GetSetting(&quot;MyApp&quot;, section, &quot;Language&quot;, &quot;en-US&quot;)
        .LastLogin = GetSetting(&quot;MyApp&quot;, section, &quot;LastLogin&quot;, &quot;&quot;)
    End With
    LoadUserProfile = profile
End Function
&#x27; Example 4: MRU (Most Recently Used) manager
Public Class MRUManager
    Private Const MAX_MRU As Integer = 10
    Private Const APP_NAME As String = &quot;MyApp&quot;
    Private Const SECTION As String = &quot;MRU&quot;
    Public Function GetMRUList() As Collection
        Dim mruList As New Collection
        Dim i As Integer
        Dim item As String
        For i = 1 To MAX_MRU
            item = GetSetting(APP_NAME, SECTION, &quot;Item&quot; &amp; i, &quot;&quot;)
            If Len(item) &gt; 0 Then
                mruList.Add item
            Else
                Exit For
            End If
        Next i
        Set GetMRUList = mruList
    End Function
    Public Sub AddMRUItem(filePath As String)
        Dim mruList As Collection
        Dim i As Integer
        Dim item As String
        Set mruList = GetMRUList()
        &#x27; Remove if already exists
        For i = 1 To mruList.Count
            If StrComp(mruList(i), filePath, vbTextCompare) = 0 Then
                mruList.Remove i
                Exit For
            End If
        Next i
        &#x27; Add to top
        mruList.Add filePath, , 1
        &#x27; Save back
        For i = 1 To mruList.Count
            If i &gt; MAX_MRU Then Exit For
            SaveSetting APP_NAME, SECTION, &quot;Item&quot; &amp; i, mruList(i)
        Next i
    End Sub
End Class</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p><code>GetSetting</code> generally doesn't raise errors, but returns the default value if the setting doesn't exist:
- <strong>No Error</strong>: If the registry key doesn't exist, returns default (or "" if no default)
- <strong>No Error</strong>: If appname, section, or key is empty, returns default value
- <strong>Type Mismatch</strong>: Can occur when converting returned String to another type (e.g., <code>CInt</code>)
- <strong>Registry Access</strong>: On systems where registry access is restricted, may return defaults</p>
<pre><code class="language-vbnet">&#x27; Safe retrieval with type conversion
On Error Resume Next
Dim timeout As Integer
timeout = CInt(GetSetting(&quot;MyApp&quot;, &quot;Network&quot;, &quot;Timeout&quot;, &quot;30&quot;))
If Err.Number &lt;&gt; 0 Then
    timeout = 30
    Err.Clear
End If
On Error GoTo 0</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><strong>Registry Access</strong>: Each call accesses the Windows registry, which is slower than memory access</li>
<li><strong>Caching</strong>: Consider caching frequently used settings in memory</li>
<li><strong>Startup Time</strong>: Loading many settings at startup can slow application initialization</li>
<li><strong>Batch Loading</strong>: Use <code>GetAllSettings</code> to retrieve all settings in a section at once for better performance</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Use Defaults</strong>: Always provide sensible default values</li>
<li><strong>Validate Values</strong>: Validate retrieved settings before using them</li>
<li><strong>Cache Settings</strong>: Load settings once and cache them for the session</li>
<li><strong>Consistent Naming</strong>: Use consistent naming conventions for <code>appname</code>, <code>section</code>, and <code>key</code></li>
<li><strong>Error Handling</strong>: Use error handling when converting string values to other types</li>
<li><strong>Cleanup</strong>: Use <code>DeleteSetting</code> to remove obsolete settings</li>
<li><strong>Documentation</strong>: Document all registry keys used by your application</li>
</ol>
<h2 id="comparison-with-other-registry-functions">Comparison with Other Registry Functions</h2>
<table>
<thead>
<tr>
<th>Function</th>
<th>Purpose</th>
<th>Returns</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>GetSetting</code></td>
<td>Get single registry value</td>
<td><code>String</code></td>
</tr>
<tr>
<td><code>GetAllSettings</code></td>
<td>Get all values in a section</td>
<td><code>Variant</code> array</td>
</tr>
<tr>
<td><code>SaveSetting</code></td>
<td>Save registry value</td>
<td>N/A (statement)</td>
</tr>
<tr>
<td><code>DeleteSetting</code></td>
<td>Delete registry key/section</td>
<td>N/A (statement)</td>
</tr>
</tbody>
</table>
<h2 id="platform-compatibility">Platform Compatibility</h2>
<ul>
<li><strong>Windows</strong>: Full support, uses <code>HKEY_CURRENT_USER</code> registry hive</li>
<li><strong>Other Platforms</strong>: May use alternative storage mechanisms or be unsupported</li>
<li><strong>Registry Location</strong>: <code>HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\section\key</code></li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Only accesses <code>HKEY_CURRENT_USER</code> hive (use Windows API for other hives)</li>
<li>Returns <code>String</code> type only (requires conversion for other types)</li>
<li>No direct way to check if a key exists (use unique default value trick)</li>
<li>Limited to VB's registry structure (use Windows API for custom locations)</li>
<li>No support for <code>REG_BINARY</code> or other complex registry types</li>
<li>Settings are user-specific, not machine-wide</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>GetAllSettings</code>: Returns all key settings and their values from a registry section</li>
<li><code>SaveSetting</code>: Saves or creates an application entry in the Windows registry</li>
<li><code>DeleteSetting</code>: Deletes a section or key setting from the Windows registry</li>
<li><code>Environ</code>: Returns the string associated with an environment variable</li>
<li><code>Command</code>: Returns the argument portion of the command line</li>
<li><code>App.Path</code>: Returns the path where the application executable is located</li>
</ul>
        </article>
        
        <div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
            <p>
                <a href="index.html">← Back to Environment</a> |
                <a href="../index.html">View all functions</a>
            </p>
        </div>

    </main>

    <footer>
        <div class="container">
            <p>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>