ASP Junction Home Of EZCodes
8 visitors online
136393 total visitors

Thursday, 11 March, 2010
Home Downloads Articles Programming Portfolio Support Privacy  




Newsletter


The HTMLJunction Store

Top 5 Downloads
EZNewsletter: 11099
EZGallery: 6267
EZPoll: 6116
EZUpload: 5574
EZGuestbook: 4299
Download Stats
EZHomepagePro: 3512
EZHomepageBasic: 1573
EZNewsletter: 11099
EZOnlineEditor: 3886
EZMedia: 3617
EZGuestbook: 4299
EZGallery: 6267
EZPoll: 6116
EZScheduler: 2226
EZUpload: 5574
Total: 48780
HTMLJunction News
ASP Article - Dynamic Date Dropdown Menu.

ASP Article - Import Text File to an Access Database.

EZScheduler now available for download!

ASP Junction has just been launched!

HTMLJunction has a new store!


Advertisements






HTMLJunction is against any kind of spyware!




ASP Article
Rated out of 5 stars (4.3137) - 51 Total Votes Submit Your ASP Article

Importing a text file to an Access database.



This article will explain how to import the contents of a text file (.txt) when using MS text or OLE DB Drivers wont do! We will be using the FileSystem and TextStream objects for this lesson!

For the purposes of this article we will be using a text file delimited using the "|" symbol. Here is the contents of the text file we will be using - info.txt.

Text File

About the Author

Steve Frazier has been a classic ASP developer for about four years. He has developed ASP applications for Fortune 500 companies and popular websites. He has also developed many ASP Scripts of his own! He is Webmaster of HTMLJunction as well as its sister sites. The HTMLJunction Store - ASP Junction. He is currentlly working on a Web Portal that has the functionalities of all the most popular Forums and Portals.



We will be importing the text file to a database called "people.mdb" with one table called "tbl_people" consisting of 4 fields - "FNAME", "LNAME", "SEX", "AGE"
Now on with the article!

First lets declare some vatiables.

Dim conn, fs, objFile, objFileTextStream, i, strSQL
Dim strLine, strLinePart, strFirstName, strLastName, strGender, intAge


Next lets open a connection to the database we will be using.

Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("people.mdb")&";User Id=Admin;Password="


Now we need to create a FileSystem Object.

Set fs = Server.CreateObject("Scripting.FileSystemObject")

Let's Get The file and open it.

Set objFile = fs.GetFile("info.txt")
Set objFileTextStream = objFile.OpenAsTextStream(1, 2)

Since the first line of the text file is a header which we dont want to enter into the database we will skip that line.

objFileTextStream.skipLine

Now we will loop through lines of the text file and read each line into the database.

Do While objFileTextStream.AtEndOfStream <> True  Keep reading each line until we reach the end of the file (objFileTextStream.AtEndOfStream = True)
  strLine = objFileTextStream.ReadLine  read the current line and assign it to a variable
  strLinePart = split(strLine,"|")  Split each part of the line using the delimiter
  for i = 0 TO UBound(strLinePart)  Loop through each part of the line.
    If i = 0 Then
      strFirstName = Trim(strLinePart(i))
  If we are at the first part of the line then assign the variable
    ElseIf i = 1 Then
      strLastName = Trim(strLinePart(i))
  Line part 2 same thing
    ElseIF i = 2 Then
      strGender = Trim(strLinePart(i))
  Line part 3
    ElseIF i = 3 then
      intAge = Trim(strLinePart(i))
  Line part 4
    End If
  Next


Now that we have each part of the first line set in variables we can enter them into the database

  intAge = Cint(intAge)  If the DataType for the AGE field in the database is an integer then we do this.
  strSQL = "INSERT INTO tbl_people ([FNAME],[LNAME],[SEX],[AGE]) Values('"&strFirstName&"','"&strLastName&"','"&strGender&"','"&intAge&"')"
  conn.Execute strSQL
Loop
  Loop through the rest of the text file and enter the info into the database.

Lets not forget to clean up our mess!

objFileTextStream.Close: Set objFileTextStream = Nothing
Set fs = Nothing
conn.Close: Set conn = Nothing


I have probably forgotten something (I usually do!) though I cant imagine what because this works for me! I suppose some ASP Developers might do it a little different but this is the way I do it and it serves my purposes and I hope yours too!

Here is what the whole thing looks like with out all the comments

<%
 Dim conn, fs, objFile, objFileTextStream, i, strSQL
 Dim strLine, strLinePart, strFirstName, strLastName, strGender, intAge

 Set conn = Server.CreateObject("ADODB.Connection")
 conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("people.mdb")&";User Id=Admin;Password="

 Set fs = Server.CreateObject("Scripting.FileSystemObject")

 Set objFile = fs.GetFile("info.txt")
 Set objFileTextStream = objFile.OpenAsTextStream(1, 2)

 objFileTextStream.skipLine

 Do While objFileTextStream.AtEndOfStream <> True
   strLine = objFileTextStream.ReadLine
   strLinePart = split(strLine,"|")
   for i = 0 TO UBound(strLinePart)
     If i = 0 Then
       strFirstName = Trim(strLinePart(i))
     ElseIf i = 1 Then
       strLastName = Trim(strLinePart(i))
     ElseIF i = 2 Then
       strGender = Trim(strLinePart(i))
     ElseIF i = 3 then
       intAge = Trim(strLinePart(i))
     End If
   Next


   intAge = Cint(intAge)

   strSQL = "INSERT INTO tbl_people ([FNAME],[LNAME],[SEX],[AGE]) Values('"&strFirstName&"','"&strLastName&"','"&strGender&"','"&intAge&"')"
   conn.Execute strSQL
 Loop

 objFileTextStream.Close: Set objFileTextStream = Nothing
 Set fs = Nothing
 conn.Close: Set conn = Nothing
%>

Well thats the nitty gritty of it. I hope you find this article useful!
Rate this Article
Good  
5 4 3 2 1
  Poor

Advertisements








We use Google Sitemaps to inform Google's crawler about all your pages and to help people discover more of your web pages.







Resources

Gunners Gallery
Wesley Ford
seo-advantage.com

copyright © 2010 ASP Junction
An HTMLJunction website