I am pulling info from a .ini file and the value in a variable looks like this:
VALUE||||||||||||||
What are those | things and can I just do a replace | with nothing?
Thanks.
Printable View
I am pulling info from a .ini file and the value in a variable looks like this:
VALUE||||||||||||||
What are those | things and can I just do a replace | with nothing?
Thanks.
Trim it
They are junk, and Trim should remove them
Already doing this and not triming it off.
Code:
gsLocation = Trim(Temp)
It's Ascii 124 - a pipe sometimes called 'logical or' symbol
It's on the Keyboard, usually on the \ (backslash) key
Code:mystr = "VALUE||||||"
mystr=Replace(mystr,chr124),"")
That's Chr(124)
PS: are you using GetPrivateProfileString or some other api to read the INI file? That works better.
The following doesn't work either:
gsLocation = Replace(Trim(Temp), Chr(124), "")
HERE IS THE WHOLE SECTION OF CODE:
Dim ret As Long
Dim Temp As String * 50
Dim sLoc As String
sLoc = "c:\windows\File.ini"
ret = GetPrivateProfileString("Location", "FullPath", sLoc, Temp, Len(Temp), sLoc)
If ret = 0 Then
Beep
Else
gsLocation = Replace(Trim(Temp), Chr(124), "")
End If
.....
real quick do this
msgbox Right(gsLocation,1)
tell me the messagebox says
I give up. Something else is going on. Replace works just fine when I try it on my machine here.
after setting gsLocation of course
Cander - nothing appears so I am going to say it's a space maybe or could it be null?
a null .
here try this
Code:Do Until Asc(Right(Temp, 1)) <> 0
Temp = Left(Temp, Len(strName) - 1)
DoEvents
Loop
gsLocation = Temp
oops change strName to Temp
Cander - That didn't work either.
arggh. well im tapped out of ideas
Join the club. He's doing something else somewhere or he's working with hosed data.
Oh. wait. Hosed data - he isn't init - ing the temp string
See if that doesn't helpCode:Dim Temp as String *50
'add
Temp = string(chr(0), 50) ' initilaize it to all chr(0)
....
After you get a RETURN
Temp = left(temp,instr(temp,chr(0)-1)
Give me a type mismatch error on this line.
Temp = string(chr(0), 50) ' initilaize it to all chr(0)
Do you need to use a fixed length string?
That is causing all your problems
Yeah, I hear you on that one. I tried without and it doesn't like that for some reason.
Here is where I got info on it:
http://www.vb-world.net/files/tip17.html
I inverted the arguments. If you can code you can fix it, too.
:D
Jeez Louise.
This is why you have the trailing crud. GetPRivateProfileString uses null-terminated string because it is written in CCode:Temp = string(50,vbNull) ' it to all chr(0)
This is what I always use and it works without problems.
VB Code:
'Add this stuff to a module 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 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 Sub ReadIniFile(SectorName As String, KeyName As String, Inifile As String) 'Param1 'The header of the INI file section the value is in. 'Param2 'The name of the value to read. 'Param3 'The value to return if a valid value cannot be read. Make it something that would definitely not be read, such as "(error)". 'Param4 'A fixed-length string that will receive either the string read from the file or lpDefault. 'Param5 'The length in characters of lpReturnedString. 'Param6 'The filename of the INI file to read from Dim uname As String ' receives the value read from the INI file Dim slength As Long ' receives length of the returned string uname = Space(255) ' provide enough room for the function to put the value into the buffer slength = GetPrivateProfileString(SectorName, KeyName, "(error)", uname, 255, Inifile) RetString = Left(uname, slength) ' extract the returned string from the buffer End Sub Sub WriteIniFile(ByVal SectorName As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal Inifile As String) 'Param1 'The section of the INI file to write to. 'Param2 'The name of the value to set. 'Param3 'The string to set as the value. 'Param4 'The filename of the INI file to write to. Dim Retval As Long ' return value ' Set the string value. Retval = WritePrivateProfileString(SectorName, KeyName, KeyValue, Inifile) End Sub 'Then to use it in a form add this code Dim retString as String Call ReadINIFile("MyApp", "DataSource", iniFile) 'where MyApp is the category, DataSource is the key, 'and inifile is the full path and file name of your inifile 'Then check to see what was returned If retString = "(error)" then 'The key wasnt found in the INI file 'so you could write a default value here if u wanted Call WriteIniFile("MyApp", "DataSource", "C:\Temp", iniFile) Else 'The value was found msgBox retString End if
Here is some code I found here at vb-world that works great.
Function TrimNull(sNullString As String) As String
'=========================================================
'=========================================================
Dim iLen As Integer
Dim iTemp As Integer
Dim sTemp As String
iLen = Len(sNullString)
iTemp = InStr(sNullString, Chr$(0))
If iTemp <> 0 Then
sTemp = Left$(sNullString, iTemp - 1)
sNullString = sTemp
End If
TrimNull = sNullString
End Function