|
|
Настройки - Работа с INI файлом локальных настроек приложения (API)
Создаём , пишем и читaем INI файл в формате:
[Имя раздела]
Название параметра = Значение параметра
Пример использования:
Private Sub INITest()
INIWrite "Путь к БАЗЕ", CurrentProject.Path, "Настройка Приложения"
MsgBox INIRead("Путь к БАЗЕ", "НЕ ЗНАЮ!", "Настройка Приложения")
End Sub
Option Compare Database
Option Explicit
Private Const INIFileName As String = "Application.ini"
#If VBA7 Then
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 Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
#Else
Private Declare 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 Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
#End If
Public Sub INIWrite(sName As String, ByVal sValue As String, Optional sPart As String = "Settings")
Dim FilePath As String
Dim intRet As Integer
On Error GoTo INIWriteErr
FilePath = CurrentProject.Path & "\" & INIFileName
intRet = WritePrivateProfileString(sPart, sName, sValue, FilePath)
If intRet <> 1 Then
MsgBox "Процедура INIWrite не смогла записать параметр INI Файла:" & vbCrLf & _
FilePath & vbCrLf & _
"-----------------------------------------------------------------" & vbCrLf & _
"[" & sPart & "]" & vbCrLf & sName & "=" & sValue, vbExclamation
End If
Exit Sub
INIWriteErr:
MsgBox "Процедура INIWrite привела к ошибке:" & vbCrLf & _
"#" & Err.Number & " " & Err.Description, vbCritical
End Sub
Public Function INIRead(sName As String, Optional sDefaultValue As String = "", Optional sPart As String = "Settings") As String
Const strNoValue As String = ""
Dim FilePath As String
Dim intRet As Integer
Dim strRet As String
On Error GoTo INIReadErr
FilePath = CurrentProject.Path & "\" & INIFileName
strRet = String(255, Chr(0))
intRet = GetPrivateProfileString(sPart, sName, strNoValue, strRet, 255, FilePath)
strRet = Left$(strRet, intRet)
If strRet = strNoValue Then
INIWrite sName, sDefaultValue, sPart
strRet = sDefaultValue
End If
INIRead = strRet
Exit Function
INIReadErr:
MsgBox "Функция INIRead привела к ошибке:" & vbCrLf & _
"#" & Err.Number & " " & Err.Description, vbCritical
End Function
Дополнительно:
Private Sub INICheckAndCreate(strFilePath$)
Dim objADODBStream As Object
Const csNewEmptyText$ = "[Settings]" & vbCrLf & vbCrLf & _
"[SQL Server Connection Settings 00]" & vbCrLf & vbCrLf & _
"[SQL Server Connection Settings 01]" & vbCrLf & vbCrLf & _
"[SQL Server Connection Settings 02]"
If Dir(strFilePath, vbNormal) = "" Then
If objADODBStream Is Nothing Then Set objADODBStream = CreateObject("ADODB.Stream")
With objADODBStream
.Type = 2: .Charset = "Windows-1251": .Open
.WriteText csNewEmptyText
.SaveToFile strFilePath, 2
.Close
End With
DoEvents
Set objADODBStream = Nothing
End If
End Sub
|
|