Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database Locations in an INI or INF File??

Status
Not open for further replies.

richrider

Programmer
Aug 4, 2000
3
US

Hi There,

Someone a while back had told me that the best way to have control of the location of my database, and it's recordsets would to create an INI or INF File containing this information and to point my VB application to their location. Needless to say I haven't been able to figure this out yet. Can anyone give me an example of what needs to be done to link to an INI or INF file (if there is a difference?) and what my vb code and any code in the INI/INF file should look like?

Thanks a bunch!

Rich H.
 
I would use API calls to getinistring and writeinistring:

In your project, have a module with the following code:

' Read INI file
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
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
' Write INI file
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

Public Function sGet_INI_String(rsSection As String, rsEntry As String) As String
' ------------------------
' Retrieve INI file string
' ------------------------

Dim lReturn As Long
Dim sINIString As String
Dim sEntry As String
Dim rsFileName As String

rsFileName = gsINIPATH ' global variable holding full path and name of ini file

' Initialise buffer - safer and quicker
' than fixed length string
sINIString = Space$(255)

' rsEntry must be assigned to local variable to work
sEntry = rsEntry

lReturn = GetPrivateProfileString(rsSection, sEntry, "", sINIString, Len(sINIString), rsFileName)

If lReturn > 0 Then
sGet_INI_String = Left$(sINIString, lReturn)
Else
sGet_INI_String = ""
End If

End Function

Sub Write_INI_String(rsSection As String, rsEntry As String, rsValue As String)
' ---------------------
' Write INI file string
' ---------------------

Dim lReturn As Long
Dim rsFileName As String

rsFileName = gsINIPATH ' global variable holding full path and name of ini file

If rsEntry <> &quot;&quot; Then
lReturn = WritePrivateProfileString(rsSection, rsEntry, rsValue, rsFileName)
Else
' To remove Section
lReturn = WritePrivateProfileString(rsSection, vbNullString, rsValue, rsFileName)
End If
If lReturn = 0 Then
' Error
End If

End Sub


In your app, call the functions by:
1. to read the ini file:

Dim sString AS String
sString = sGet_INI_String(&quot;Database&quot;, &quot;Path&quot;)


2. to write to the ini file:

Write_INI_String &quot;Database&quot;, &quot;Path&quot;, &quot;C:\data.mdb&quot;

Your ini file would be a normal text file (I normally call mine data.ini - or sometimes name_of_project.ini) which you can edit in Notepad and would look like this:

[Database]
Path=c:\data.mdb


[Database] signifies the start of the section called &quot;Database&quot;.
Path is the entry called &quot;Path&quot;.
Within the section you can have whatever entries you like. For example, you may have the following entries:

[Database]
Server=\\server1\c\data.mdb
Client=c:\data.mdb


You can add more sections if you want, and you can repeat entry names IN DIFFERENT SECTIONS, BUT NOT IN THE SAME SECTION.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top