Private Sub DBCreate()
'Create new MDB.
Dim catDB As Object
Set catDB = CreateObject("ADOX.Catalog")
catDB.Create strConn
With catDB
'Create empty table.
Set cnDB = .ActiveConnection
With cnDB
.Execute "CREATE TABLE Employees (" _
& "[Source] TEXT(255) WITH COMPRESSION, " _
& "[Date] DATETIME, " _
& "[Time] DATETIME, " _
& "[EmployeeNo] TEXT(20) WITH COMPRESSION)"
End With
cnDB.Close
Set cnDB = Nothing
End With
End Sub
Private Function Extract(ByVal CSVName As String) As String
Extract = Left$(CSVName, InStrRev(CSVName, ".") - 1)
End Function
Private Function DBImport(ByVal CSVName As String) As Long
'Assumes cnDB is open to the MDB.
Dim intFile As Integer
intFile = FreeFile(0)
Open strCSVFolder & "\schema.ini" For Output As #intFile
Print #intFile, "[" & CSVName & "]"
Print #intFile, "MaxScanRows=1"
Print #intFile, "Format=CSVDelimited"
Print #intFile, "ColNameHeader=False"
Print #intFile, "DateTimeFormat=YYYY/MM/DD HH:NN:SS"
Print #intFile, "Col1=""Date"" DateTime"
Print #intFile, "Col2=""Time"" DateTime"
Print #intFile, "Col3=EmployeeNo Text Width 20"
Close #intFile
cnDB.Execute _
"INSERT INTO [Employees] ([Source], [Date], [Time], [EmployeeNo]) " _
& "SELECT '" _
& Extract(CSVName) _
& "' AS [Source], [Date], [Time], " _
& "[EmployeeNo] FROM [Text;Database=" _
& strCSVFolder _
& "].[" _
& CSVName _
& "]", _
DBImport, _
adCmdText Or adExecuteNoRecords
Kill App.Path & "\schema.ini"
End Function
Private Sub DBOpen()
Set cnDB = New ADODB.Connection
cnDB.Open strConn
cmdImport.Enabled = True
End Sub
Private Sub cmdImport_Click()
Log "Text imported: " & CStr(DBImport("abc.txt")) & " records."
Log "Text imported: " & CStr(DBImport("xyz.txt")) & " records."
End Sub