Solved

Create .ini file to pull and put settings to.

Posted on 1999-01-25
3
246 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

839 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