Image of Navigational Panel mapped to Contents / Home / Search Converting a Text File into a Database
Tech Support Question

Image of line break

Q. Please help!

I'm trying to convert a text file into a database using Visual Basic. After which I will use the grid to display and manipulate the data. Do you have any suggestions? Books, similar code, etc.?

Please help, I'm desperate.

Edward Overhuls
Email : edward@inx.net

A. Ed, your question covers a lot of ground, certainly what you are asking is more than achievable. For a start Visual Basic has virtually the same test file reading abilities as any other version of Basic (remember Input#?), as well as a reasonable database engine underneath it.

Your task can be broken down into a number of small steps.

  1. Read and interpret the text file.
  2. Write the interpreted data into a database table.
  3. Display the results in a grid.

Let's try and knock the sharp corners off those one at a time.

  1. The first thing to determine here is what sort of format the file is in. Visual Basic is quite adept at reading both fixed width and delimited text files. Essentially this involves reading each line of the file (have a look at Line Input# in VBs online help), and separating the string you read into each field. You will need to use the Open statement to open the file, and have a look at FreeFile for allocating a file handle.

  2. Writing the data to a database is a different matter, for this you will first of all need a Database. The easiest way to create a database for this sort of use is to use Microsoft Access. It can be done through VB code - but Access is easier. You need to make sure the database has a table for you to write the data into. The OpenDatabase function will get your database open, the OpenTable function will give you direct access to the table. Then you can read through each line of the file (as above), and write it into the table. Remember to not only close the file when you are finished, but to close the table and database as well. If you have a look at the VisData sample that ships with VB3 or 4 you will see examples of using functions like OpenDatabase, OpenTable and so on. It should roughly look like this:

      Dim tblImport as Table
      Dim dbImport as Database
      Dim iHandle as integer
    
      Set dbImport = OpenDatabase("C:\Foobar\MYDB.MDB")
      Set tblImport = dbImport.OpenTable("MyImportTable")
      iHandle = FreeFile
      Open "C:\Foobar\MyImport.TXT" For Input Access Read As iHandle
      Do While Not EOF(iHandle)
      ' Read a line of data from the text file
      ' Break the line into the fields you want
      ' Write the data to the table
      Loop
      Close #iHandle
      tblImport.Close
      dbImport.Close
    

    That's a rough structure anyway...

  3. Displaying the data in a grid can be done fairly easily. It depends on what grid you are using as to how exactly you would do this. To use the standard Grid that comes with VB 3 all you have to do is iterate through each row and column that you want, placing the data in the cell as you read it from the open table. The Grid sample application that ships with VB should give you a fair idea of how to do this. Also the VisData sample that ships with VB does this sort of thing.

Remember that the methods I have outlined here are not the most efficient and are not the prettiest, but for a beginner this should get you up and running. Just take it one step at a time, and make sure each section is working before going on to the next. The important thing is to get started and give it a go. If you have any further specific questions feel free to send them in.

Good luck and happy coding.


Image of Arrow to Previous Article Image of Arrow to Next Article

[TECH SUPPORT TOC]
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]