Solved

Accessing .ini files from Visual BAsic 6.0

Posted on 2004-04-08
8
2,643 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to setup 18 79
Reading variable length EBCDIC in SAS 9 75
oracle query help 18 77
Please explain: Aspect Oriented Programming 2 53
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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

16 Experts available now in Live!

Get 1:1 Help Now