Question : Import fails for "data  type conversion error'

I have  raw text files that i import into Access as TBL_Data.
I have the attached code that imports the Text files into a table.
Other than the column headers, all the data is numbers.
The import automatically assigns each field as a TEXT Data Type value.

I need the Data Type to be Number so I can write SUM queries for reports.

When i changed the Data Type to Number in the table the import scipt fails with a "Data Type converson error".

The Debugger references this line of code:
"rstImportData(strFields(lngField)) = strValues(lngField)"

What am i missing???
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
Public Function ImportData2800()
    Const IMPORT_FOLDER As String = "C:\Shared\Reports\Data\"
    Dim dbsCurrent As DAO.Database
    Dim fldField As DAO.Field
    Dim intFile As Integer
    Dim lngField As Long
    Dim strFields() As String
    Dim strFile As String
    Dim strLine As String
    Dim strTable As String
    Dim strDevice As String
    Dim strValues() As String
    Dim rstImportData As DAO.Recordset
    Dim tdfTableDef As DAO.TableDef

    ' Process files
    Set dbsCurrent = CurrentDb
    strFile = Dir(IMPORT_FOLDER & "\*.txt")
    Do Until strFile = ""

        ' Open file
        intFile = FreeFile
        Open IMPORT_FOLDER & strFile For Input Access Read Shared As #intFile

        ' Name the master table here
        strTable = "TBL_DATA"  'Change me to whatever you want

        ' Name the device
        strDevice = Left(strFile, Len(strFile) - 4)
         
        ' Read column headings
        If Not EOF(intFile) Then
            Line Input #intFile, strLine
            strFields = Split(strLine, vbTab)
        End If

        ' This should get rid of any double spaces.
        For j = 0 To UBound(strFields)
            strFields(j) = Replace(strFields(j), "  ", " ")
        Next
        ' Add the column for the device into our list of columns
        ReDim Preserve strFields(0 To UBound(strFields) + 1) As String

        strFields(UBound(strFields)) = "Device"

        ' Create table, if necessary
        Set tdfTableDef = Nothing
        On Error Resume Next  ' Ignore missing table
        Set tdfTableDef = dbsCurrent.TableDefs(strTable)
        On Error GoTo 0
        If tdfTableDef Is Nothing Then
            Set tdfTableDef = dbsCurrent.CreateTableDef(strTable)
        End If

        ' Create fields, if necessary
        For lngField = 0 To UBound(strFields)
            Set fldField = Nothing
            On Error Resume Next  ' Ignore missing fields
            Set fldField = tdfTableDef.Fields(strFields(lngField))
            On Error GoTo 0
            If fldField Is Nothing Then
                Set fldField = tdfTableDef.CreateField(strFields(lngField), dbText)  ' Always create text fields
                tdfTableDef.Fields.Append fldField
            End If
        Next

        ' Create date stamp date field, if necessary
        Set fldField = Nothing
        On Error Resume Next  ' Ignore missing field
        Set fldField = tdfTableDef.Fields("DateStamp")
        On Error GoTo 0
        If fldField Is Nothing Then  ' No DateStamp field
            Set fldField = tdfTableDef.CreateField("DateStamp", dbDate)
            fldField.DefaultValue = "Date()"
            tdfTableDef.Fields.Append fldField
        End If

        On Error Resume Next  ' Ignore existing table
        dbsCurrent.TableDefs.Append tdfTableDef
        On Error GoTo 0
        ' Read file
        Set rstImportData = CurrentDb.OpenRecordset(strTable)
        Do Until EOF(intFile)
            Line Input #intFile, strLine
            strValues = Split(strLine, vbTab)
            ReDim Preserve strValues(0 To UBound(strValues) + 1) As String
            strValues(UBound(strValues)) = strDevice
            ' Import data
            rstImportData.AddNew
            For lngField = 0 To UBound(strValues)
                rstImportData(strFields(lngField)) = strValues(lngField)
            Next
            rstImportData.Update
        Loop
        rstImportData.Close

        ' Close file
        Close #intFile
        strFile = Dir
    Loop
End Function

Answer : Import fails for "data  type conversion error'

This article solved my problem.  I did everything in this article, and was able to reinstall WSUS SP 1.

http://social.technet.microsoft.com/Forums/en-US/winserverwsus/thread/6d14231f-50f0-4a3c-b9f1-417e1efdb288

Random Solutions  
 
programming4us programming4us