Loading

Creating a Config.INI File in VB6.0

Submitted by: 


In the Module File I name it INIWrite.bas
Here is the code:
Author: kevern
Copyright © 2009 Kevern Solutions

  1. Option Explicit
  2.  
  3. Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
  4. Dim temp As String
  5. Dim LcaseTemp As String
  6. Dim ReadKey As String
  7. Dim ReadVariable As String
  8. Dim LOKEY As Integer
  9. Dim HIKEY As Integer
  10. Dim KEYLEN As Integer
  11. Dim VAR As Integer
  12. Dim VARENDOFLINE As Integer
  13. Dim NF As Integer
  14. Dim X As Integer
  15.  
  16. AssignVariables:
  17. NF = FreeFile
  18. ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
  19. KEYLEN = Len(ReadKey)
  20. ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
  21.  
  22. EnsureFileExists:
  23. Open INIpath For Binary As NF
  24. Close NF
  25. SetAttr INIpath, vbArchive
  26.  
  27. LoadFile:
  28. Open INIpath For Input As NF
  29. temp = Input$(LOF(NF), NF)
  30. temp = vbCrLf & temp & "[]"
  31. Close NF
  32. LcaseTemp = LCase$(temp)
  33.  
  34. LogicMenu:
  35. LOKEY = InStr(LcaseTemp, ReadKey)
  36. If LOKEY = 0 Then GoTo AddKey:
  37. HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
  38. VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
  39. If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
  40. GoTo RenewVariable:
  41.  
  42. AddKey:
  43. temp = Left$(temp, Len(temp) - 2)
  44. temp = temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
  45. GoTo TrimFinalString:
  46.  
  47. AddVariable:
  48. temp = Left$(temp, Len(temp) - 2)
  49. temp = Left$(temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(temp, LOKEY + KEYLEN + 1)
  50. GoTo TrimFinalString:
  51.  
  52. RenewVariable:
  53. temp = Left$(temp, Len(temp) - 2)
  54. VARENDOFLINE = InStr(VAR, temp, Chr$(13))
  55. temp = Left$(temp, VAR) & PutVariable & "=" & PutValue & Mid$(temp, VARENDOFLINE)
  56. GoTo TrimFinalString:
  57.  
  58. TrimFinalString:
  59. temp = Mid$(temp, 2)
  60. Do Until InStr(temp, vbCrLf & vbCrLf & vbCrLf) = 0
  61. temp = Replace(temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
  62. Loop
  63.  
  64. Do Until Right$(temp, 1) > Chr$(13)
  65. temp = Left$(temp, Len(temp) - 1)
  66. Loop
  67.  
  68. Do Until Left$(temp, 1) > Chr$(13)
  69. temp = Mid$(temp, 2)
  70. Loop
  71.  
  72. OutputAmendedINIFile:
  73. Open INIpath For Output As NF
  74. Print #NF, temp
  75. Close NF
  76. End Function
  77.  
  78. This is the INIRead File I name it INIRead.bas
  79. Here is the Code:
  80. Option Explicit
  81.  
  82. Public Function ReadIniValue(INIpath As String, KEY As String, Variable As String) As String
  83. Dim NF As Integer
  84. Dim temp As String
  85. Dim LcaseTemp As String
  86. Dim ReadyToRead As Boolean
  87.  
  88. AssignVariables:
  89. NF = FreeFile
  90. ReadIniValue = ""
  91. KEY = "[" & LCase$(KEY) & "]"
  92. Variable = LCase$(Variable)
  93.  
  94. EnsureFileExists:
  95. Open INIpath For Binary As NF
  96. Close NF
  97. 'SetAttr INIpath, vbArchive
  98.  
  99. LoadFile:
  100. Open INIpath For Input As NF
  101. While Not EOF(NF)
  102. Line Input #NF, temp
  103. LcaseTemp = LCase$(temp)
  104. If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
  105. If LcaseTemp = KEY Then ReadyToRead = True
  106. If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
  107. If InStr(LcaseTemp, Variable & "=") = 1 Then
  108. ReadIniValue = Mid$(temp, 1 + Len(Variable & "="))
  109. Close NF: Exit Function
  110. End If
  111. End If
  112. Wend
  113. Close NF
  114. End Function



Comments

You can have more sample code in my blog, just take a visit here: http://jackjones2010.blogspot.com/2010/07/creating-configini-file-in-vb60.html
Thanks and Good Luck!

~kevern~

how to create a payroll history of an employee? from the day of hiring up to present..

Good day,

What you mean payrol history? history of every employee transaction or the employee database.?

I wonder what is the method to automatically poll key strokes from outside vb applications. and if any source program to this use.

Thanks

Mike Kim

sana sinama mo na din un iniwrite.bas..
anyways, here is the original author of this module..
Bernie Madigan

I am an experience VB Programmer, I don't take advices from other authors, I created this code w/o any help from others. Keep that in mind!

Pages

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.