create a text file called contacts using Notepad (programs> accessories > Notepad) with n=15
records for storing their name,sex age ,telephone and email address that resembles the below.

"Hiba", "M",20,1234567,"aaa@yahoo.com"

"ahmed", "f",21,2345678,"bbb@yahoo.com"

"ali", "M",25,3456789,"ccc@yahoo.com"
write a program to read the file display contact information.

Dani AI

Generated

A compact, practical VB6 approach for — as implied, showing code is useful, and is correct that the file is CSV. The key points: read the file line-by-line, handle quoted fields (commas inside quotes), strip an optional UTF-8 BOM that Notepad may add, and then display rows in a ListBox or grid. The example below is a minimal, self-contained CSV parser plus loader; call it from a form (for example: LoadContactsIntoListBox App.Path & "\contacts.txt", List1).

Sub LoadContactsIntoListBox(ByVal filePath As String, ByVal lst As ListBox)
    Dim fn As Integer, sLine As String, arr As Variant
    fn = FreeFile
    On Error GoTo ErrHandler
    Open filePath For Input As #fn
    Do While Not EOF(fn)
        Line Input #fn, sLine
        ' remove UTF-8 BOM if present
        If Len(sLine) >= 3 Then
            If Asc(Mid$(sLine, 1, 1)) = 239 And Asc(Mid$(sLine, 2, 1)) = 187 And Asc(Mid$(sLine, 3, 1)) = 191 Then
                sLine = Mid$(sLine, 4)
            End If
        End If
        arr = ParseCSVLine(sLine)
        If UBound(arr) >= 4 Then
            lst.AddItem Trim$(arr(0)) & vbTab & Trim$(arr(1)) & vbTab & Trim$(arr(2)) & vbTab & Trim$(arr(3)) & vbTab & Trim$(arr(4))
        End If
    Loop
    Close #fn
    Exit Sub
ErrHandler:
    If fn <> 0 Then Close #fn
End Sub

Function ParseCSVLine(ByVal sLine As String) As Variant
    Dim inQuote As Boolean, field As String, fields() As String
    Dim pos As Long, L As Long, n As Long, ch As String
    pos = 1: L = Len(sLine): field = "": n = 0
    Do While pos <= L
        ch = Mid$(sLine, pos, 1)
        If ch = """" Then
            If inQuote Then
                If pos < L And Mid$(sLine, pos + 1, 1) = """" Then
                    field = field & """": pos = pos + 1
                Else
                    inQuote = False
                End If
            Else
                inQuote = True
            End If
        ElseIf ch = "," And Not inQuote Then
            ReDim Preserve fields(0 To n): fields(n) = field: n = n + 1: field = ""
        Else
            field = field & ch
        End If
        pos = pos + 1
    Loop
    ReDim Preserve fields(0 To n): fields(n) = field
    ParseCSVLine = fields
End Function

Notes and troubleshooting: Notepad may save as UTF-8 — either save as ANSI or remove the BOM as shown. Using a ListBox with tabs is simple but column alignment can be improved with a FlexGrid or ListView. The parser above handles quoted commas and doubled quotes but not embedded newline characters inside fields; files containing embedded CR/LF inside quotes require a more advanced reader (or ADODB.Stream / proper CSV library). Fields should be validated after parsing (check numeric age, trim spaces, handle missing fields). This covers a reliable baseline usable for the 15-record contacts file described in the thread.

Recommended Answers

All 2 Replies

Just posting questions won't get you answers. Show your code, and clearly state the problem you are having.

so its in csv format. you can easily read/write that using vb6.

can you show us what you have done so far?..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.