Solved

Checking the value of an empty string

Posted on 2006-07-18
13
197 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 300 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 100 total points
ID: 17129973
If you need to remove a string, you can use something like:

replace(x,"0","")
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 100 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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 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