new to access, import a .txt file no delimiters to Access

leeloo0505

Board Regular
Joined
Mar 28, 2003
Messages
130
Hello new to Access..
Looked in the historical posts to see how to get started importing a .txt file with no delimiters into an Access table...and not able to find one general enough to get me started, is it just like Excel and determining arrays?

Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You can import text files using File>Get External Data>Import...
This will give you a dialog box to select your file then open a wizard to help you split up the data.

HTH

Peter
 
Upvote 0
Re: new to access, import a .txt file no delimiters to Acces

Thanks, I did the Import wizard thing, but was wanting the actual code that does this.. Any one have a resource that shows how to 'basically' get started? My guess is it's something similiar to the vb code in excel using arrays?
Thanks,
leeloo
 
Upvote 0
You need to use the "TransferText" method but to import a fixed-width text file you need to create and save a specification first using the wizard.

Peter
 
Upvote 0
Re: new to access, import a .txt file no delimiters to Acces

Hi Peter,

Thanks for your time on this.
I did finish the wizard import and have a table with data in it. Is this what you mean my saving a specification? If so, cool but not sure what I do from here. Looking in the Design Veiw, I see it has the field size and all.. is that all I need?

leeloo
 
Upvote 0
When you run the wizard there is an advanced option button, if you click it you can then save the definition and use this saved definition in the transferText code

Peter
 
Upvote 0
Re: new to access, import a .txt file no delimiters to Acces

Hi Peter,

Think I just figured out what you were talking about with the specifications, I imported the file using the Advanced Import Specification but didn't realize you could save it... sorry to take so much of your time.

leeloo
 
Upvote 0
Re: new to access, import a .txt file no delimiters to Acces

Well if you just must, go nuts here. I developed this because Access had a infuritating problem with saving specifications beyond some size (25 fields, I think it was). This may have been corrected in later versions - feedback welcome on that issue.

Just substitute your desired field names and widths into the code below, and set the constant as desired. Warning: compress after using. It simply absurdly bloats the .MDB. Feedback is welcome on a tweak to index 1 or more fields (this code does not).

Option Compare Database
Option Explicit

Sub ImportTextFile()
'import a fixed width text file into a new table called "Newtable" (you must rename it afterwards)
'LOOK BETWEEN THE ASTERISKS BELOW for variable info
Dim db As Database
Dim rs As Recordset
Dim tdfNewtable As TableDef, fldNew As Field
Dim strData As String, strTemp As String
Dim strDirFileName As String
Dim fieldnames, fieldlengths 'as is, so valid as variants for Array function
Dim i As Long, iFieldcount As Long, iRunningTotal As Long, iRecordCount As Long
'********************************************************************************************************
strDirFileName = "c:\dUMMY." 'text file source
'build these in Excel from Lotus from AS-400 download control file
fieldnames = Array( _
"MTRCST", "MTUPST", "MTDADD", "MTDOLC", "MTOPID", _
"MTCOID", "MTPLAN", "MTCODE", "MTAGE", "MTDUR", _
"MTSEX", "MTEFDT", "MTRES1", "MTRES2", "MTRES3", _
"MTRES4", "MTRES5", "MTRES6", "MTRES7", "MTRES8", _
"MTRES9", "MTRES0")
fieldlengths = Array( _
1, 1, 8, 8, 3, _
3, 5, 1, 3, 3, _
1, 10, 8, 8, 8, _
8, 8, 8, 8, 8, _
8, 8)
#Const TABLE_PREEXISTS = False
'********************************************************************************************************
iFieldcount = UBound(fieldnames)
Set db = CurrentDb
Close #1
Open strDirFileName For Input As #1 'open the file for input
#If Not TABLE_PREEXISTS Then
Set tdfNewtable = db.CreateTableDef("Newtable")
With tdfNewtable
' Create and append new Field objects to the table.
For i = 0 To iFieldcount
Set fldNew = .CreateField(fieldnames(i))
fldNew.Size = fieldlengths(i)
fldNew.Type = dbText
.Fields.Append fldNew
Next i
End With
db.TableDefs.Append tdfNewtable 'this actually creates the table described above
#End If
Set rs = db.OpenRecordset("Newtable", dbOpenDynaset)
Do Until EOF(1)
Line Input #1, strData
rs.AddNew 'get set for new data
iRunningTotal = 1 'because "Mid" function is 1-based
For i = 0 To iFieldcount
strTemp = Mid(strData, iRunningTotal, fieldlengths(i))
If strTemp = "" Then strTemp = " "
rs.Fields(i) = strTemp
iRunningTotal = iRunningTotal + fieldlengths(i)
Next i
rs.Update
iRecordCount = iRecordCount + 1
'If iRecordCount > 3 Then Stop 'debugging statement
If 0 = iRecordCount Mod 10000 Then Debug.Print iRecordCount & " records so far"
Loop
Close #1
Debug.Print iRecordCount & " records - job complete"
ProcExit:
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

------------
And as I always say, for A2000, at least:
To fix F#@$!ing Access to run NORMAL #$%%$#!! CODE:
tools/references/check Microsoft DAO 3.6 Object Library
get RID of ADO 2.1
rock on, brother
 
Upvote 0
Re: new to access, import a .txt file no delimiters to Acces

Thanks much appreciated, I just wanted to get a look at it.. helps me understand what is going on and modify and tweak certain things..

leeloo
 
Upvote 0

Forum statistics

Threads
1,223,560
Messages
6,173,030
Members
452,500
Latest member
jesuisazep

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top