Results 1 to 5 of 5

Thread: [RESOLVED] Need Help Converting Text File to Recordset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    California
    Posts
    79

    Resolved [RESOLVED] Need Help Converting Text File to Recordset

    Hello Everybody, I have a little experience with ADO Recordsets. I made a Database Program with VB6, Using Access 97 as the Database.The Program works Just Fine. I am a Teacher, I get Updated Information from What I believe was Created In a DOS Batch File that When Activated Creates a Text File Containing Student Information. I want to Be Able to Convert the Text File to a ADO Recordset then Save the Information into my Program's Database. The Information I want to retrieve is as Follows:

    StudentNo:
    LastName:
    FirstName:
    Facility:
    Class:
    Teacher:
    Grade:

    I'm not sure where to begin, Any help would be appreaciated.
    Thanks
    Ready to be a Student
    Robert

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

    Re: Need Help Converting Text File to Recordset

    How is the text file layed out? What type of format is it in?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    California
    Posts
    79

    Re: Need Help Converting Text File to Recordset

    I opened the file in MSAccess 97, it said the fields were fixed Width. One other thing I left out, the text characters of the file on each line I only need the first 70. The rest of the Line I do not need, since all of the information is contained in the first 70.
    The Text file contains anywhere between 5800 to 6200 lines (Records)
    I though of creating a Recordset that would search the File for only the Information that matches my Records that way I would not have to load the entire file.

    Maybe like this:

    VB Code:
    1. Dim cn as New ADODB.Connection
    2. Dim rs as New ADODB.Recordset
    3. Dim sSqlstr as String,cnstr as string,conn as string
    4. Dim MyLines As String,MyFields as String
    5. Dim i as Long
    6.  
    7. UpdateCon 'Read from the Registry
    8. Set cn = New ADODB.Connection
    9. Set rs = New ADODB.Recordset
    10. cnstr = Conn ' DBPath String
    11.  
    12. 'Select the Students
    13.  
    14. sSqlstr = "Select StudentNo,LastName,DateAsgn,Facility,ClassNm,Teacher FROM Students WHERE Title = 'Student' "
    15.  
    16. cn.Open cnstr
    17. rs.CursorLocation = adUseClient
    18. rs.Open sSqlstr, cn, , , adCmdText
    19.  
    20.   If Not (rs.bof = True) and Not (rs.eof = true) then
    21.       rs.MoveFirst
    22.  
    23.      While Not rs.eof
    24.          for i = 1 to UBound(MyLines)
    25.       Open App.path & "\" & "Update.txt" for Input as #1
    26.        'Here is where I am Stuck
    27.  
    28.        Close # 1
    29.  
    30.  
    31. wend
    32.  
    33. end if
    Robert

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    California
    Posts
    79

    Re: Need Help Converting Text File to Recordset

    Well after a few hours here is what I came up with it works. If anyone has a better solution I'm all Ears... thanks ...

    VB Code:
    1. Sub OpenText()
    2. Dim Student as string,LName as string,FName as String,HClass as string,MyGrade as String,DateAsgn as String,Fac as String,MyLine as String
    3. Dim MyRow as Long, recs as Long
    4.  
    5. recs = 0
    6. MyRow = 1
    7.  
    8. 'Read the Database Connection String
    9.         UpdateCon
    10.         Set cn = New ADODB.Connection
    11.         Set rs = New ADODB.Recordset
    12.         cnstr = Conn
    13.  
    14.         'Select the Records
    15.         strSql = "SELECT StudentNo,Last,First,Facility,MyClass,DateAsgn,Grade FROM Students  WHERE Status = 'Assigned' ORDER BY StudentNo" '
    16.         cn.Open cnstr
    17.         rs.CursorLocation = adUseClient
    18.         rs.Open strSql, cn, , , adCmdText
    19.  
    20.         If Not (rs.BOF = True) Or Not (rs.EOF = True) Then
    21.             rs.MoveFirst
    22.            
    23.             While Not rs.EOF
    24.             Student = rs.Fields("StudentNo").value
    25.             ' Open the selected file.
    26.             Open Filename For Input As #1
    27.             ' Change the mouse pointer to an hourglass.
    28.                 Screen.MousePointer = 11
    29.                 Do While Not EOF(1)   ' Check for end of file.
    30.                
    31.                 Line Input #1, MyLine  ' Read line of data.
    32.                 'Read the Data Needed
    33.                 MyLine = Left(MyLine, 80)
    34.  
    35.                 If Left(MyLine, 6) = StudentNo Then
    36.                 Student = StudentNo
    37.                 'Break up the Data
    38.                 LName = Mid(MyLine, 8, 23)
    39.                 RTrim (LName) 'Trim any spaces
    40.                
    41.                 FName = Mid(MyLine, 31, 17)
    42.                 RTrim (FName) 'Trim any spaces
    43.                
    44.                 HClass = Mid(MyLine, 48, 10)
    45.                 RTrim (MyClass) 'Trim any spaces
    46.                
    47.                 Fac = Mid(MyLine, 59, 2)
    48.                
    49.                 Grade= Mid(MyLine, 65, 3)
    50.                
    51.                 DateAsgn = Mid(MyLine, 70, 10)
    52.  
    53.                 'Set the Coloumn Names
    54.                 FrmDown.VSFlexGrid2.TextMatrix(0, 1) = "Student #"
    55.                 FrmDown.VSFlexGrid2.TextMatrix(0, 2) = "Last Name"
    56.                 FrmDown.VSFlexGrid2.TextMatrix(0, 3) = "First Name"
    57.                 FrmDown.VSFlexGrid2.TextMatrix(0, 4) = "Facility"
    58.                 FrmDown.VSFlexGrid2.TextMatrix(0, 5) = "Class"
    59.                 FrmDown.VSFlexGrid2.TextMatrix(0, 6) = "Grade"
    60.                 FrmDown.VSFlexGrid2.TextMatrix(0, 7) = "Assigned"
    61.  
    62.                 FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = Student
    63.                FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = LName
    64.                  FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = FName
    65.                FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = Facility
    66.               FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = HClass
    67.                FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = Grade
    68.               FrmDown.VSFlexGrid2.TextMatrix(MyRow, 1) = DateAsgn
    69.          
    70.          recs = recs + 1
    71.     MyRow = MyRow + 1
    72.     FrmDown.VSFlexGrid2.Rows = FrmDown.VSFlexGrid2.Rows + 1
    73.  
    74.     End If
    75.        
    76.     Loop
    77.  
    78. Close #1
    79.  
    80. rs.MoveNext
    81. Wend
    82.  
    83. Screen.MousePointer = 0
    84. FrmDown.VSFlexGrid2.Rows = FrmDown.VSFlexGrid2.Rows - 1 'Delete the Extra Row
    85.  
    86. End If
    87.  
    88.  
    89. Set rs = Nothing
    90. Set cn = Nothing
    91.  
    92. end Sub
    Robert

  5. #5
    Addicted Member SpeedyDog's Avatar
    Join Date
    Feb 2004
    Location
    Missouri, USA
    Posts
    189

    Re: [RESOLVED] Need Help Converting Text File to Recordset

    You just want to put the data file into an Access Database?

    Access will input Fixed Length and Delimited Files automatically.
    Just goto File > Get External Data > Import.

    From there, you want to change your FileType to like TEXT or what ever.
    Select the file.. and you can select the lengths of the fields correct?

    Sometimes using Access' features can be much easier than building them yourself.

    -SpeedyDog

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