<!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">' Example 1: Get a simple setting with default
Dim userName As String
userName = GetSetting("MyApp", "User", "Name", "Guest")
' Example 2: Get window position
Dim formLeft As String
formLeft = GetSetting("MyApp", "Window", "Left", "0")
' Example 3: Get setting without default
Dim lastFile As String
lastFile = GetSetting("MyApp", "Recent", "File1")
' Example 4: Get database connection
Dim connString As String
connString = GetSetting("MyApp", "Database", "ConnectionString", "")</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<pre><code class="language-vbnet">' Pattern 1: Load form position and size
Private Sub Form_Load()
Me.Left = CLng(GetSetting("MyApp", "MainForm", "Left", "0"))
Me.Top = CLng(GetSetting("MyApp", "MainForm", "Top", "0"))
Me.Width = CLng(GetSetting("MyApp", "MainForm", "Width", "6000"))
Me.Height = CLng(GetSetting("MyApp", "MainForm", "Height", "4500"))
End Sub
' 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, "X")
SettingExists = (GetSetting(app, section, key, marker) <> marker)
End Function
' Pattern 3: Get with type conversion
Dim showTips As Boolean
showTips = CBool(GetSetting("MyApp", "Options", "ShowTips", "True"))
' 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("MyApp", "Recent", "File" & i, "")
If recentFiles(i) = "" Then Exit For
Next i
' Pattern 5: Get connection info
Dim server As String, database As String
server = GetSetting("MyApp", "Database", "Server", "localhost")
database = GetSetting("MyApp", "Database", "Name", "MyDB")
' Pattern 6: Get user preference with validation
Dim fontSize As Integer
fontSize = CInt(GetSetting("MyApp", "UI", "FontSize", "10"))
If fontSize < 8 Or fontSize > 72 Then fontSize = 10
' Pattern 7: Get setting in With block
With Form1
.BackColor = CLng(GetSetting("MyApp", "Colors", "Background", "16777215"))
End With
' Pattern 8: Conditional loading
If GetSetting("MyApp", "Options", "AutoSave", "False") = "True" Then
EnableAutoSave
End If
' Pattern 9: Get multiple related settings
Dim smtp As String, port As String, useTLS As String
smtp = GetSetting("MyApp", "Email", "SMTPServer", "smtp.gmail.com")
port = GetSetting("MyApp", "Email", "Port", "587")
useTLS = GetSetting("MyApp", "Email", "UseTLS", "True")
' Pattern 10: Safe retrieval with error handling
On Error Resume Next
Dim value As String
value = GetSetting("MyApp", "Config", "Setting", "DefaultValue")
If Err.Number <> 0 Then
value = "DefaultValue"
Err.Clear
End If
On Error GoTo 0</code></pre>
<h2 id="advanced-usage-examples">Advanced Usage Examples</h2>
<pre><code class="language-vbnet">' Example 1: Settings manager class
Public Class AppSettings
Private Const APP_NAME As String = "MyApplication"
Public Function GetStringSetting(section As String, key As String, _
Optional defaultValue As String = "") 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 <> 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
' Example 2: Application configuration loader
Private Sub LoadApplicationConfig()
Dim config As New Collection
config.Add GetSetting("MyApp", "Paths", "Data", App.Path & "\Data"), "DataPath"
config.Add GetSetting("MyApp", "Paths", "Export", App.Path & "\Export"), "ExportPath"
config.Add GetSetting("MyApp", "Paths", "Temp", Environ$("TEMP")), "TempPath"
config.Add GetSetting("MyApp", "Database", "Server", "localhost"), "DBServer"
config.Add GetSetting("MyApp", "Database", "Name", "AppDB"), "DBName"
config.Add GetSetting("MyApp", "Options", "AutoBackup", "True"), "AutoBackup"
config.Add GetSetting("MyApp", "Options", "BackupInterval", "60"), "BackupInterval"
Set g_AppConfig = config
End Sub
' Example 3: Multi-user profile system
Public Function LoadUserProfile(userName As String) As UserProfile
Dim profile As New UserProfile
Dim section As String
section = "User_" & userName
With profile
.FullName = GetSetting("MyApp", section, "FullName", userName)
.Email = GetSetting("MyApp", section, "Email", "")
.Role = GetSetting("MyApp", section, "Role", "User")
.Theme = GetSetting("MyApp", section, "Theme", "Default")
.Language = GetSetting("MyApp", section, "Language", "en-US")
.LastLogin = GetSetting("MyApp", section, "LastLogin", "")
End With
LoadUserProfile = profile
End Function
' Example 4: MRU (Most Recently Used) manager
Public Class MRUManager
Private Const MAX_MRU As Integer = 10
Private Const APP_NAME As String = "MyApp"
Private Const SECTION As String = "MRU"
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, "Item" & i, "")
If Len(item) > 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()
' 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
' Add to top
mruList.Add filePath, , 1
' Save back
For i = 1 To mruList.Count
If i > MAX_MRU Then Exit For
SaveSetting APP_NAME, SECTION, "Item" & 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">' Safe retrieval with type conversion
On Error Resume Next
Dim timeout As Integer
timeout = CInt(GetSetting("MyApp", "Network", "Timeout", "30"))
If Err.Number <> 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>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>