Results 1 to 10 of 10

Thread: Need ur advise

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Question Need ur advise

    Need your advise badly. Below code doesn't work. Any idea where I can resolve this??Any help is greatly appreciated


    Private Sub cmdValidate_Click()

    Const ForReading = 1, ForWriting = 2
    Dim RawdataStr As String
    Dim RawStream
    Dim i
    Dim CAT(1 To 10) As Integer
    Dim AllTotal As Integer


    Open "a:\text.txt" For Input As #1
    Do Until EOF(1)
    DoEvents
    Line Input #1, RawdataStr
    If InStr(RawdataStr, " 774 ") = 0 Then
    'If 774 found on a line

    '''''''''''How to make the below part simpler???''''''''''''''''''''''
    CAT1 = Mid(RawdataStr, 25, 4) 'eg output = 0101
    CAT2 = Mid(RawdataStr, 29, 4) '0000
    CAT3 = Mid(RawdataStr, 33, 4) '1516
    CAT4 = Mid(RawdataStr, 37, 4) '1771
    CAT5 = Mid(RawdataStr, 41, 4) '9009
    CAT6 = Mid(RawdataStr, 45, 4) '1111
    CAT7 = Mid(RawdataStr, 49, 4) '1122
    CAT8 = Mid(RawdataStr, 53, 4) '0000
    CAT9 = Mid(RawdataStr, 57, 4) '0000
    CAT10 = Mid(RawdataStr, 61, 4) '7878

    ''''''''''How to add all value from CAT1 to CAT 10??''''''''''''''''''

    For i = 1 To 10
    AllTotal = AllTotal + CAT(i)
    Next i
    MsgBox AllTotal & ": is the total amount"

    End

    End If
    Loop
    Close #1

    End Sub

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Posts
    17
    What doesn't work, do you receive an error, what error ?
    in the line If InStr(RawdataStr, " 774 ") = 0 Then is a space in front of 774, is this correct ?
    Nobody is perfect. Hi !! My name is nobody...

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: Need ur advise

    Originally posted by skyseh
    If InStr(RawdataStr, " 774 ") = 0 Then
    'If 774 found on a line

    Haven't tested your code or anything, but the above line isn't true.
    If InStr(...) = 0, that means that "774" was not found on a line.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Need ur advise

    Originally posted by skyseh


    Dim intStart As Integer, intIndex As Integer
    '''''''''''Here's how to make the below part simpler''''''''''''''''''''''
    ' First, change CATx to an array i.e. Dim Cat(1 To 10) As String
    intIndex = 1
    For intStart = 25 To 61 Step 4
    Cat(intIndex) = Mid$(RawdataStr, intStart, 4)
    AllTotal = AllTotal + Cat(intIndex)

    intIndex = intIndex + 1
    Next

    MsgBox AllTotal & ": is the total amount"


    'Etc etc

  5. #5
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    You could make your own type and fill it with a line of the text file

    Like plenderj mentioned it isn't found when result is 0

    You should change it to
    VB Code:
    1. If InStr(RawdataStr, " 774 ") <> 0 Then
    2. 'then it is found

    But your ending your program to wiht the End statement

    If you want only to count the total of all the lines where 774 is included then you can rewrite it to
    VB Code:
    1. Private Sub cmdValidate_Click()
    2.  
    3. Const ForReading = 1, ForWriting = 2
    4. Dim RawdataStr As String
    5. Dim RawStream
    6. Dim AllTotal As Integer
    7.  
    8. 'initialize all total
    9. AllTotal = 0
    10. ' SURE IT WILL NOT EXCEED AN INTEGER VALUE ???
    11. Open "a:\text.txt" For Input As #1
    12. Do Until EOF(1)
    13.    DoEvents
    14.    Line Input #1, RawdataStr
    15.    If InStr(RawdataStr, " 774 ") = 0 Then
    16.    'If 774 found on a line
    17.  
    18.       'add to all total the values
    19.       AllTotal = AllTotal + Mid(RawdataStr, 25, 4) 'eg output = 0101
    20.       AllTotal = AllTotal + Mid(RawdataStr, 29, 4) '0000
    21.       AllTotal = AllTotal + Mid(RawdataStr, 33, 4) '1516
    22.       AllTotal = AllTotal + Mid(RawdataStr, 37, 4) '1771
    23.       AllTotal = AllTotal + Mid(RawdataStr, 41, 4) '9009
    24.       AllTotal = AllTotal + Mid(RawdataStr, 45, 4) '1111
    25.       AllTotal = AllTotal + Mid(RawdataStr, 49, 4) '1122
    26.       AllTotal = AllTotal + Mid(RawdataStr, 53, 4) '0000
    27.       AllTotal = AllTotal + Mid(RawdataStr, 57, 4) '0000
    28.       AllTotal = AllTotal + Mid(RawdataStr, 61, 4) '7878
    29.  
    30.       MsgBox AllTotal & ": is the total amount"
    31.  
    32.       ' commented the End part , this stops your program
    33.       ' End
    34.      
    35.      'or use action_sa his method, replying same time
    36.      'For intStart = 25 To 61 Step 4
    37.      '   AllTotal = AllTotal + Mid$(RawdataStr, intStart, 4)
    38.      'Next
    39.  
    40.  
    41.    End If
    42. Loop
    43. Close #1
    44.  
    45. End Sub
    Last edited by swatty; Nov 7th, 2002 at 04:38 AM.
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    statement

    Well I knew
    If InStr(RawdataStr, " 774 ") = 0 Then
    ..
    is a mistake. Surprisingly, it only execute the if statement when I omit the Not which is in front of InStr(as mentioned by plenderj or put an else...
    I wonder why too...

  7. #7
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    Did you read all reply's, and have you still got problems ??
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  8. #8
    Junior Member
    Join Date
    Nov 2002
    Posts
    17
    I guess not...
    Nobody is perfect. Hi !! My name is nobody...

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Wink

    Yea I have read and tried axion_sa's...thanks so much axion_sa for sharing and giving me such a valuable tip. Next i shall try on swatty's. thanks thanks for all helps

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Posts
    69

    Close to end

    I am close to completing my program....btw I would appreciate if any of you can solve my last problem..

    The loop will continue to search for the keyword "774".
    For the first found "774" my program will perform some calculations.
    What if the textfile contain more than one "774" which exist in some other line in the same file? I want my program to execute the same thing as the first.
    Thanks!...I am using axion_sa algo

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