Valhalla Legends Forums Archive | Visual Basic Programming | [VB] Problems reading INI file. (Solved)

AuthorMessageTime
LockesRabb
I'm using the below I found by searching on the forums. I had originally written my own using solely GetPrivateProfileString and WritePrivateProfileString, but I liked this one as it was considerably easier to use. It was written by I believe Stealth.

It writes fine to the INI, however when I try to pull up the value, it shows up as blank. I think there's an error somewhere in reading, but I'm unable to locate the error. Perhaps you guys will have better luck. Thanks in advance for any assistance rendered.

[code]Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Const sINIFile As String = "Sanity.ini"

'Read/WriteIni code thanks to ickis
Public Sub WriteINI(ByVal wiSection$, ByVal wiKey$, ByVal wiValue$, ByVal wiFile$)
    wiFile$ = (App.Path & "\" & wiFile$)
    WritePrivateProfileString wiSection, wiKey, wiValue, wiFile
End Sub

Public Function ReadINI(ByVal riSection$, ByVal riKey$, ByVal riFile$) As String
    Dim sRiBuffer$
    Dim sRiValue$
    Dim sRiLong$
 
    riFile$ = (App.Path & "\" & riFile$)
    If Dir(riFile$) <> "" Then
        sRiBuffer = String(255, vbNull)
        sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, riFile)
        If Left(sRiBuffer, 1) <> Chr(1) Then
            sRiValue = Left(sRiBuffer, sRiLong)
            ReadINI = sRiValue
        End If
    Else
        ReadINI = ""
    End If
End Function

Public Sub SaveOptions(sVariable As String, ByVal sValue)
    WriteINI "Options", sVariable, sValue, sFile
End Sub

Public Function GetOptions(sVariable)
    GetOptions = ReadINI("Options", sVariable, sINIFile)
End Function[/code]

And the code I'm using for the command button click is:

[code]
Private Sub Command2_Click()
    SaveOptions "Test", "TestValue"
    tempvar = GetOptions("Test")
End Sub[/code]

I checked the INI file, and it shows this:

[quote][Options]
Test=TestValue[/quote]

What do you think?
February 12, 2007, 7:50 AM
Barabajagal

Public Sub SaveOptions(sVariable As String, ByVal sValue)
    WriteINI "Options", sVariable, sValue, sFile
End Sub

sFile doesn't seem to be defined. You seem to be pretty new at VB with all these mistakes... enable Option Explicit and it won't happen.
February 12, 2007, 7:56 AM
LockesRabb
D'oh. Yeah, I never used Option Explicit mainly because it was annoying (I tend to not bother to properly define my variables). But considering how I just wasted your time with those silly newbish mistakes (very embarassing when I've been coding in VB for a year now), I think I'll take your advice and just use Option Explicit. At least it'll ensure I make less of those newbish mistakes.

Anyway, it works now. Thanks!
February 12, 2007, 11:00 AM
Quarantine
..did you just say you don't like predefining your variables? ..oh god. What has VB done to people..
February 12, 2007, 9:14 PM
UserLoser
[quote author=Warrior link=topic=16299.msg164649#msg164649 date=1171314891]
..did you just say you don't like predefining your variables? ..oh god. What has VB done to people..
[/quote]

VB code is a measurement of intelligence.
February 12, 2007, 9:21 PM
LockesRabb
Ouch. Burnt. >.<
February 12, 2007, 10:05 PM
Grok
[quote author=Kyro link=topic=16299.msg164617#msg164617 date=1171278024]
D'oh. Yeah, I never used Option Explicit mainly because it was annoying (I tend to not bother to properly define my variables). But considering how I just wasted your time with those silly newbish mistakes (very embarassing when I've been coding in VB for a year now), I think I'll take your advice and just use Option Explicit. At least it'll ensure I make less of those newbish mistakes.

Anyway, it works now. Thanks!
[/quote]

Good!  Remember this -- regardless of the language you use, the compiler probably includes helpful restrictions which protect you from ... yourself!  Option Explicit is one of those protections.  I wouldn't write professional code without this restriction.  When you're focused on other things, such as overall architecture, algorithms, data validation, exception handling, you're often writing code by the function, rather than line-by-line.  Having the compiler catch when you forget to declare and type a variable is a tremendous help.
February 23, 2007, 6:04 PM

Search