?
Solved

Accessing .ini files from Visual BAsic 6.0

Posted on 2004-04-08
8
Medium Priority
?
2,655 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 375 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
Technology Partners: 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 375 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes
Suggested Courses

765 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