I used the code below to import CSV files from a local folder then into a table in Access.
However, I noticed the Leading Zeros are missing/truncated.
Not sure why as I was under the impression that Access did not cut off the zeros.
I tried padding with zeros but not working. I am thinking the leading zeros are being truncated during import into access and since this is happening by code, not sure how to stop it.
Hopefully the great pros here can take a look:
Option Compare Database
Option Explicit
Sub ImportFileProcedure()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim xlApp As Object, xlWb As Object, xlWs As Object
Dim lngRow As Long, lngLastRow As Long
Dim rst As DAO.Recordset
strPath = "C:\FOLDER\"
strTable = "TEMP_TBL"
strFile = Dir(strPath & "*.csv")
Set rst = CurrentDb.OpenRecordset(strTable)
Do While Len(strFile) > 0
strPathFile = strPath & strFile
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Open(strPathFile)
Set xlWs = xlWb.Worksheets(1)
lngLastRow = xlWs.UsedRange.Rows.Count
For lngRow = 2 To lngLastRow
rst.AddNew
rst("SAMPLE") = xlWs.Range("A" & lngRow).Value
rst("SAMPLE") = xlWs.Range("B" & lngRow).Value
rst("SAMPLE") = xlWs.Range("C" & lngRow).Value
rst("SAMPLE") = xlWs.Range("D" & lngRow).Value
rst("SAMPLE") = xlWs.Range("E" & lngRow).Value
rst.Update
Next
xlApp.Quit
Set xlApp = Nothing
Set xlWb = Nothing
Set xlWs = Nothing
strFile = Dir()
Loop
rst.Close
Set rst = Nothing
End Sub
Thanks for looking
I decided to simply create an UPDATE query that adds/pads the leading zeros, the UPDATE is run after the data becomes loaded into the table. This was the best I could do rather than modifying the above code:
This is SQL for the UPDATE Query:
Thanks everyone!