Results 1 to 4 of 4

Thread: How to read a file using ADO

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    Nashville, TN
    Posts
    3

    Lightbulb How to read a file using ADO

    How do I read a *.csv file using ADO so I can import it into a table.

    Thanks,

    Steve

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Galway, Ireland
    Posts
    316
    You woudn't read the file using ado here is an example of what you want (I run a stored procedure to load it but you could just run the sql

    lFileNum = FreeFile()
    Open txtFileName.Text For Input As #lFileNum

    Do Until EOF(lFileNum)
    Line Input #lFileNum, sRecord
    lRow = lRow + 1

    sValues = Split(sRecord, "|")
    sKitPartNo = sValues(0)
    sStockroomTransmitAs = sValues(1)
    sDescription = sValues(2)

    .Clear
    .AddParameter "@vkit_part_no", adParamInput, adVarChar, 20, sKitPartNo
    .AddParameter "@vdescription", adParamInput, adVarChar, 100, Trim(sDescription)
    .AddParameter "@vstockroom_transmit_as", adParamInput, adVarChar, 3, Trim(sStockroomTransmitAs)

    If Not (.ExecuteSP("spm_import_kit", adoImport)) Then
    Screen.MousePointer = vbDefault
    MsgBox "An error occurred importing the Kits." & vbCrLf & gobjSQL.LastError, vbCritical
    GoTo end_cmdImport_Click
    End If
    Loop

    Close #lFileNum
    Slan

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    To open a CSV file using ADO you can use the Jet oledb provider. You need to set some extended properties. Here is a sample.

    VB Code:
    1. Dim objDB As ADODB.Connection
    2. Dim objRS As ADODB.Recordset
    3.    
    4. Set objDB = New ADODB.Connection
    5.    
    6. objDB.Open "provider=microsoft.jet.oledb.4.0;data source=C:\Projects;" & _
    7. "Extended Properties='text;HDR=NO;FMT=Delimited'"
    8.    
    9. Set objRS = New ADODB.Recordset
    10.  
    11. objRS.Open "Select * From Testing.txt", objDB, adOpenStatic, adReadOnly, adCmdText
    12.  
    13. Do
    14.    '... Processing
    15.    objRS.MoveNext
    16. Loop Until objRS.Eof
    17.  
    18.  
    19. objRS.Close
    20. objDB.Close
    21. Set objDB = Nothing

    HOWTO: Open Delimited Text Files Using the Jet Provider's Text IIsam

    If you are interested - to open fixed length files you need to indicate the fields via a schema ini file. How to Create a Schema.ini File Programmatically

  4. #4
    Lively Member roglopes's Avatar
    Join Date
    Sep 2002
    Location
    Brazil
    Posts
    113

    How to read a file using ADO (WHERE)

    How I use WHERE when I read a file using ADO?
    My fields are separeted with ;
    Thanks and sorry about my bad engish.

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