Solved

Create .ini file to pull and put settings to.

Posted on 1999-01-25
3
244 Views
Last Modified: 2010-05-03
How can I create a .ini file to save program settings to and retrieve the next time the program is loaded?
0
Comment
Question by:q_ship
3 Comments
 
LVL 4

Accepted Solution

by:
CraigLazar earned 50 total points
ID: 1470704
Hi
here is some code thath does the trick
Private Sub Command1_Click()
       CommonDialog1.Action = 1
       PathField.Text = CommonDialog1.filename
       Dim path As String
       Dim PathName As String
       PathName = PathField.Text
       Open "c:\tflorist.InI" For Output Shared As #1
       Write #1, PathName    ' Write comma-delimited data.
       Write #1,   ' Write blank line.
       Close #1
       Unload Registry
End Sub

this function pops up a form for the user to browse and select the database file. It then auto creates an ini file called tflorist.ini with the path that the user selected

'********  to read from the ini **********
 Public Function databasePath()
On Error GoTo error_handler:
        Open "c:\Tflorist.InI" For Input Shared As #1
        Input #1, TempPath
        Close #1

error_handler:
If Err.Number = 53 Then
       MsgBox "Please can you use the next screen to Locate the Database file on the Network Server", vbInformation
       Load Registry
       Registry.Show 1
      Resume
       End If
End Function

This function opens and reads the ini file for it's value

Now if you sre going to have multiple values in an ini file you need to make calls to the Api
getprivateprofile String
writeprivateprofile string

'Declare statements for the Dll's which go in the General Declarations Section of the form
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Declare Function WritePrivateProfileStringA Lib "kernel32.dll" (ByVal ipszsection As String, ByVal ipszkey As String, ByVal ipszString As String, ByVal ipszfile As String) As Long

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _

Dim wTest As Long
Dim wAppName As String
Dim wKeyName As String
Dim wString As String
Dim wFile As String
Dim rAppName As String
Dim rKeyName As String
Dim rDefault As String
Dim rReturnedStrings As String
Dim rSize As Long
Dim rFile As String
Dim rTest As Long
Dim Tempbuff As Variant

Call DatPath
Dim teste As String
dbpath = dbpat
rAppName = "Drivers"
rKeyName = "bpsdriver"
rDefault = "not"
rReturnedStrings = Tempbuff
rSize = 20
rFile = "c:\windows\system.ini"
Tempbuff = GetPrivateProfileString(rAppName, rKeyName, rDefault, rReturnedStrings, rSize, rFile)
If Tempbuff <> 10 Then
    wAppName = "Drivers"
    wKeyName = "Bpsdriver"
    wString = "bpsdrv.dll"
    wFile = "system.ini"
    wTest = WritePrivateProfileStringA(wAppName, wKeyName, wString, wFile)
End If


good luck
Craig
0
 
LVL 18

Expert Comment

by:mdougan
ID: 1470705
Just a note, GetPrivateProfileString has a place where you can specify "default" values - so, in the procedure where you call it, if GetPrivateProfileString does not find your INI file, then it will return the defaults you provide - so you can populate your program variables.  

Then, in your application, when you close down, you should WritePrivateProfileString your program variables to the INI file.  If the INI file does not exist (as it wont the first time you run), then it will create it for you with the proper Section names etc.

The next time you run your program, then GetPrivateProfileString should be able to find your INI file.

0
 

Author Comment

by:q_ship
ID: 1470706
Thank you for the code, that points me in the right direction!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 56
VBA filters 2 59
Access Object Property from VBA Module in Excel 2010 2 31
Prevent user closing word document opened with VB6 6 73
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question