eneate
asked on
Checking the value of an empty string
Hi
I am reading a value from a text file, I need to check of the text exists or is empty, I have been using the following
Dim buffer As String
Dim datastring As String
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim x As String
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t bwinpos", "x", vbNullString, buffer, Len(buffer), filename)
datastring = StripChr(buffer)
x = buffer 'buffer contains the text from the ini file
If x = " " Then
x = 300
Call WritePrivateProfileString( "tbwinpos" , "x", x, filename)
tbcheck1_x = True
Else
tbcheck1_x = True
End If
End If
I think the software is seeing spaces or something as it doesn't see the string as empty, I think I need to use trim or something. Can anyone help
I am reading a value from a text file, I need to check of the text exists or is empty, I have been using the following
Dim buffer As String
Dim datastring As String
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim x As String
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t
datastring = StripChr(buffer)
x = buffer 'buffer contains the text from the ini file
If x = " " Then
x = 300
Call WritePrivateProfileString(
tbcheck1_x = True
Else
tbcheck1_x = True
End If
End If
I think the software is seeing spaces or something as it doesn't see the string as empty, I think I need to use trim or something. Can anyone help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry that would be:
x = replace(x,"0","")
x = replace(x,"0","")
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks,
But that stil returns a 0, the only way it seams to work is if I use the following
Dim buffer As String
Dim datastring As String
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim y As String
Dim tempstring As String * 2
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t bwinpos", "y", vbNullString, buffer, Len(buffer), filename)
datastring = StripChr(buffer)
y = buffer 'buffer contains the text from the ini file
tempstring = Trim(y)
tempstring = (Asc(Left(tempstring, 1)))
Debug.Print tempstring '(Asc(Left(tempstring, 1)))
If tempstring = 0 Then
y = 300
Call WritePrivateProfileString( "tbwinpos" , "y", y, filename)
tbcheck1_y = True
Else
tbcheck1_y = True
End If
End If
End Function
any ideas why and will this be stable long term or is that a debatable question!!!!!!!!!!!!!!!!!! !!
But that stil returns a 0, the only way it seams to work is if I use the following
Dim buffer As String
Dim datastring As String
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim y As String
Dim tempstring As String * 2
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t
datastring = StripChr(buffer)
y = buffer 'buffer contains the text from the ini file
tempstring = Trim(y)
tempstring = (Asc(Left(tempstring, 1)))
Debug.Print tempstring '(Asc(Left(tempstring, 1)))
If tempstring = 0 Then
y = 300
Call WritePrivateProfileString(
tbcheck1_y = True
Else
tbcheck1_y = True
End If
End If
End Function
any ideas why and will this be stable long term or is that a debatable question!!!!!!!!!!!!!!!!!!
@ alainbryden
<Thanks the offending chartacter is 0 how would I remove this as well>
I understood this to mean "how can I remove '0' from the x string"
My mistake...
<Thanks the offending chartacter is 0 how would I remove this as well>
I understood this to mean "how can I remove '0' from the x string"
My mistake...
No worry leclairm. Eneate I think you missed my post as you were posting, but the problem is infact that you are returning a null string so the code should be the following:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim buffer As String
Dim datastring As String
Dim y As String
Dim tempstring As String * 2
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t bwinpos", "y", vbNullString, buffer, Len(buffer), filename)
datastring = StripChr(buffer)
y = buffer
If buffer = vbNullString Then
y = 300
Call WritePrivateProfileString( "tbwinpos" , "y", y, filename)
tbcheck1_y = True
Else
tbcheck1_y = True
End If
End If
End Function
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
I don't understand why you would set y = 300 though if y is a string. Perhaps this is an error?
--------------------------
Const filename As String = "C:\Program Files\dcs-3\tools.ini"
Dim buffer As String
Dim datastring As String
Dim y As String
Dim tempstring As String * 2
If FileExists(filename) Then
buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("t
datastring = StripChr(buffer)
y = buffer
If buffer = vbNullString Then
y = 300
Call WritePrivateProfileString(
tbcheck1_y = True
Else
tbcheck1_y = True
End If
End If
End Function
--------------------------
I don't understand why you would set y = 300 though if y is a string. Perhaps this is an error?
ASKER
Hi
Thanks for that, y is a string but the value relastes to the coordintes of where the form opens, this is for if the ini file corrupts in any way a default position for the form is load at 300 x by 300 y.
Thanks for that, y is a string but the value relastes to the coordintes of where the form opens, this is for if the ini file corrupts in any way a default position for the form is load at 300 x by 300 y.
Oh I see. Well good luck with everything.
ASKER
Hi
I have tried that but for some reason it doesn't work either, I tried changing the if buffer = vbnullstring to if y =
but the code I used above does work, no idea why???
I have tried that but for some reason it doesn't work either, I tried changing the if buffer = vbnullstring to if y =
but the code I used above does work, no idea why???
ASKER
Just to say, thanks to all for the feedback, finding what the character was helped the most the points reflect that. I got there eventually.
oh I see, in that case you aren't finding the vbnullstring, but simply the vbnull. They are seperate format nulls, both with a value of 0 when converted to integer.
try if y = vbNull.
try if y = vbNull.
ASKER
Thanks the offending chartacter is 0 how would I remove this as well