Solved

Checking the value of an empty string

Posted on 2006-07-18
13
195 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

785 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