設定テーブルを使用する
以下の様なテーブルを作成します。
テーブル名:tblinitialize
フィールド名:Section, Key, Value
設定を取得するには、以下の様にします。
str設定 = DLookup("Value", "tblinitialize", "Section = 'セクション' AND Key = 'キー'")
設定ファイルを使用する
'〔初期化ファイル関連〕
'.INI ファイルから文字列を取得する
Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private Sub コマンド1_Click()
Dim fstrBuffer As String * 256
Dim intBufferSize As Integer
intBufferSize = Len(fstrBuffer)
Dim strInitializeFilePath As String
strInitializeFilePath = Application.CurrentProject.Path & "\設定.ini"
Dim lngRet As Long
'すべてのセクション名を取得する
lngRet = GetPrivateProfileString(vbNullString, vbNullString, "", fstrBuffer, intBufferSize, strInitializeFilePath)
Dim strBuffer As String
strBuffer = Left(fstrBuffer, InStr(fstrBuffer, vbNullChar & vbNullChar) - 1)
Dim strSections() As String
strSections = Split(strBuffer, vbNullChar)
Dim varSection As Variant
For Each varSection In strSections
Dim strSection As String
strSection = varSection
fstrBuffer = String(intBufferSize, vbNullChar)
'セクション中のすべてのキーを取得する
lngRet = GetPrivateProfileString(varSection, vbNullString, "", fstrBuffer, intBufferSize, strInitializeFilePath)
strBuffer = Left(fstrBuffer, InStr(fstrBuffer, vbNullChar & vbNullChar) - 1)
Dim strKeys() As String
strKeys = Split(strBuffer, vbNullChar)
Dim varKey As Variant
Dim strKey As String
For Each varKey In strKeys
strKey = varKey
'キーの値を取得する
lngRet = GetPrivateProfileString(varSection, strKey, "", fstrBuffer, intBufferSize, strInitializeFilePath)
Dim strValue As String
strValue = Left(fstrBuffer, InStr(fstrBuffer, vbNullChar) - 1)
Debug.Print "[" & varSection & "] " & strKey & "=" & strValue
Next
Next
End Sub
例
cls設定
'〔初期化ファイル関連〕
'.INI ファイルから文字列を取得する
Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Private strセクション1_キー1 As String
Private strセクション1_キー2 As String
Private Sub Class_Initialize()
Dim strInitializeFilePath As String
strInitializeFilePath = Application.CurrentProject.Path & "\設定.ini"
Dim fstrValue As String
Dim lngRet As Long
fstrValue = String(256, vbNullChar)
lngRet = GetPrivateProfileString("セクション1", "キー1", "", fstrValue, 256, strInitializeFilePath)
strセクション1_キー1 = Left(fstrValue, InStr(fstrValue, vbNullChar) - 1)
fstrValue = String(256, vbNullChar)
lngRet = GetPrivateProfileString("セクション1", "キー2", "", fstrValue, 256, strInitializeFilePath)
strセクション1_キー2 = Left(fstrValue, InStr(fstrValue, vbNullChar) - 1)
End Sub
Public Property Get セクション1_キー1()
セクション1_キー1 = strセクション1_キー1
End Property
Public Property Get セクション1_キー2()
セクション1_キー2 = strセクション1_キー2
End Property
mod共通
Public gobj設定 As New cls設定
Private Sub コマンド1_Click()
Debug.Print (gobj設定.セクション1_キー1)
Debug.Print (gobj設定.セクション1_キー2)
End Sub