VB code help

smoochy boys on tour

WiseWarrior

LOMCN Veteran
Veteran
Loyal Member
Nov 16, 2003
327
0
62
14 NeverNeverLand
I am getting a VB program to read an INI file and display the text in a text box array, I am doing this using the following code.
Code:
Private Sub cmdRead_Click()

'Defining location of INI
    File = App.Path & "\LOM2.ini"
    OFLen = FileLen(File)

'Read INI and store in String Array
    Str(0) = ReadIni(File, "ledu", "ledu")
    Str(1) = ReadIni(File, "db", "db_svr")
    Str(2) = ReadIni(File, "game", "game_gate")
    Str(3) = ReadIni(File, "login1", "login_gate")
    Str(4) = ReadIni(File, "login2", "login_svr")
    Str(5) = ReadIni(File, "select", "sel_gate")
    Str(6) = ReadIni(File, "m2", "m2_svr")
    
'Take Array values and place in Text Value Array to display
    For i = 0 To 6
        txtPaths(i) = Str(i)
    Next i
    
End Sub

The problem I am facing is that when I display the fields I want, the display does not show speech marks, numbers (unless they were in speech marks in the ini) and also forgets the last letter/number in the INI, e.g.;

Code:
[ledu]
ledu="hello 123"

would display as;

hello 12

in the right text box

Any help please ^^
 
Last edited:

LeoCrasher

Former Administrator
VIP
Mar 23, 2003
2,001
4
235
::1
Well the code you've shown us is useless without the sub/function for reading the INI (which is I think the problem lies). Either post your INI reading function here, or change it using other source.

/Leo
 

WiseWarrior

LOMCN Veteran
Veteran
Loyal Member
Nov 16, 2003
327
0
62
14 NeverNeverLand
Form2 (Config page)
Code:
Private Sub cmdBack_Click()

Form1.Show
Unload Me

End Sub

Private Sub cmdExit_Click()

End

End Sub

Private Sub cmdRead_Click()
Dim File As String, OFLen As Double, Str(6) As String

'Open INI
    File = App.Path & "\LOM2.ini"
    OFLen = FileLen(File)

    Str(0) = ReadIni(File, "ledu", "ledu")
    Str(1) = ReadIni(File, "db", "db_svr")
    Str(2) = ReadIni(File, "game", "game_gate")
    Str(3) = ReadIni(File, "login1", "login_gate")
    Str(4) = ReadIni(File, "login2", "login_svr")
    Str(5) = ReadIni(File, "select", "sel_gate")
    Str(6) = ReadIni(File, "m2", "m2_svr")
    
    For i = 0 To 6
        txtPaths(i) = Str(i)
    Next i
    
End Sub

Private Sub cmdWrite_Click()
Dim File As String, OFLen As Double, Str(6) As String

'Open INI
    File = App.Path & "\LOM2.ini"
    OFLen = FileLen(File)
    
    For i = 0 To 6
        Str(i) = txtPaths(i)
    Next i

WriteIni File, "ledu", "ledu", Str(0)
WriteIni File, "db", "db_svr", Str(1)
WriteIni File, "game", "game_gate", Str(2)
WriteIni File, "login1", "login_gate", Str(3)
WriteIni File, "login2", "login_svr", Str(4)
WriteIni File, "select", "sel_gate", Str(5)
WriteIni File, "m2", "m2_svr", Str(6)

MsgBox ("Configuration Written to " & File)

End Sub

Modual
Code:
'declarations for working with Ini files
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
    "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As String) As Long

Private 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

Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
    "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
    ByVal lpFileName As String) As Long

Private 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
    
'// INI CONTROLLING PROCEDURES
'reads an Ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
  v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
  ReadIni = Left(RetVal, v - 1)
End Function

'reads an Ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
  v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
  ReadIniSection = Left(RetVal, v - 1)
End Function

'writes an Ini string
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
  WritePrivateProfileString Section, Key, Value, Filename
End Sub

'writes an Ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
  WritePrivateProfileSection Section, Value, Filename
End Sub

That more help?
 

WiseWarrior

LOMCN Veteran
Veteran
Loyal Member
Nov 16, 2003
327
0
62
14 NeverNeverLand
hehe not worried about it anymore, as its only forgetting the last number or letter ive figure a way to cheat it

when I write the paths to the INI file i just add a letter or number to the end of the path and when the program reads it back, it forgets the last digit so it loads the correct path ^^

WriteIni File, "ledu", "ledu", Str(0) & "0"

Thanks anyway leo ^^
 

Dataforce

LOMCN VIP
VIP
Apr 15, 2003
2,080
0
283
thats a lame hack, dont do that.

and wtf kinda code is that?

" ReadIni = Left(RetVal, v - 1)" << i believe that line is part of your problem.

However, your probably best using some decent routines.

Some non-api ones here:
http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=9272&lngWId=1

(Non API doesn't have the file size limit)

I did have some i coded myself at one point, but having moved away from VB i cba to find them from my backups.