AccessVBAで外部ファイルに設定値を保存しておく方法
レジストリに保存が望ましいみたい
変数定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
'INIファイル Public Const str_iniFilePathName As String = "D:\F_Data_Ini\1005.ini" 'INI関連 Public Type Ini str_lpAppName As String str_lpKeyName As String lng_Leng As Long str_rtString As String rt As Long str_iniFilename As String str_lpDefault As String str_lpString As String int_IniData As Integer str_IniData As String End Type Public mIni As Ini Public str_KeyName As String Public str_AppName As String |
読み書きのモジュール
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
Option Compare Database Option Explicit Private Const str_ModuleName As String = "m_Initial" 'INIファイル読込 Public Declare Function GetPrivateProfileString Lib "kernel32" Alias _ "GetPrivateProfileStringA" _ (ByVal str_lpAppName As String, _ ByVal str_lpKeyName As String, _ ByVal str_lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long, _ ByVal str_iniFilename As String) As Long 'INIファイル書込 Public Declare Function WritePrivateProfileString Lib "kernel32" Alias _ "WritePrivateProfileStringA" _ (ByVal lpApplicationName As String, _ ByVal str_lpKeyName As String, _ ByVal str_lpString As String, _ ByVal lpFileName As String) As Long '-------------------------------------------------------------------- '【INIセクション名とキー名を指定で読込】20160225 '-------------------------------------------------------------------- Public Function ReadIni(str_AppName As String, str_KeyName As String) Const str_ProcName = "ReadIni" On Error GoTo ERR_ReadIni With mIni .str_lpAppName = str_AppName 'セクション名 .str_lpKeyName = str_KeyName 'キー名 .str_rtString = String(256, Chr(0)) '取得した文字列 .lng_Leng = Len(.str_rtString) 'サイズ .str_iniFilename = str_iniFilePathName '見つからなかった場合の規定値 .str_lpDefault = "" .rt = GetPrivateProfileString(.str_lpAppName, .str_lpKeyName, .str_lpDefault, .str_rtString, .lng_Leng, .str_iniFilename) 'NULL除去 'InStr(開始位置, 文字列, 検索文字列, 比較モード) 'Chr(0)=NULL If InStr(1, .str_rtString, Chr(0), vbBinaryCompare) > 0 Then .str_rtString = Left(.str_rtString, InStr(1, .str_rtString, Chr(0), vbBinaryCompare) - 1) End If 'Space除去 .str_IniData = Trim(.str_rtString) End With Exit Function ERR_ReadIni: MsgBox str_ModuleName & ":" & str_ProcName & ":" & Err.Number & ":" & Err.Description End Function '-------------------------------------------------------------------- '【INIファイル書込】 20160225 '-------------------------------------------------------------------- Public Function WriteIni(str_AppName As String, str_KeyName As String, str_String As String) Const str_ProcName = "WriteIni" On Error GoTo ERR_WriteIni With mIni .str_lpAppName = str_AppName 'セクション名 .str_lpKeyName = str_KeyName 'キー名 .str_lpString = str_String .str_iniFilename = str_iniFilePathName .rt = WritePrivateProfileString(.str_lpAppName, .str_lpKeyName, .str_lpString, .str_iniFilename) End With Exit Function |
コメント