Solved

Checking the value of an empty string

Posted on 2006-07-18
13
193 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
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 300 total points
Comment Utility
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
Comment Utility

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
Comment Utility
If you need to remove a string, you can use something like:

replace(x,"0","")
0
 
LVL 11

Expert Comment

by:leclairm
Comment Utility
Sorry that would be:

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

Assisted Solution

by:alainbryden
alainbryden earned 100 total points
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 11

Expert Comment

by:leclairm
Comment Utility
@ 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
Comment Utility
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
Comment Utility
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
Comment Utility
Oh I see. Well good luck with everything.
0
 

Author Comment

by:eneate
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now