Results 1 to 3 of 3

Thread: Import CSV to table using VBA

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    23

    Question Import CSV to table using VBA

    Hi all,

    I would like to use VBA to import a csv file into an existing access table. Fairly new to VBA, and this is out of my current league...could anyone please point me in the right direction.

    Cheers for any help.

  2. #2
    Member
    Join Date
    Nov 2002
    Location
    England
    Posts
    34
    Hi marksimus,

    This was in VB but I changed to VBA from memory so check it but it should work for you:

    Code:
    Function importData(sFileName As String, vDelimeter As Variant, sQuery As String)
        
        Dim vArray() As String
        Dim iLocalLoop As Integer
        Dim db as Database
        Dim rst As New Recordset
        Dim sLine As String
        
        Close #1
        Open sFileName For Input As #1
        Set db = CurrentDB
        Set rst = db.OpenRecordset(sQuery)
        
        Do Until EOF(1)
            sLine = ""
            Line Input #1, sLine
            vArray = Split(sLine, vDelimeter)    ' vDelimeter can be vbtab or a comma ',' etc.
            rst.AddNew
            iLocalLoop = 0
            For iLocalLoop = 0 To UBound(vArray())
                On Error Resume Next            ' In case of vartype error
                rst.Fields(iLocalLoop) = vArray(iLocalLoop)
                On Error GoTo ErrMsg
            Next iLocalLoop
            rst.Update
        Loop
        
        Close #1
        rst.Close
        set rst = Nothing
        
    End Function
    You just need to pass it the filename with path, delimeter and the query or table name.

    Hope it helps.

    TC

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    23

    Talking Cheers TC

    Thanks TC,

    This is exactly what I need.

    Cheers

    Marksimus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width