Results 1 to 3 of 3

Thread: Help wit format problem please

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    43

    Help wit format problem please

    I'm totally at a loss with this code:

    I need to store 25KG N (the N after the space to indicate Nett weight) or
    I need to store 25KG G (the G after the space to indicate Gross weight)

    However a calculation should be made in dbgrid1 so I am trying to strip off the alpha characters leaving only 1 to 9 (ascii 48 to 57)
    and write that to DBGrid1.Columns("Max")

    'Leave 0 to 9
    Dim strMax As String
    Dim strNet As String
    Dim lngIndex As Long
    Dim netIndex As Long
    strMax = DBGrid1.Columns("Mass").Text
    For lngIndex = Len(strMax) To 1 Step -1
    If (Asc(Mid$(strMax, lngIndex, 1)) >= 48 And Asc(Mid$(strMax, lngIndex, 1)) <= 57) Then
    ' it's OK
    Else
    strMax = Replace(strMax, Mid$(strMax, lngIndex, 1), "")
    End If
    Next
    DBGrid1.Columns("Max").Text = strMax


    THIS works fine if the data in DBGrid1.Columns("Mass").Text is 25KG or 25kg or 25Kg (Upper or lower case)

    Now THE PROBLEM AS soon as a space is in the string

    I need to store 25KG N (the N after the space to indicate Nett weight) or
    I need to store 25KG G (the G after the space to indicate Gross weight)

    if I run the code
    25KG N returns correctly as 25 (all with anything accept the second G is OK)

    But
    25KG G returns error Invalid procedure call or argument

    what is funny is that sometimes it returns OK and other combinations not

    25kg G or 25KG g or 25kg G returns correctly as 25

    AND

    25KG G or 25kG G or 25kg g or 25 Kg G returns error Invalid procedure call or argument
    (Must be the second G?)

  2. #2
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Help wit format problem please

    VB Code:
    1. MsgBox Val("25kg N")

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help wit format problem please

    You really don't need to "strip" anything. Example:
    VB Code:
    1. Dim sTest As String
    2. Dim iNumber As Integer
    3.  
    4. sTest = "25KG N"
    5. iNumber = Val(sTest) 'iNumber now equals 25
    So in sTest you still have your original string which can be stored. In iNumber you have the number upon which you can do you calculations.

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