CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Posts
    29

    Inport .csv File into a Access DB Programmatically

    I need to be able to inport a .csv file into an access database programmatically. If you have any ideas please let me know.
    Thanks,
    Brian


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Inport .csv File into a Access DB Programmatically

    private Sub Form_Load()
    CommonDialog1.ShowOpen
    If len(CommonDialog1.Filename) > 0 then ParseFile (CommonDialog1.Filename)
    End Sub

    public Function ParseFile(Filename as string, optional ParseChar as string = "|") as Boolean
    Dim sArray() as string
    Dim intNum as Integer
    Dim lngCnt as Long
    Dim inputString as string
    Dim lngLoop as Long

    intNum = FreeFile'Read the File Into an Array
    Open Filename for input as intNum
    Do While Not EOF(intNum)
    Line input #intNum, inputString
    'if you want to put into excel then use this and write sarray to an output file
    'sArray(lngLoop) = Replace(sArray(lngLoop), "|", vbTab)

    ReDim Preserve sArray(lngCnt)
    sArray(lngCnt) = inputString
    lngCnt = lngCnt + 1
    Loop'Close the File
    Close intNum
    Dim sRowArray() as string
    Dim RowLoop as Long
    'Split each line into an array
    for lngLoop = 0 to lngCnt - 1
    sRowArray = Split(sArray(lngLoop), ParseChar)
    'Read each field in the line
    for RowLoop = 0 to UBound(sRowArray) - 1
    Debug.print sRowArray(RowLoop)
    ' Here you would probably want to do your field assingment and do an addnew
    next
    next
    End Function




    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163

    Re: Inport .csv File into a Access DB Programmatically

    This code will import into a table in Access
    [vbcode]
    Dim appAccess As New Access.Application
    '***********************************************************************
    '************* Open Access database and transfer file to DB ************
    'Must select Access 8 Object Library for Access 97 &
    'Access 9 Library for Access 2000 in Project|References
    'Open Database DB Path Exclusive
    appAccess.OpenCurrentDatabase "Path\Some..MDB", True

    ' Transfer text file to database
    appAccess.DoCmd.TransferText acImportDelim, , "Tablename", "Path\somefile.csv", 1

    ' Close
    appAccess.CloseCurrentDatabase

    ' Set object to Nothing
    Set appAccess = Nothing
    [\vbcode]


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

Featured