Converting a Text File into a Database 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
Your task can be broken down into a number of small steps.
Let's try and knock the sharp corners off those one at a time.
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...
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.