Hi iedereen,
In een Access 2003 DB wil ik met het FyleSystemObject een Teksbestand inlezen in een tabel.
Eerst duid ik de file aan die ik wil inlezen via een absoluut path.
varData is de Array waar ik 11 velden (FaVelden) in stop omdat mijn bestand ook 11 velden heeft.
Elk veld in het textbestand is afgesloten door een pipe |
Alles loopt echt heel goed tot de loop aan de laatste ingevulde lijn komt, deze wordt nog ingelezen in varData tot het laatste veld.
Maar dan als de loop moet stoppen omdat het de laatste lijn is komt de foutboodschap: Run-time error '9' Subscript out of range
Ik heb al zoveel uitgeprobeerd maar zonder hulp raak ik er zelf niet uit. De code heb ik uit macro's gehaald die anderen hadden geschreven, het meeste kan ik verklaren maar niet (FaVelden -1) misschien dat iemand anders het wel ziet?
Ik ben nog niet gekomen aan het daadwerkelijk importeren naar een tabel via een RecordSet omdat ik hier al ben vastgelopen.
Kan iemand me bij deze macro helpen aub? Ik heb ook een klein .txt bestandje toegevoegd
Groetjes,
Monique
In een Access 2003 DB wil ik met het FyleSystemObject een Teksbestand inlezen in een tabel.
Eerst duid ik de file aan die ik wil inlezen via een absoluut path.
varData is de Array waar ik 11 velden (FaVelden) in stop omdat mijn bestand ook 11 velden heeft.
Elk veld in het textbestand is afgesloten door een pipe |
Alles loopt echt heel goed tot de loop aan de laatste ingevulde lijn komt, deze wordt nog ingelezen in varData tot het laatste veld.
Maar dan als de loop moet stoppen omdat het de laatste lijn is komt de foutboodschap: Run-time error '9' Subscript out of range
Ik heb al zoveel uitgeprobeerd maar zonder hulp raak ik er zelf niet uit. De code heb ik uit macro's gehaald die anderen hadden geschreven, het meeste kan ik verklaren maar niet (FaVelden -1) misschien dat iemand anders het wel ziet?
Ik ben nog niet gekomen aan het daadwerkelijk importeren naar een tabel via een RecordSet omdat ik hier al ben vastgelopen.
Kan iemand me bij deze macro helpen aub? Ik heb ook een klein .txt bestandje toegevoegd
Code:
Option Compare Database
Option Explicit
Private Const FaDelim As String = "|"
Private Const FaVelden As Byte = 11
Public Function ImportFA() As Boolean
Dim fd As FileDialog, strBestand As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogOpen)
'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim varSelectedItem As Variant
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the drive where the file exists.
.InitialFileName = A:\Bestanden\
'Don't allow the selection of multiple files.
.AllowMultiSelect = False
'Change the title of the dialog
.Title = "Kies het FA bestand: "
'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the action button.
Select Case .Show
Case False
Set fd = Nothing
Exit Function
End Select
'Step through each string in the FileDialogSelectedItems collection
For Each varSelectedItems In .SelectedItems
MsgBox "Bestand is geselecteerd"
Next varSelectedItems
strBestand = .SelectedItems(1)
End With
Set fd = Nothing
'Feitelijk inlezen van het bestand:
Dim fso As FileSystemObject, f As TextStream, varData() As Variant
Dim L As Long, i As Integer, strLijn() As String
'The following code illustrates how the FileSystemObject is used to return a TextStream object that can be read from or written to:
Set fso = New FileSystemObject
Set f = fso.OpenTextFile(strBestand)
'per record wordt de data veld per veld ingevuld
'Store|FTT|Route|Stop|FSP|Aant_Pal|Sel_Method|Total_Volume|Total_Weight|Aant_Prod|Aant_Collis
'10544|KKK|3702 | 1 |400| 1 | S | 0.239 | 74.70 | 26 | 28
'10544|YYY|3702 | 1 |400| 1 | S | 0.487 | 135.36 | 41 | 56
With f
Do While .AtEndOfStream <> True
ReDim Preserve varData(0 To (FaVelden -1), 0 To L)
strLijn = VBA.Split(.ReadLine, "|")
For i = 0 To (FaVelden -1)
varData(i, L) = strLijn(i)
Next
L = L + 1
Loop
.Close
End With
Set fso = Nothing
Set f = Nothing
End Function
Groetjes,
Monique
Bijlagen
Laatst bewerkt: