Solved

Accessing .ini files from Visual BAsic 6.0

Posted on 2004-04-08
8
2,651 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
A short article about problems I had with the new location API and permissions in Marshmallow
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 …
Introduction to Processes

707 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