none
Categorizing using File Handling techniques RRS feed

  • Question

  • Hello,

    I do hope that I am in the right forum this time.

    Recently, I have been working on a few independent projects.

    The long-term goal is to eliminate any need for data entry by using file handling techniques.

    Here is the source code:

    Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ(3), StaticArr(3) As String

    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100

    For Jp = 0 To 3
        Line Input #100, strZ(Jp)
    Next Jp

    For Jp = 0 To 3
    If strZ(Jp) = "Male" Then
        OutputMaleNames StAddX:=StaticArr
    End If

    If strZ(Jp) = "Female" Then
        OutputFemaleNames StArr:=StaticArr
    End If

    Next Jp
       

    End Sub


    Sub OutputFemaleNames(ByRef StArr() As String)

    Dim Hp As Long

    Open "C:\Users\John\Documents\Female Names.txt" For Output As #101
    For Hp = LBound(Hp) To UBound(Hp)
        If InStr(1, StArr(Hp), "Female", vbTextCompare) > 0 Then
             Write #101, StArr(Hp)
        End If
    Next Hp
    Close #101



    End Sub
    Sub OutputMaleNames(ByRef StAddX() As String)
    Dim Hp As Long

    Open "C:\Users\John\Documents\Male Names.txt" For Output As #102
    For Hp = LBound(Hp) To UBound(Hp)
        If InStr(1, StAddX(Hp), "Male", vbTextCompare) > 0 Then
             Write #102, StAddX(Hp)
        End If
    Next Hp
    Close #102


    I do hope that I do get the most views, which would lead to the most replies.

    This code snippet runs fine, but no output in any of the two files that are created.

    I have tried passing and returning parameters just to generate the output at runtime.

    This time by using the ByRef reserved keywords as referenced parameters to the two called routines.

    Can anyone figure this out, so that it can produce the output in two Notepad files, which are categorized?

    Regards,

    JohnDBCTX

    Thursday, June 23, 2016 3:48 AM

Answers

  • I have redone the entire code snippet below:

    Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ(2) As String
    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
    Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
    Open "C:\Users\John\Documents\Female Names.txt" For Output As #102

    For Jp = 0 To 2
        Line Input #100, strZ(Jp)
    Next Jp
    For Jp = 0 To 2
        Select Case InStr(1, strZ(Jp), "Male", vbTextCompare)
            Case 0:
                Write #101, strZ(Jp)
        End Select
        Select Case InStr(1, strZ(Jp), "Female", vbTextCompare)
            Case Is > 0:
                Write #102, strZ(Jp)
            Case Else
                Write #101, strZ(Jp)
        End Select
    Next Jp
    Close #102, 101, 100

    End Sub

    The output in Female Names.txt:

    "3, Jill, Female"

    And the output in Male Names.txt:

    "1, John, Male"

    "2, Jack, Male"

    Therefore; this should eliminate the need for data entry.

    I've finally did it. Thanks to myself.

    I do hope this shall prove others that I can solve problems by myself.

    JohnDBCTX


    • Edited by JohnDBCTX Thursday, June 23, 2016 9:06 AM Problem Solved
    • Marked as answer by JohnDBCTX Thursday, June 23, 2016 9:07 AM
    Thursday, June 23, 2016 8:50 AM
  • I have redone the entire code snippet below:

    Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ(2) As String
    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
    Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
    Open "C:\Users\John\Documents\Female Names.txt" For Output As #102

    For Jp = 0 To 2
        Line Input #100, strZ(Jp)
    Next Jp
    For Jp = 0 To 2
        Select Case InStr(1, strZ(Jp), "Male", vbTextCompare)
            Case 0:
                Write #101, strZ(Jp)
        End Select
        Select Case InStr(1, strZ(Jp), "Female", vbTextCompare)
            Case Is > 0:
                Write #102, strZ(Jp)
            Case Else
                Write #101, strZ(Jp)
        End Select
    Next Jp
    Close #102, 101, 100

    End Sub

    The output in Female Names.txt:

    "3, Jill, Female"

    And the output in Male Names.txt:

    "1, John, Male"

    "2, Jack, Male"

    Therefore; this should eliminate the need for data entry.

    I've finally did it. Thanks to myself.

    I do hope this shall prove others that I can solve problems by myself.

    JohnDBCTX



    Someone spread the word.
    • Marked as answer by JohnDBCTX Thursday, June 23, 2016 9:11 AM
    Thursday, June 23, 2016 9:10 AM
  • Sub CatagorizeNamesByGender()
        Dim strZ As String

        Open "C:\Users\John\Documents\List of Names.csv" For Input As #100
        Open "C:\Users\John\Documents\Male Names.csv" For Output As #101
        Open "C:\Users\John\Documents\Female Names.csv" For Output As #102

       

        While Not EOF(100)
            Line Input #100, strZ
        Select Case InStr(1, strZ, "M", vbTextCompare)
        Case Is > 0:
            Write #101, strZ
        End Select
        Select Case InStr(1, strZ, "F", vbTextCompare)
        Case Is > 0:
            Write #102, strZ
        End Select
        Wend

    Close #102, 101, 100

    End Sub

    A Correction:

    Since the text is not case-sensitive, I had to change Male to M and Female to F.

    This time, it finally worked after I resaved the List of names.csv

    From now on, whenever I need to perform file handling techniques, I would need to open and close them

    as CSV(Comma Separated Value) files.

    Does that finally make sense?

    Regards,

    JohnDBCTX

    • Marked as answer by JohnDBCTX Friday, June 24, 2016 3:02 AM
    Friday, June 24, 2016 3:02 AM

All replies

  • See http://www.eileenslounge.com/viewtopic.php?f=30&t=23856

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, June 23, 2016 5:34 AM
  • I have redone the entire code snippet below:

    Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ(2) As String
    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
    Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
    Open "C:\Users\John\Documents\Female Names.txt" For Output As #102

    For Jp = 0 To 2
        Line Input #100, strZ(Jp)
    Next Jp
    For Jp = 0 To 2
        Select Case InStr(1, strZ(Jp), "Male", vbTextCompare)
            Case 0:
                Write #101, strZ(Jp)
        End Select
        Select Case InStr(1, strZ(Jp), "Female", vbTextCompare)
            Case Is > 0:
                Write #102, strZ(Jp)
            Case Else
                Write #101, strZ(Jp)
        End Select
    Next Jp
    Close #102, 101, 100

    End Sub

    The output in Female Names.txt:

    "3, Jill, Female"

    And the output in Male Names.txt:

    "1, John, Male"

    "2, Jack, Male"

    Therefore; this should eliminate the need for data entry.

    I've finally did it. Thanks to myself.

    I do hope this shall prove others that I can solve problems by myself.

    JohnDBCTX


    • Edited by JohnDBCTX Thursday, June 23, 2016 9:06 AM Problem Solved
    • Marked as answer by JohnDBCTX Thursday, June 23, 2016 9:07 AM
    Thursday, June 23, 2016 8:50 AM
  • I have redone the entire code snippet below:

    Sub CatagorizeNamesByGender()
    Dim Jp As Long
    Dim strZ(2) As String
    Open "C:\Users\John\Documents\List of Names.txt" For Input As #100
    Open "C:\Users\John\Documents\Male Names.txt" For Output As #101
    Open "C:\Users\John\Documents\Female Names.txt" For Output As #102

    For Jp = 0 To 2
        Line Input #100, strZ(Jp)
    Next Jp
    For Jp = 0 To 2
        Select Case InStr(1, strZ(Jp), "Male", vbTextCompare)
            Case 0:
                Write #101, strZ(Jp)
        End Select
        Select Case InStr(1, strZ(Jp), "Female", vbTextCompare)
            Case Is > 0:
                Write #102, strZ(Jp)
            Case Else
                Write #101, strZ(Jp)
        End Select
    Next Jp
    Close #102, 101, 100

    End Sub

    The output in Female Names.txt:

    "3, Jill, Female"

    And the output in Male Names.txt:

    "1, John, Male"

    "2, Jack, Male"

    Therefore; this should eliminate the need for data entry.

    I've finally did it. Thanks to myself.

    I do hope this shall prove others that I can solve problems by myself.

    JohnDBCTX



    Someone spread the word.
    • Marked as answer by JohnDBCTX Thursday, June 23, 2016 9:11 AM
    Thursday, June 23, 2016 9:10 AM
  • Sub CatagorizeNamesByGender()
        Dim strZ As String

        Open "C:\Users\John\Documents\List of Names.csv" For Input As #100
        Open "C:\Users\John\Documents\Male Names.csv" For Output As #101
        Open "C:\Users\John\Documents\Female Names.csv" For Output As #102

       

        While Not EOF(100)
            Line Input #100, strZ
        Select Case InStr(1, strZ, "M", vbTextCompare)
        Case Is > 0:
            Write #101, strZ
        End Select
        Select Case InStr(1, strZ, "F", vbTextCompare)
        Case Is > 0:
            Write #102, strZ
        End Select
        Wend

    Close #102, 101, 100

    End Sub

    A Correction:

    Since the text is not case-sensitive, I had to change Male to M and Female to F.

    This time, it finally worked after I resaved the List of names.csv

    From now on, whenever I need to perform file handling techniques, I would need to open and close them

    as CSV(Comma Separated Value) files.

    Does that finally make sense?

    Regards,

    JohnDBCTX

    • Marked as answer by JohnDBCTX Friday, June 24, 2016 3:02 AM
    Friday, June 24, 2016 3:02 AM
  • Here is the entire list:
       1, John, Male
     2, Jack, Male
     3, Jill,  Female
        While Not EOF(100)
            Line Input #100, strZ
            If InStr(1, strZ, "Male", vbTextCompare) Then
                Write #102, strZ
            End If
        Wend

    Retruns the output:   

       1, John, Male
       2, Jack, Male
       3, Jill,  Female


    Whereas:
        While Not EOF(100)
            Line Input #100, strZ
            If InStr(1, strZ, "Female", vbTextCompare) Then
                Write #102, strZ
            End If
        Wend
    Returns the output: 3, Jill, Female
    How can I fix this source code snippet so that it can only produce
    the output:

    1, John, Male
    2, Jack, Male

    JohnDBCTX


    • Edited by JohnDBCTX Saturday, June 25, 2016 12:46 AM Refine code
    Saturday, June 25, 2016 12:44 AM
  • You could use

        Do While Not EOF(100)
            Line Input #100, strZ
            If InStr(1, strZ, "Female", vbTextCompare) Then
                Write #102, strZ
            ElseIf InStr(1, strZ, "Male", vbTextCompare) Then
                Write #101, strZ
            End If
        Loop


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Saturday, June 25, 2016 7:52 AM
  • I have changed it from vbTextCompare to vbBinaryCompare and it finally worked.

    Just the way I wanted it to be.

    JohnDBCTX

    Saturday, June 25, 2016 8:35 AM