Adding date from header in file to data while importing (1 Viewer)

sandy22

New member
Local time
Today, 06:28
Joined
May 20, 2020
Messages
10
Hello, I receive multiple .txt files everyday, The import code is working fine. I have been looking for examples to see how I can do the below though I am not sure if it can be done, Any guidance is much appreciated.
The First row in every file is the date as below:

HEADER;2021051701
ABC;Origin1; destination1;;;29870
DEF;Origin2; destination2;;;29870

Is there a way where I can take the date from the first row and add it to the data as below?

ABC;Origin1; destination1;;;29870;20210517
DEF;Origin2; destination2;;;29870;20210517
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:28
Joined
Oct 29, 2018
Messages
21,447
Hi. There are several ways to read a file. For example, you can use FSO or the Open statement. If the header info always match the date the file was created, you can even use the FileDateTime function.

 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:28
Joined
May 7, 2009
Messages
19,226
make sure that you always have that "Header"
copy and paste to a Module.
call it:

Call fnAddDate("theTextfileName")
Code:
'arnelgp
'add Reference to Microsoft Scripting Runtime
Public Function fnAddDate(ByVal pTextFile As String)
Dim fso As Scripting.FileSystemObject
Dim f As TextStream
Dim v As Variant
Dim sContent As String
Dim sNewContent As String
Dim sDate As String
Dim tfFirstLine As Boolean
Set fso = New Scripting.FileSystemObject
Set f = fso.OpenTextFile(pTextFile, ForReading)
tfFirstLine = True
With f
    Do Until .AtEndOfLine
        sContent = .ReadLine
        If tfFirstLine Then
            v = Split(sContent, ";")
            sDate = Left$(Trim$(v(1)), 8)
            tfFirstLine = False
        Else
            sNewContent = sNewContent & Replace(Replace$(sContent, Chr(10), ""), Chr(13), "") & ";" & sDate & vbCrLf
        End If
    Loop
    .Close
End With
Set f = fso.OpenTextFile(pTextFile, ForWriting)
f.Write sNewContent
f.Close
Set f = Nothing
Set fso = Nothing
End Function
 

sandy22

New member
Local time
Today, 06:28
Joined
May 20, 2020
Messages
10
I understood the reading part, However I can't edit the files I receive how can I make the code to write the date into a field say DATE in the database table once file is imported ?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:28
Joined
Oct 29, 2018
Messages
21,447
I understood the reading part, However I can't edit the files I receive how can I make the code to write the date into a field say DATE in the database table once file is imported ?
Could you use an UPDATE query?
 

conception_native_0123

Well-known member
Local time
Today, 05:28
Joined
Mar 13, 2021
Messages
1,834
However I can't edit the files I receive
why not? you are saying that you receive text files that are not editable? if they *are*, or that they should be because you are viewing them on your own machine, these are the functions that you would probably, or should use before your importing process:




how can I make the code to write the date into a field say DATE in the database table once file is imported ?
you would have to use a subquery wouldn't you? or, capture the date from the first record however you wish and then run an update query like dbGuy said using a literal like you receive (2021051701) or possibly a "broken-up" literal put into the dateSerial() function:

 

Users who are viewing this thread

Top Bottom