Reading information from .dat files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rnashting
    New Member
    • Mar 2010
    • 17

    Reading information from .dat files

    I am trying to create a form that writes a .dat file, and then another form that reads the file and outputs the fields into a list box. The creation seems to have gone fine, but reading the file seems to not work so much. Can anyone tell me what I'm doing wrong? Here's what I have so far:

    Code:
    Private Sub cmdCreate_Click()
        Open "c:\users\brian\Documents\School\CPT 323\friends.dat" For Output As #1
        Write #1, 1, "phone number", "first name", "last name"
        Write #1, 2, "111-111-1111", "Michael", "Massey"
        Write #1, 3, "222-222-2222", "Brian", "Tjarks"
        Write #1, 4, "333-333-3333", "George", "Fuller"
        Close 1    
    End Sub
    
    Private Sub cmdRead_Click()
        Dim phoneNumber As String
        Dim firstName As String
        Dim lastName As String    
        Open "friends.dat" For Input As #1    
        Do Until EOF(1)
            Input #1, phoneNumber, firstName, lastName
            lstFriends.AddItem "phoneNumber " & _
            "firstName " & "lastName"
        Loop    
        Close 1       
    End Sub
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Why in the world would you want to create an external data file instead of simply using a table to hold your data?

    Linq ;0)>

    Comment

    • rnashting
      New Member
      • Mar 2010
      • 17

      #3
      using .dat

      I'm still learning to program, and the book I'm using is teaching about .dat files now. You're right that it doesn't seem like the best way, but I don't want to discount it without knowing what I'm discounting :)

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        You've made several mistakes, so hopefully this will be a learning experience for you. I'll simply Post the code and should you have any questions, feel free to ask:
        Code:
        Open "c:\users\brian\Documents\School\CPT 323\friends.dat" For Output As #1
        
        Write #1, 1, "phone number", "first name", "last name"
        Write #1, 2, "111-111-1111", "Michael", "Massey"
        Write #1, 3, "222-222-2222", "Brian", "Tjarks"
        Write #1, 4, "333-333-3333", "George", "Fuller"
        
        Close #1
        Code:
        Dim SequenceNumber As Long
        Dim phoneNumber As String
        Dim firstName As String
        Dim lastName As String
        
        With Me![lstFriends]
          .RowSourceType = "Value List"
          .ColumnCount = 4
          .ColumnWidths = ".25 in;;;"
        End With
        
        Me![lstFriends].RowSourceType = "Value List"
        Me![lstFriends].ColumnCount = 4
        
        Open "c:\users\brian\Documents\School\CPT 323\friends.dat" For Input As #1
        
        Do While Not EOF(1)
         Input #1, SequenceNumber, phoneNumber, firstName, lastName
           lstFriends.AddItem SequenceNumber & ";" & phoneNumber & ";" & firstName & ";" & lastName
        Loop
        
        Close #1

        Comment

        • rnashting
          New Member
          • Mar 2010
          • 17

          #5
          Thank you so much! The book we are using is very vague...and as you can see, I'm still not very good at this. I didn't even realize that the number in the write was a field, I assumed it was a place holder. Concerning your With block, can I set those things within the form design or do I have to write the code? I had set those things when building the form, and it seems to work, I'm just curious as to why you coded it instead.
          Thanks again for your help!

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            The With..End With Block sets certain Properties of the List Box in Code, but you can also do this in the Properties Window for the List Box (manually).

            Comment

            Working...