Solved

Accessing .ini files from Visual BAsic 6.0

Posted on 2004-04-08
8
2,647 Views
Last Modified: 2012-08-14
In Visual Basic 6.0

I have a form, a string varibale and a button.
When I click the button I want to look in a ini file that is on my C drive.

C:\Tickets.ini

and inside the Tickets.ini file I have

[Net_Location]
Location = K:\Tickets\Jobs

I want to put K:\Tickets\Jobs into my string variable.

How can I access the ini file in VB 6.0 and put K:\Tickets\Jobs into My_Variable.

Thanks,

Jason.
0
Comment
Question by:JasonAsh
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Assisted Solution

by:Leroyski
Leroyski earned 125 total points
ID: 10783324
I have this function to read from ini files:

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal StringSize As Long, ByVal FileName As String) As Long

In your case, you could do the following:

sRetBuf$ = String$(256, 0)
My_Variable = GetPrivateProfileString("Net_Location", "Location", "", sRetBuf$, Len(sRetBuf$), "C:/Tickets.ini")

Hope that helps.
0
 

Author Comment

by:JasonAsh
ID: 10783481
Thanks Leroyski,

I have copied onto my form:

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal StringSize As Long, ByVal FileName As String) As Long

and I get the following error::

Compile error:

Constants, fixed-length strings, arrays, user-defined types and Declare
statements not allowed as Public members of object modules.

Any Ideas ?
0
 
LVL 1

Expert Comment

by:Leroyski
ID: 10783503
Ah, sorry about that, that works if you put the function in a module (which I always do with my functions).

If you want to use it straight on your form itself (in other words, the object module the error blabbers about), you need to make it a Private function instead of a Public one, so you get

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal SectionName As String, ByVal KeyName As String, ByVal Default As String, ByVal ReturnedString As String, ByVal StringSize As Long, ByVal FileName As String) As Long
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 10

Accepted Solution

by:
Kavar earned 125 total points
ID: 10783505
here is what I use

in your bas put this declaration
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

then use this wrapper function

Public Function GetSetting(WhatSection As String, WhatKey As String, DefaultValue As String) As String
Dim Results As String
Dim Length As Long
Dim WhatINI As String
Dim returnCode As Long

Length = 255
WhatINI =<your ini path here>
Results = String(Length, " ")
returnCode = GetPrivateProfileString(WhatSection, WhatKey, DefaultValue, Results, Length, WhatINI)
GetSetting = Left(Results, returnCode)

End Function
0
 
LVL 10

Expert Comment

by:Kavar
ID: 10783513
Make sure that you put this in a MODULE

not in a form
0
 

Author Comment

by:JasonAsh
ID: 10784019
Sorry Guys I am getting my knickers in a right twist over this one !

If I put either of the codes into a module do I call the module from my button command ?

So for example I want the string from the ini file to go into text1.text

I put all the code above into a Module then under my button command I
have a call then have text1.text = my_variable or getsetting ?
0
 

Expert Comment

by:Tuvai
ID: 10784266
You have to put the function in a module, once it's there your forms will automaticly be able to use whatever is in it.
0
 

Author Comment

by:JasonAsh
ID: 10784338
Thank you both I have sussed out from your tips and got it working.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

9 Experts available now in Live!

Get 1:1 Help Now