?
Solved

Checking the value of an empty string

Posted on 2006-07-18
13
Medium Priority
?
200 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:eneate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 1200 total points
ID: 17129205
I would use if trim(x) = "" then

If that does not work - do a debug.print (asc(left(x, 1)) wich will give you the offending character number,
Then exclude that as well
0
 

Author Comment

by:eneate
ID: 17129230

Thanks the offending chartacter is 0 how would I remove this as well
0
 
LVL 11

Assisted Solution

by:leclairm
leclairm earned 400 total points
ID: 17129973
If you need to remove a string, you can use something like:

replace(x,"0","")
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:leclairm
ID: 17129975
Sorry that would be:

x = replace(x,"0","")
0
 
LVL 20

Assisted Solution

by:alainbryden
alainbryden earned 400 total points
ID: 17130166
if the offending character number is zero, then it's a nullstring leclairm :P

check for null strings with if x = vbNullString
you could also check if len(x) = 0 (zero length string)
If, as you seem to have indicated with " " there is a potential for the empty string to contain a space, then check if x = " " but I'm not sure that's what you intended.
I think this whole problem stems from the fact that you were checking if x = "_" (with a space in between) which actually is a string, not a null string at all. If you correct  this statement with one of the first two I mentioned, I'm sure you will find that it works as it should.
0
 

Author Comment

by:eneate
ID: 17130215
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!!!!!!!!!!!!!!!!!!!!
0
 
LVL 11

Expert Comment

by:leclairm
ID: 17130239
@ 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...



0
 
LVL 20

Expert Comment

by:alainbryden
ID: 17130327
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?
0
 

Author Comment

by:eneate
ID: 17130521
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.
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 17130535
Oh I see. Well good luck with everything.
0
 

Author Comment

by:eneate
ID: 17130585
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???
0
 

Author Comment

by:eneate
ID: 17130643
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.
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 17131587
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question