Solved

Accessing .ini files from Visual BAsic 6.0

Posted on 2004-04-08
8
2,649 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
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 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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 130
groupNoAdj 7 116
Plain Text Editor for iPad 6 105
MS Access VBA How To Properly Set Up A multipart/related HTTP Request 13 73
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.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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…
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…

730 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