Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2006, 07:42 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am going on the presumption that the first value in the text file is the account number, the second is the first name, the third is the last name, and the fourth is the phone number. I am also assuming that you don't need the "Start" and "Width" values in the table. If you do, please let me know.

'-----Code Starts-----
Dim sAccount As String
Dim sFName As String
Dim sLName As String
Dim sPhone As String
...

Set objConn = CreateObject("ADODB.Connection")
Set objRS = CreateObject("ADODB.Recordset")
    objConn.Open "DSN=Your DSN;" 'or Use a Provider for Jet to Access
    objRS.Open "SELECT * FROM tblYourTable", objConn, 3, 3

Set fso = CreateObject("Scripting.FileSystemObject")
     If fso.FileExists("C:\data.txt") Then
       Set objStream = fso.OpenTextFile("C:\data.txt", 1, False, 0)
     End If

Do While Not objStream.AtEndOfStream
      strLine = objStream.ReadLine
      YourArray = Split(strLine, " ") 'use the tab key to create space between quotes.

      sAccount = Trim(YourArray(0))
      sFName = Trim(YourArray(1))
      sLName = Trim(YourArray(2))
      sPhone = Trim(YourArray(3))

      objRS.AddNew
      objRS("Account") = sAccount
      objRS("FirstName") = sFName
      objRS("LastName") = sLName
      objRS("Phone") = sPhone
      objRS.Update
Loop

objConn.Close
objRS.Close

'-----Code Ends-----

As I say, you may want to use DAO instead of ADO.

HTH

mmcdonal
 
Old May 3rd, 2006, 08:44 AM
Authorized User
 
Join Date: Apr 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
You know what!!

I am a slave to a person that teach me one word.

Thank you so much

I will let you know abou the results.

The process is a bit more complicated than this, but you helpd with the most important part of it.

Thank you
Sayed

 
Old May 5th, 2006, 05:35 AM
Authorized User
 
Join Date: Apr 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I just wanted to let you know that the function above works perfectly.

I just had to modify the way to extract the fields, because for some reasons the split function did not pick up the 2nd and 3rd values, although I used the tab to create the space.
The array is a bright idea, but I can get it to work


Debug.Print strLine

1234567891234567 SAYED BEN AYCHA 2048349999





     'sAccount = Arrayline(0)
     'Debug.Print sFName
     sFName = Mid(strLine, 21, 30)
     'sFName = Arrayline(1)
     'Debug.Print sLName
     'sLName = Arrayline(2)
     sLName = Mid(strLine, 51, 35)
     'sPhone = Arrayline(3)
     sPhone = Mid(strLine, 86, 30)

Thank you again Sir.
Sayed





Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing Fixed Width .txt Document and Then Some! Diretemus Access VBA 8 March 26th, 2008 07:27 AM
Fixed Width text file to MSDE with C# mgudmund C# 2005 3 November 9th, 2007 05:09 AM
Import a Comma separated txt file to SQL table arielote C# 2 April 17th, 2006 01:08 AM
Automate import (fixed width) U.N.C.L.E. Access VBA 0 February 3rd, 2006 06:55 PM
DTS Fixed Width File Import rochak SQL Server DTS 3 September 26th, 2005 09:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.