Solved

Create .ini file to pull and put settings to.

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now