Link to home
Start Free TrialLog in
Avatar of eneate
eneateFlag for United Kingdom of Great Britain and Northern Ireland

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("tbwinpos", "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
ASKER CERTIFIED SOLUTION
Avatar of Raynard7
Raynard7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eneate

ASKER


Thanks the offending chartacter is 0 how would I remove this as well
SOLUTION
Avatar of leclairm
leclairm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leclairm
leclairm

Sorry that would be:

x = replace(x,"0","")
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eneate

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("tbwinpos", "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!!!!!!!!!!!!!!!!!!!!
@ 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...



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("tbwinpos", "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?
Avatar of eneate

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.
Oh I see. Well good luck with everything.
Avatar of eneate

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???
Avatar of eneate

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.