TiderInsider
New Member
- Joined
- May 18, 2004
- Messages
- 27
I need to import many non-delimited files from text into an access database. The text files come from the mainframe and are all of the same format. I have written the code to import the files into excel, but now I am thinking access would be a better tool for this project.
My question is...how much do I need to change the code to get access to import these text files? Currently, with excel I have all the file names in a directory sheet and I have written a program that imports these files into excel. I think you should be able to get the gist of what this program does.
I am not looking for someone to write the code for me...rather I would like to hear of some good resources for doing something like this...and have a reasonble expectation of how difficult this is to do.
Thanks in advance for any help.
Below is some of my code:
My question is...how much do I need to change the code to get access to import these text files? Currently, with excel I have all the file names in a directory sheet and I have written a program that imports these files into excel. I think you should be able to get the gist of what this program does.
I am not looking for someone to write the code for me...rather I would like to hear of some good resources for doing something like this...and have a reasonble expectation of how difficult this is to do.
Thanks in advance for any help.
Below is some of my code:
Code:
Sub importprn()
Dim strFileName As String
Dim Row As Integer, Column As Integer
Dim Data As String, i As Integer, Age As String, Year As String
Dim Exposure As Double, Lapse As Double, Ratio As Double
Dim strTime As String, Duration As String, StartRow As Integer
Dim finalif As Integer, j As Integer
Application.ScreenUpdating = False
finalif = Application.WorksheetFunction.CountIf _
(Sheets("Directory").Range("D2:D200"), "*")
Row = 5
For i = 1 To finalif
Sheets("Test").[fname] = Sheets("Directory").Cells(1 + i, 5)
Sheets("Test").[Name] = Sheets("Directory").Cells(1 + i, 1)
Sheets("Test").[Mode] = Sheets("Directory").Cells(1 + i, 2)
strFileName = [directory] & "\" & [fname]
Open strFileName For Input As #1
While Not EOF(1)
Line Input #1, Data
Year = Mid(Data, yStart, 2)
Line Input #1, Data
Age = Mid(Data, aStart, 2)
SkipLines 2 'A FUNCTION TO AID IN MOVING THROUGH
For j = 1 To 12 'THE TEXT FILE
Line Input #1, Data
Duration = Mid(Data, dStart, 8)
Exposure = CDbl(Mid(Data, eStart, 12))
Lapse = CDbl(Mid(Data, lStart, 12))
Ratio = CDbl(Mid(Data, rStart, 6))
Sheets("Test").Cells(Row, 1) = [Name]
Sheets("Test").Cells(Row, 2) = [Mode]
Sheets("Test").Cells(Row, 3) = Year
If Age = " " Then
Sheets("Test").Cells(Row, 4) = "Aggregate"
Else
If CDbl(Age) = 0 Then
Sheets("Test").Cells(Row, 4) = CDbl(Age)
Else
Sheets("Test").Cells(Row, 4) = CDbl(Age) + 2
End If
End If
Sheets("Test").Cells(Row, 5) = Duration
Sheets("Test").Cells(Row, 6) = Exposure
Sheets("Test").Cells(Row, 7) = Lapse
If Ratio > 1 Then
Sheets("Test").Cells(Row, 8) = 1
Else
Sheets("Test").Cells(Row, 8) = Ratio
End If
Row = Row + 1
Next j
.........SOME CODE NOT INCLUDED TO SAVE SPACE
For j = 1 To 13
Line Input #1, Data
Duration = Mid(Data, dStart, 8)
Exposure = CDbl(Mid(Data, eStart, 12))
Lapse = CDbl(Mid(Data, lStart, 12))
Ratio = CDbl(Mid(Data, rStart, 6))
Sheets("Test").Cells(Row, 1) = [Name]
Sheets("Test").Cells(Row, 2) = [Mode]
Sheets("Test").Cells(Row, 3) = Year
If Age = " " Then
Sheets("Test").Cells(Row, 4) = "Aggregate"
Else
If CDbl(Age) = 0 Then
Sheets("Test").Cells(Row, 4) = CDbl(Age)
Else
Sheets("Test").Cells(Row, 4) = CDbl(Age) + 2
End If
End If
Sheets("Test").Cells(Row, 5) = Duration
Sheets("Test").Cells(Row, 6) = Exposure
Sheets("Test").Cells(Row, 7) = Lapse
If Ratio > 1 Then
Sheets("Test").Cells(Row, 8) = 1
Else
Sheets("Test").Cells(Row, 8) = Ratio
End If
Row = Row + 1
Next j
SkipLines 3
Wend
Close #1
Next i
Application.ScreenUpdating = True
End Sub