'' *******************************************************************
'' 1. Import *.txt file into Access table
''
'' 2. Edit/add the "Description" property to the table
'' ---------------------------------------------------
'' References:
''
'' Microsoft DAO 3.6 Object Library,
'' Microsoft Scripting Runtime
''
'' *******************************************************************
Sub ImprtProp()
On Error GoTo Err_ImprtProp
Dim dbs As DAO.Database, tdf As DAO.TableDef, prop As DAO.Property
Dim strTableName As String, strPropName As String, strPropDescription As String
Dim strMsg As String, strFile As String, strPath As String
Dim fs As FileSystemObject, f As Object
Dim strLine As String
Dim intLineNo As Integer, intLineLen As Integer
Const ForReading = 1
strFile = "file.txt"
strPath = "C:\Documents and Settings\Administrator\Desktop\"
strTableName = "tblImport"
strPropName = "Description"
strPropDescription = "Import: " & strFile
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(strPath & strFile, ForReading)
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strTableName)
With rst
' Start reading the file
Do While f.AtEndOfStream <> True
strLine = Trim(f.ReadLine)
intLineLen = Len(strLine)
intLineNo = f.Line
' Specify criteria here,
' eg no import of empty lines
If intLineLen > 0 Then
.AddNew
!ImpText = strLine
!ImpNo = intLineNo
.Update
End If
Loop
End With
rst.Close
f.Close
' And now for something completely different:
Set tdf = dbs.TableDefs(strTableName)
' Check setting: Tools>Options>General>Error Trapping
' It should not be set to break on all errors because the
' next line will cause an error if the property does not exist!
tdf.Properties(strPropName) = strPropDescription
On Error GoTo 0
Exit_ImprtProp:
Set dbs = Nothing
Set rst = Nothing
Set tdf = Nothing
Set prop = Nothing
Set fs = Nothing
Set f = Nothing
Exit Sub
Err_ImprtProp:
' Error 3270 = Property not found.
If DBEngine.Errors(0).Number = 3270 Then
' User information, possibly input
strMsg = Err.Number & vbCrLf & _
String(Len(Err.Description), "-") & vbCrLf & _
Err.Description & vbCrLf & _
String(Len(Err.Description), "-") & vbCrLf & _
Err.Source
MsgBox strMsg, vbOKOnly, "Error"
' Create property, set its value, and append it to the Properties collection.
' Note: The new property setting may not be visible right away,
' press F5 key in database window
Set prop = tdf.CreateProperty(strPropName, 10, strPropDescription)
tdf.Properties.Append prop
Resume Exit_ImprtProp
Else
' Other errors
MsgBox Err.Description
Resume Exit_ImprtProp
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''