G
Guest
Is there a way in Access to add the filename (actually the first 8 letters of the filename) to each record when importing a file. I would have to somehow create the field and then for each record put the filename. I need to set this up in VBA so that it is automatic. I am transfering out of Excel into Access, and I know how to do this in excel just fine, but have no idea where to start in Access. Below is the VB code that I use in Excel, which works fine, but I don't know how to convert this process to Access
Any help would be greatly appreciated
Stev
Sub SetupReport(
Dim cLastRow As Lon
Dim i As Lon
Dim Sh As Workshee
Set Sh = ActiveWorkbook.ActiveShee
cLastRow = Cells(Rows.Count, "D").End(xlUp).Ro
'Looks in column D to get the filenam
For i = 2 To cLastRo
UpdateTextFile Left(Sh.Cells(i, "D").Value, Len(Sh.Cells(i, "D").Value) - 4
Next
End Su
Sub UpdateTextFile(Name As String
Dim wb As Workboo
'Opens the txtfil
Workbooks.OpenText
FileName:=ThisWorkbook.Path & "\RawTextFiles\" & Name,
DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote,
ConsecutiveDelimiter:=False,
Tab:=True,
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1),
Array(4, 1), Array(5, 1), Array(6, 1),
Array(7, 1), Array(8, 1), Array(9, 1),
Array(10, 1), Array(11, 1), Array(12, 1),
Array(13, 1), Array(14, 1), Array(15, 1),
Array(16, 1)
'finds the last row of data in the text fil
Dim LastRowForSerielNumber1 As Lon
LastRowForData = Cells(Rows.Count, "A").End(xlUp).Ro
'Adds the file name and then copies it down the pag
Range("Q1").Selec
ActiveCell.Formula = "=Left(Name,8)
Selection.Cop
Range("Q2:Q" & LastRowForData).Selec
Selection.Past
'Saves and closes the workboo
ActiveWorkbook.Close SaveChanges:=Tru
End Sub
Any help would be greatly appreciated
Stev
Sub SetupReport(
Dim cLastRow As Lon
Dim i As Lon
Dim Sh As Workshee
Set Sh = ActiveWorkbook.ActiveShee
cLastRow = Cells(Rows.Count, "D").End(xlUp).Ro
'Looks in column D to get the filenam
For i = 2 To cLastRo
UpdateTextFile Left(Sh.Cells(i, "D").Value, Len(Sh.Cells(i, "D").Value) - 4
Next
End Su
Sub UpdateTextFile(Name As String
Dim wb As Workboo
'Opens the txtfil
Workbooks.OpenText
FileName:=ThisWorkbook.Path & "\RawTextFiles\" & Name,
DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote,
ConsecutiveDelimiter:=False,
Tab:=True,
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1),
Array(4, 1), Array(5, 1), Array(6, 1),
Array(7, 1), Array(8, 1), Array(9, 1),
Array(10, 1), Array(11, 1), Array(12, 1),
Array(13, 1), Array(14, 1), Array(15, 1),
Array(16, 1)
'finds the last row of data in the text fil
Dim LastRowForSerielNumber1 As Lon
LastRowForData = Cells(Rows.Count, "A").End(xlUp).Ro
'Adds the file name and then copies it down the pag
Range("Q1").Selec
ActiveCell.Formula = "=Left(Name,8)
Selection.Cop
Range("Q2:Q" & LastRowForData).Selec
Selection.Past
'Saves and closes the workboo
ActiveWorkbook.Close SaveChanges:=Tru
End Sub