Consulting

Results 1 to 8 of 8

Thread: Need to know if a file is corrupt

  1. #1
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location

    Need to know if a file is corrupt

    I have a csvfile where the user saves his settings for the macro.

    The settings are

    Altitude: Meter or Feet
    Velocity: Km/h, M/s, Mph or feet/s

    I can read and write to the file, but i cant figure out how to know if its corrupt or not.
    The two settings are V(0) and V(1)

    [VBA]
    If V(0) <> "Meter" Or V(0) <> "Feet" Then
    MsgBox "The Flysight settings file is corrupt, please delete it and open this file again"
    Exit Sub
    End If
    If V(1) <> "Km/h" Or V(1) <> "M/s" Or V(1) <> "Mph" Or V(1) <> "Feet/s" Then
    MsgBox "The Flysight settings file is corrupt, please delete it and open this file again"
    Exit Sub
    End If
    [/VBA]

    The first if works (Meter/Feet) but V(1) does not.
    When i hover the mouse on V(1) i see the value is "Km/h", if i add a watch and look at the value there it says "Km/h ".
    What is that <Space>? A normal space does not work.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Perhaps it is Chr(160)?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    No, that did not work either.

    I tried to make a Case but i cant get that working either

    [VBA] Select Case V(0)
    Case Not "Meter", "Feet"
    Exit Sub
    End Select
    Select Case V(1)
    Case Not "Km/h", "M/s", "Mph", "feet/s"
    Exit Sub
    End Select[/VBA]

    I get type mismatch with the above code, if i remove the "" it seems to read them as variables

    Is there any way i can get this to work?

    I found out that if
    [VBA]str = Left(V(1), 4)[/VBA]
    str would contain only "Km/h" but it seems my statement is flawed anyway.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What exactly did not work, I just suggested what it might be, what did you do/try?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Also, your Case logic seems flawed to me. Maybe try

    [vba]

    Select Case V(0)
    Case "Meter", "Feet"
    Case Else
    Exit Sub
    End Select
    Select Case V(1)
    Case "Km/h", "M/s", "Mph", "feet/s"
    Case Else
    Exit Sub
    End Select
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    I think i got it working now, and i think i know why it didnt work.
    It seems when the file is created it creates a new line and some other sign that is not visible.
    By removing the two last characters it woks.
    [VBA] V(1) = Left(V(1), Len(V(1)) - 2)
    [/VBA]


    So you cant use Case not? ok, i didnt know that.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, and even if you could, it would not make sense not for multiple entries. It is a bit like saying

    [vba]

    If x <> "a" And x <> "b2" Then
    [/vba]

    that will always be True, as VBA doesnot short-circuit.

    Can you post a workbook, I still suspect spaces?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    This blank character can be Chr(0) it had once given me nightmares. The thread may not be exactly useful. But you may see the posts #11 to #13 specifically.
    http://www.vbaexpress.com/forum/showthread.php?t=32670
    Hope this helps
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •