Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-30-2001, 04:01 AM   #1
bob
Member (12 bit)
Premium Member
 
bob's Avatar
 
Join Date: Mar 1999
Location: LA, CA
Posts: 2,227
I have done this before in Foxpro. Need to do in Access.

Need to take a text file that is fixed and import into a database [no wizards!]

Example

table1

field1 = 2
Field2 = 5

Text file data looks like this
AABBBBB
AABBBBB

Want to import automatically from the text file where the AA goes into field1 and the BBBBB goes into field2

[Edited by bob on 03-30-2001 at 05:04 AM]
bob is online now   Reply With Quote
Old 03-30-2001, 04:48 AM   #2
SQL nutcase
 
mosquito's Avatar
 
Join Date: Sep 2000
Location: Belgium
Posts: 1,136
Send a message via AIM to mosquito
I think the fastest way to do this is to use ADO. To achieve this, create a reference to "Microsoft Active X data object" in your MsAccess vba project.
Then you have to create a schema.ini file in the directory with the file you want to import. This schema.ini file will define the structure of your text file.

Here is an example of how the schema.ini file could look like. Do note that the "Sample.txt" can be replaced with the filename you are using
Code:
[Sample.txt]
Format=FixedLength
CharacterSet=ANSI
Col1=field1 Text Width 2
Col2=field2 Text Width 5
Now the code to include in your VBA code.
Code:
    Dim conn        As ADODB.Connection
    Dim rs	        As ADODB.Recordset
    
    Set conn = New Connection
        
    'Connect to the Directory where the textfile is                                     '
    With conn
        .CursorLocation = adUseClient
        .ConnectionString = "Provider=MSDASQL;DRIVER={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=" & sDir
        .Mode = adModeRead
        .Open
    End With
    
    
    Set rs = New Recordset
    With rs
        .ActiveConnection = conn
        .CursorType = adOpenForwardOnly
        .LockType = adLockReadOnly
        .Open "select * from [" & sFile & "]"
    End With
    
    Do While Not rs.EOF
        docmd.runquery "Insert into t_table (field1, field2, field3) values (" & _
                       rs.fields("field1") & ", " & rs.fields("field2") & _
                       ", " & rs.fields("field3") & ")"
        rs.movenext
    loop
    
    rs.close
    
    set rs = nothing
    conn.close
    set conn = nothing
That's it. I always create the schema.ini file with code to make the application more flexible.
mosquito is offline   Reply With Quote
Old 03-31-2001, 12:14 AM   #3
bob
Member (12 bit)
Premium Member
 
bob's Avatar
 
Join Date: Mar 1999
Location: LA, CA
Posts: 2,227
Angry

Thanks. Really wanted to avoid that - may just have to do it anyways. Must deal with 20+ different flat files with from 10 to 40 fields each.
bob is online now   Reply With Quote
Old 03-31-2001, 07:01 PM   #4
Member (9 bit)
 
Great_One's Avatar
 
Join Date: May 2000
Location: Lexington, Michigan
Posts: 353
Another option would be to create a macro and use the transfer text option to import the file. you will need to run the import wizard the first time to create and save the import specification, but after that simply running the macro will handle the import of the marco (assuming the file locations and the text file layouts remain static).
the macro can handle multiple transfer text statements so you should be able to do it all with one click.

__________________
Certifiable
===========================================

Cisco CCNA,CCDA
CompTIA A+, Network+,Inet+,Security+
CIW Associate
IBM AIX certified
IBM Certified Specialist - p5 and pSeries Administration and Support for AIX 5L V5.3
IBM Certified Systems Expert - p5 and pSeries Enterprise Technical Support AIX 5L V5.3
Great_One is offline   Reply With Quote
Old 04-04-2001, 03:45 AM   #5
bob
Member (12 bit)
Premium Member
 
bob's Avatar
 
Join Date: Mar 1999
Location: LA, CA
Posts: 2,227
You are the great one for sure. That was exactly what I was thinking on how to solve the problem. Also have GUI programs that need data input too. Macros have the best hope for a solution.

All input files are text and fixed - no deliminators.
I can use ADO - thanx mosquito - Foxpro (because I know it well) can be the main app to automate the processes.

In the old days I only needed to create a simple import with foxpro.
...
open database
append from xxxxxx.txt sdf
...

Grrrrrrrrrrrrrrr.
Worked my way up to WAN admin and now I need to manage data processing and programming. It would be nice if I could reuse my experience but things change fast.


bob is online now   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:52 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2