Results 1 to 5 of 5

Thread: CLng causes error....sometimes!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Check out the following piece of code.

    Private Sub Form_Load()
    Dim V1 As String
    V1 = "£0.00"
    MsgBox "about to do clng of v1"
    If CLng(V1) = 0 Then
    MsgBox "Just did clng of v1, val = 0 "
    Else
    MsgBox "Just did clng of v1, val != 0 "
    End If
    End Sub

    On my machine, it works fine. On another, the line with CLng in it terminates the program with a `run-time error 13: type mismatch`.

    I know CLng uses your locale settings, but this is unacceptable, isnt it? Or is it my fault. I`m sure i`ve had this problem before and its `gone away`, though its not going anywhere at the moment!

    Perhaps a few of you could check this out and see what happens on your machine?


    Thanks,
    Alex.

  2. #2
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Unhappy Hmmmm

    I can't really see why it would work on your machine in the first place!?!?! I think your machine's the wierd one out of the two!

    You've started off by dimming V1 as a string value, and you've put what is essentially a string (a bunch of numbers, letters and symbols) into that variable. Fair enough. However, a long value only stores numbers (and decimal points) so converting from a string to a long value you must have only numbers (or decimal points) in the string in the first place, because VB cannot convert "£" to a number.

    One way to get round it is to strip off the first characted (ie/ the "£" sign).

    Code:
    Private Sub Form_Load()
    Dim V1 As String
    Dim V2 As String
    V1 = "£0.00"
    'save all of the characters except the first one to a
    'new variable
    V2 = Right(V1,Len(V1)-1)
    'put the value of V2 back into the original variable V1
    V1 = V2
    MsgBox "about to do clng of v1"
    If CLng(V1) = 0 Then
    MsgBox "Just did clng of v1, val = 0 "
    Else 
    MsgBox "Just did clng of v1, val != 0 " 
    End If
    End Sub
    I hope that works - I don't have VB with me so I can't check it - you'll have to do that. If it doesn't work then use common sense or tell me.

    Incidentally, have you done C++ or JavaScript before VB?

  3. #3
    Guest

    Hoi Pallex,

    The program crashes too on my system. I have the following
    regional settings:
    Currency Symbol $
    Pos. of curr. symbol *1.1
    Decimal symbol .
    No of dig. after decimal 2
    digit grouping symbol ,
    number of dig. in group 3

    The program ended oke after changing the pound to a dollar
    sign.

    I hope this helps. Let us know what you find.
    Interesting problem.

    GUUS

  4. #4
    Guest
    Shouldn't you be using CCur(V1) to do a currency conversion. That will take into account the regional settings etc. Once you've useed CCur, then you could cLng it, although that will lose the decimal places.

    - gaffa

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Yeah, shortly after i posted my plea for help i worked out it was specifically the £ character in
    Control Panel\Regional Settings\Currency\Currency symbol.

    The problem was the same whether i used CLng or CCur. Ie it depended solely on what was in the setting mentioned above, not which function i used.

    I think i would describe an app as `badly behaved` or `produces side-effects` if it set the value of this setting to what it wanted, as it obviously has system-wide implications (what if another app wanted the $ symbol), so the i guess the choice is to detect what the current setting is and complain if its not a £ symbol, or change all instaces of the CLng test to a custom subroutine.

    Thanks,
    Alex.

Posting Permissions

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



Click Here to Expand Forum to Full Width