Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 84
Default Who do you read an ini file with Excel VBA?

Hello,

I need to have a couple of offices use an application. The front end is
written in Excel with Userforms and VBA. The back end is SQL Server.

One office is on site. The other office is in another building. I need to
create a DSN for that office that can see the data across the internet. But
I also need to create an ini file so that the application can retrieve a
value and know whether to access the data directly or use a dsn.

Question
======
What is the code to read an ini from within Excel VBA?


Thanks,

Tony
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Who do you read an ini file with Excel VBA?

Hi,

This would read an .ini file line by line. The message box isn't necessary,
I put it there to demonstrate it works.

Sub Read_INI()
saveDir = "C:\" 'Place where your INI file is
filenum = FreeFile
targetfile = saveDir & "Myfile.ini" 'Change to your filename
Open targetfile For Input As filenum
Do While Not EOF(filenum)
Input #filenum, Line$
MsgBox Line$
Loop
Close #filenum
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"Webtechie" wrote:

Hello,

I need to have a couple of offices use an application. The front end is
written in Excel with Userforms and VBA. The back end is SQL Server.

One office is on site. The other office is in another building. I need to
create a DSN for that office that can see the data across the internet. But
I also need to create an ini file so that the application can retrieve a
value and know whether to access the data directly or use a dsn.

Question
======
What is the code to read an ini from within Excel VBA?


Thanks,

Tony

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,203
Default Who do you read an ini file with Excel VBA?

Typically a .ini file is just a simple ASCII text file. The trick to reading
one is to know what you expect to find in it, where to find it and how to
parse it. Here are the basic pieces you'll need. I have one line in it that
simply echos the .ini file contents to the currently active sheet - that can
help while you're trying to code up the parsing. Look at the Instr()
function in VB, it can help with the parsing, and you'll probably end up
using Mid(), Left() and/or Right().

Sub INI_FileReading()
Dim myIniFile As String
Dim iniBuff As Integer
Dim rawLineInput As String

'set up a path to your .ini file
myIniFile = "x:\folder\folder\theFile.ini"
iniBuff = Freefile() ' get available file buffer number
Open myIniFile For Input As #iniBuff

Do While Not (EOF(iniBuff))
Line Input #iniBuff, rawLineInput ' get one line from the file
'next line will show you what's being read from the file
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = rawLineInput
'parse the lines of data here and save any values you need to use later
Loop
Close #iniBuff

End Sub
"Webtechie" wrote:

Hello,

I need to have a couple of offices use an application. The front end is
written in Excel with Userforms and VBA. The back end is SQL Server.

One office is on site. The other office is in another building. I need to
create a DSN for that office that can see the data across the internet. But
I also need to create an ini file so that the application can retrieve a
value and know whether to access the data directly or use a dsn.

Question
======
What is the code to read an ini from within Excel VBA?


Thanks,

Tony

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Who do you read an ini file with Excel VBA?

To read or write to an .ini you need to use the API as there can be a
problem
to do with the Windows cache when you open the file as described in the
previous posts.
There is code on Chip Pearson's site that should do the job or you could use
something like this:

Option Explicit
Private 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


Function ReadINIValue(strINIPath As String, _
vSection As Variant, _
vKey As Variant, _
Optional vDefault As Variant = "<no value") As String

Dim buf As String * 256
Dim Length As Long

On Error GoTo ERROROUT

If bFileExists(strINIPath) = False Then
ReadINIValue = "<no file"
Exit Function
End If

Length = GetPrivateProfileString(vSection, _
vKey, _
vDefault, _
buf, _
Len(buf), _
strINIPath)

ReadINIValue = Left$(buf, Length)

Exit Function
ERROROUT:

ReadINIValue = "<error"

End Function


Function bFileExists(strFile As String) As Boolean

Dim lAttr As Long

On Error Resume Next

lAttr = GetAttr(strFile)
bFileExists = (Err.Number = 0) And ((lAttr And vbDirectory) = 0)

End Function


RBS


"Webtechie" wrote in message
...
Hello,

I need to have a couple of offices use an application. The front end is
written in Excel with Userforms and VBA. The back end is SQL Server.

One office is on site. The other office is in another building. I need
to
create a DSN for that office that can see the data across the internet.
But
I also need to create an ini file so that the application can retrieve a
value and know whether to access the data directly or use a dsn.

Question
======
What is the code to read an ini from within Excel VBA?


Thanks,

Tony


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you save an excel file to be read as IBM-type text file ? Dee Franklin Excel Worksheet Functions 2 October 10th 06 02:46 AM
excel 2002 -how can you convert a file to a pdf read only file? spunkyf15 Excel Discussion (Misc queries) 4 September 29th 06 09:36 PM
Pasword protected Excel file encrypted, how do I read this file? jonesteam Excel Discussion (Misc queries) 2 December 12th 05 06:32 PM
"Unable to read file" error message when opening a Excel file that contains a PivotTable report. Tim Marsden Charts and Charting in Excel 2 October 15th 05 02:10 PM
Excel file won't open because it' a read only file when it is not IanB Excel Discussion (Misc queries) 1 September 12th 05 01:28 PM


All times are GMT +1. The time now is 02:40 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"