473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading&Writing from&to .INI files using VBA code

I have written code to enter data for the ini file in Access 2000. The ini file is the input for a report using Crystal Reports. I would like to replace the value in the current ini file with the new values entered into the Access form. Can someone tell me how to do this.

Here is a sample of the data in the current ini file I would like to replace the value in the lines User*Value= (value):


[ExtraSampleInfo]
ItemCount=11
User1Label=Gas Temperature
User1Value=50
User1SaveData=0
User2Label=Company
User2Value=duke
User2SaveData=0
User3Label=Sample Date
User3Value=2/5/6
User3SaveData=0
User4Label=Station Name
User4Value=test
User4SaveData=0
User5Label=Sampled By
User5Value=jb
User5SaveData=0
User6Label=Station Number
User6alue=
User6SaveData=0
User7Label=Flowing Pressure
User7Value=67
User7SaveData=0
User8Label=Plant
User8Value=Rockcreek
User8SaveData=0
User9Label=Cylinder #
User9Value=23
User9SaveData=0
User10Label=Composite/Spot
User10Value=Spot.
User10SaveData=1
User11Label=Barometer
User11Value=
User11SaveData=0
Col1Label=Item
Col1Width= 2190
Col2Width= 1920
User6Value=12345
Peak1Value=0

[ExtraPeakInfo]
ItemCount=2
Peak1Label=H2S
Peak1Value=0.5
Peak2Label=Oxygen
Peak2value=0.08
Col1Label=" Peak"
Col1Width= 1860
Col2Label=" Result"
Col2Width= 2445


I found the code, VBnet, Randy Birch. I would like to change this code into VBA for Access if anyone can help me couldn't find a way to contact Randy.

thanks for you help
Nov 2 '06 #1
6 21417
pks00
280 Expert 100+
Im not sure what code u have but I have used this in the past successfully

Add this lot to a module

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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
  3. Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
  4. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
  5.  
  6.  
  7. Private Const CONFIG_FILE = "C:\EE\DBConfig.ini"
  8.  
  9.  
  10. Public Function GetINIString(Byval sApp as String, ByVal sKey As String) As String
  11.  
  12.     Dim sBuf As String * 256
  13.     Dim lBuf As Long
  14.  
  15.     lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), "C:\myfile.ini")
  16.     GetINIString= Left$(sBuf, lBuf)
  17. End Function
  18.  
  19.  
  20. Public Function WriteINI(Byval sApp as String, ByVal sKey As String, ByVal sValue As String) As String
  21.     WritePrivateProfileString sApp, sKey, sValue, "C:\myfile.ini"
  22. End Function
  23.  
  24.  
then use GetINIString specifing application and key to get
i.e.

x = GetINIString("ExtraSampleInfo","User1Label")

and WriteINI to save a value


Note example for strings, should work for numbers but u can always use the other api call provided
Nov 2 '06 #2
This code should just about do it. You will need to debug It & modifiy it a little but it should be essentially correct

Expand|Select|Wrap|Line Numbers
  1. Sub IpdateIniFile(UserNo As Integer, NewValue As Long)
  2.  
  3. Dim cnt As Integer
  4. Dim IniLine As String
  5. Dim UserID As String
  6. Dim Pth As String
  7. Dim TempFile As String
  8.  
  9.     UserID = Trim(CStr(UserNo))
  10.     ' replace with the path to ytour ini file
  11.     Pth = "Path\YourIniFile.ini"
  12.     TempFile = "Path\Temp.ini"
  13.     Open Pth For Input As #1
  14.     Open TempFile For Output As #2
  15.     Do Until EOF(1)
  16.          Line Input #1, IniLine
  17.         If Left(IniLine, 4) = "User" And Mid(IniLine, 5, Len(UserID)) = UserID And Mid(IniLine, Len(UserID) + 4, 5) = "Value " Then ' we have found the users entries
  18.             IniLine = "User" & UserID & "Value=" & Trim(CStr(NewValue))
  19.         End If
  20.         Print #2, IniLine
  21.     Loop
  22.     Close 1
  23.     Close 2
  24.     Kill Pth
  25.     Name TempFile As Pth
  26.  
  27. End Sub
  28.  
Nov 2 '06 #3
Im not sure what code u have but I have used this in the past successfully

Add this lot to a module

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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
  3. Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
  4. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
  5.  
  6.  
  7. Private Const CONFIG_FILE = "C:\EE\DBConfig.ini"
  8.  
  9.  
  10. Public Function GetINIString(Byval sApp as String, ByVal sKey As String) As String
  11.  
  12.     Dim sBuf As String * 256
  13.     Dim lBuf As Long
  14.  
  15.     lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), "C:\myfile.ini")
  16.     GetINIString= Left$(sBuf, lBuf)
  17. End Function
  18.  
  19.  
  20. Public Function WriteINI(Byval sApp as String, ByVal sKey As String, ByVal sValue As String) As String
  21.     WritePrivateProfileString sApp, sKey, sValue, "C:\myfile.ini"
  22. End Function
  23.  
  24.  
then use GetINIString specifing application and key to get
i.e.

x = GetINIString("ExtraSampleInfo","User1Label")

and WriteINI to save a value


Note example for strings, should work for numbers but u can always use the other api call provided
ok, this is close to the code I was looking at from Randy. But I am not understanding where to enter it. You said in a module, is that the code behind a form or in a code module?

"then use GetINIString specifing application and key to get", how do I do this?
I am still learning Access and have a long way to go. Thanks for your help.
Nov 3 '06 #4
This code should just about do it. You will need to debug It & modifiy it a little but it should be essentially correct

Expand|Select|Wrap|Line Numbers
  1. Sub IpdateIniFile(UserNo As Integer, NewValue As Long)
  2.  
  3. Dim cnt As Integer
  4. Dim IniLine As String
  5. Dim UserID As String
  6. Dim Pth As String
  7. Dim TempFile As String
  8.  
  9.     UserID = Trim(CStr(UserNo))
  10.     ' replace with the path to ytour ini file
  11.     Pth = "Path\YourIniFile.ini"
  12.     TempFile = "Path\Temp.ini"
  13.     Open Pth For Input As #1
  14.     Open TempFile For Output As #2
  15.     Do Until EOF(1)
  16.          Line Input #1, IniLine
  17.         If Left(IniLine, 4) = "User" And Mid(IniLine, 5, Len(UserID)) = UserID And Mid(IniLine, Len(UserID) + 4, 5) = "Value " Then ' we have found the users entries
  18.             IniLine = "User" & UserID & "Value=" & Trim(CStr(NewValue))
  19.         End If
  20.         Print #2, IniLine
  21.     Loop
  22.     Close 1
  23.     Close 2
  24.     Kill Pth
  25.     Name TempFile As Pth
  26.  
  27. End Sub
  28.  

I put this code into Visual Basic 6, changed the paths to my directories. This seems to work, but what does the Kill Pth do?
and what does the Name Tempfile as Pth do?
When I run the code the Temp.ini file disappears.
Nov 3 '06 #5
pks00
280 Expert 100+
Just put the code in a normal module, dont need it in a form module.
If in a module, then all forms and other modules have access to it
Nov 3 '06 #6
Kill pth deletes the original INI file
Name Tempfile as Pth renames the output file to the INI file.

The program works by sequentially reading from the ini file & writing to a temp file, changing the rquired lines as it goes. It then deletes the original INI file and renames the temp file to the INI file.
Nov 3 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
by: lec | last post by:
Hi, I'm trying to write a program to read from the serial port & write whatever that is read to the X console (/dev/tty7). For X to recognize the characters sent, I believe you have to send...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
12
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA...
16
by: Ali | last post by:
Hi I want to write (or read) to a stream, but the data is not byte array I converted the data to byte array manually, but it is very slow, (becuse the data is very large) Is another way for this...
1
by: lion | last post by:
helo...... i need help for how to read/write file in C++ i use fstream but i wont to open file one time for all functions(file is glopal) and i wont the file be open in the constructor of my...
1
by: KWSW | last post by:
Got a question about reading and writing binary files. I know that to write a value(integer) to a binary file I can use the fileoutputstream to do it. try { // Create an output...
0
by: Roman Optio | last post by:
Hi, I am currently working on a tool to transfer addressbook data such as contacts and organizations between Vondle and Outlook. For those of you who are wondering what Vondle is, it is an...
7
by: kimmelsd33 | last post by:
I am using VB6. I want to read a tab delimited file, and assign each column value into a variable. If the variable is "-999.25", I want to make it a "0". I then want to reassemble the values, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.