Four functions for reading and writing INI files

Source: Internet
Author: User
Tags chr goto ini trim
four functions for reading and writing INI files' FileName Sourcedb.ini file
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

' The following two functions, read/write INI file, fixed node Setting,in_key for write/Read primary key
' Only for non-value
' Y:yes,n:no,e:error
Public Function GETINITF (ByVal In_key as String) as Boolean
On Error GoTo Getinitferr
GETINITF = True
Dim Getstr as String
Getstr = VBA. String (128, 0)
GetPrivateProfileString "Setting", In_key, ", Getstr, App.Path &/sourcedb.ini"
Getstr = VBA. Replace (Getstr, VBA. CHR (0), "")
If getstr = "1" Then
GETINITF = True
Getstr = ""
Else
GoTo Getinitferr
End If
Exit Function
Getinitferr:
Err.Clear
GETINITF = False
Getstr = ""
End Function

Public Function WRITEINITF (ByVal in_key as String, ByVal In_data as Boolean) as Boolean
On Error GoTo Writeinitferr
WRITEINITF = True
If In_data = True Then
WritePrivateProfileString "Setting", In_key, "1", App.Path & "/sourcedb.ini"
Else
WritePrivateProfileString "Setting", In_key, "0", App.Path & "/sourcedb.ini"
End If
Exit Function
Writeinitferr:
Err.Clear
WRITEINITF = False
End Function


' The following two functions, read/write INI file, not fixed node, In_key for write/Read primary key
' For string values
' Null value indicates an error
Public Function getinistr (ByVal AppName As String, ByVal In_key as String) as String
On Error GoTo Getinistrerr
If VBA. Trim (In_key) = "Then
GoTo Getinistrerr
End If
Dim Getstr as String
Getstr = VBA. String (128, 0)
GetPrivateProfileString AppName, In_key, ", Getstr, App.Path &/sourcedb.ini"
Getstr = VBA. Replace (Getstr, VBA. CHR (0), "")
If getstr = "Then
GoTo Getinistrerr
Else
Getinistr = Getstr
Getstr = ""
End If
Exit Function
Getinistrerr:
Err.Clear
Getinistr = ""
Getstr = ""
End Function

Public Function writeinistr (ByVal AppName As String, ByVal In_key as String, ByVal In_data as String) as Boolean
On Error GoTo Writeinistrerr
Writeinistr = True
If VBA. Trim (In_data) = "" Or VBA. Trim (In_key) = "" Or VBA. Trim (AppName) = "Then
GoTo Writeinistrerr
Else
WritePrivateProfileString AppName, In_key, In_data, App.Path & "/sourcedb.ini"
End If
Exit Function
Writeinistrerr:
Err.Clear
Writeinistr = False
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.