Results 1 to 17 of 17

Thread: Data Transfer from Text file to Excel & MS Access table

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Lightbulb Data Transfer from Text file to Excel & MS Access table

    I just learned how to copy data in a ADO recordset into Excel File using this link by Si_the_geek.
    I also learned some Textfile manipulations from the links of dee-u & Pengate.

    Thank you everyone.

    Now I am wondering how to transfer the data
    i) from a textfile (which is having ":" as delimiter) to access table
    ii) from a textfile (which is having ":" as delimiter) to Excel File
    ii) from a Excel file to an MS Access Table

    I tried using the below code. No data was transferred. So its obviously incorrect
    Text to Excel Code:
    1. Dim fNo As Integer
    2.     Dim sTemp As String
    3.     fNo = FreeFile
    4.    
    5.     Open app.Path & "\Text1.txt" For Input As fNo
    6.         Do While Not EOF(fNo)
    7.             Line Input #fNo, sTemp
    8.         Loop
    9.         oSheet.Range("A1").Value = sTemp
    10.         oWorkbook.SaveAs App.Path & "\TestBook.xls"
    11.     Close fNo
    I request the members show me how to do the above or point to some tutorial.
    Thankyou.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    Use a standard delimiter (either a comma to create CSV, or a tab to create tab-delimited file) so you can use Jet database engine to extract records from file and insert into Access as a batch (rather than executing insert query per record).
    http://www.vbforums.com/showthread.p...sert+csv+parse

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    sorry leinad31 I am not able to follow the coding in your link. That appears to be more specific to the text file attached there. Can you show me a simpler coding. OK I will go for vbTab as delimiter.Let us go step by step. First show me how create CommaSV/TabSV file.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    You can use Replace() fnuction to change all instances of colon to tab/comma.

    Then try connecting to a CSV or tab-delimited file. Research appropriate connection string. http://www.carlprothman.net/Default.aspx?tabid=81

    Once you have the connection, you just execute the query.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Lightbulb Re: Data Transfer from Text file to Excel & MS Access table

    I have come this much
    New Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3.  
    4. Private Sub Command1_Click()
    5.     'On Error Resume Next
    6.     Set rs = New ADODB.Recordset
    7.     rs.Open "SELECT * FROM Text3.txt", conn, adOpenStatic, adLockOptimistic, adCmdText
    8.         If rs.RecordCount > 0 Then
    9.             For i = 0 To rs.RecordCount - 1
    10.                 MsgBox rs.Fields(0).Name & " --> " & rs.Fields(0)
    11.                 rs.MoveNext
    12.             Next i
    13.         End If
    14.     rs.Close
    15.  
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Set conn = New ADODB.Connection
    20.     conn.Open _
    21.         "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    22.         "Dbq=" & App.Path & "\;" & _
    23.         "Extensions=asc,csv,tab,txt"
    24. End Sub
    25.  
    26. Private Sub Form_Unload(Cancel As Integer)
    27.     Set rs = Nothing
    28.     conn.Close
    29.     Set conn = Nothing
    30. End Sub
    but when I change to this line
    Error Point Code:
    1. rs.Open "SELECT * FROM Text3.txt WHERE value1 =20", conn, adOpenStatic, adLockOptimistic, adCmdText
    I get err msg ODBC driver does not support the requested properties.
    I have also attached the sample data I am using.
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    Other than doing an rs.MoveNext when your iterating the column names and not row data... sorry I am unable to simulate your error (check if you have filename right)... it works fine here.

    Code:
    Option Explicit
    
    Private oConn As ADODB.Connection
    
    Private Sub Form_Load()
    Dim rs As ADODB.Recordset
    Dim i As Long
    
       Set oConn = New ADODB.Connection
       'oConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=c:\temp\;" & _
          "Extended Properties=""text;HDR=Yes;FMT=Delimited"""
      
       oConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
          "Dbq=c:\temp\;" & _
          "Extensions=asc,csv,tab,txt"
    '-----
       'Set rs = oConn.Execute("SELECT * FROM text3.txt WHERE Value1 = 20 ")
        
       Set rs = New ADODB.Recordset
       rs.Open "SELECT * FROM text3.txt WHERE Value1 = 20 ", oConn, adOpenStatic, adLockOptimistic, adCmdText
    '-----
       For i = 0 To rs.Fields.Count - 1
          Text1.Text = Text1.Text & vbTab & rs.Fields(i).Name
       Next
       Text1.Text = Text1.Text & vbCrLf & rs.GetString(, , vbTab, vbCrLf, "")
    End Sub

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    I had the code revised (minor) and the criteria is working
    Revised Code:
    1. Dim oExcel As Object
    2. Dim oWorkbook As Object
    3. Dim oSheet As Object
    4. Dim oRangeTargetCell As Object
    5.  
    6. Dim conn As ADODB.Connection
    7. Dim rs As ADODB.Recordset
    8.  
    9. Dim lngRcdsAffected As Long
    10.  
    11. Private Sub Command1_Click()
    12.     lngRcdsAffected = 0
    13.     Set oExcel = CreateObject("Excel.Application")
    14.     Set oWorkbook = oExcel.Workbooks.Add
    15.     Set oSheet = oWorkbook.Worksheets(1)
    16.         GetTextFileData "SELECT * FROM Text3.txt WHERE value1 = 2  ORDER BY [Grand Total] DESC", "F:\Tips for VB Access etc\VB\Export Txt to Excel\"
    17.         oWorkbook.SaveAs App.Path & "\Book" & Format(Date, "d_M_yyyy") & ".xls"
    18.     Set oSheet = Nothing
    19.     Set oWorkbook = Nothing
    20.     oExcel.Quit
    21.     Set oExcel = Nothing
    22.     MsgBox "Export Sucessful" & vbCrLf & lngRcdsAffected & " Records Exported", vbInformation, App.Title
    23. End Sub
    24.  
    25. Private Sub GetTextFileData(strSQL As String, strFolder As String)
    26.  
    27.     Set conn = New ADODB.Connection
    28.  
    29.     conn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
    30.         "Dbq=" & strFolder & ";" & _
    31.         "Extensions=asc,csv,tab,txt;"
    32.  
    33.     If conn.State <> adStateOpen Then Exit Sub
    34.     Set rs = New ADODB.Recordset
    35.  
    36.     MsgBox strSQL
    37.     rs.Open strSQL, conn, adOpenStatic, adLockOptimistic, adCmdText 'adOpenForwardOnly, adLockReadOnly, adCmdText
    38.  
    39.     If rs.State <> adStateOpen Then
    40.         conn.Close
    41.         Set conn = Nothing
    42.         Exit Sub
    43.     End If
    44.    
    45.     ' the field headings
    46.     Dim i As Integer
    47.     For i = 0 To rs.Fields.Count - 1
    48.         oSheet.Cells(1, i + 1).Value = rs.Fields(i).Name
    49.     Next i
    50.     oSheet.Range("A2").CopyFromRecordset rs
    51.    
    52.     oSheet.Range("A1:F1").Font.Bold = True
    53.     oSheet.Columns.EntireColumn.Autofit
    54.    
    55.     lngRcdsAffected = rs.RecordCount
    56.    
    57.     rs.Close
    58.     Set rs = Nothing
    59.     conn.Close
    60.     Set conn = Nothing
    61. End Sub
    But another problem is noticed. If the data in the txt file is CSV then the export works properly, but if the data is TSV (Tab seperated) all values are loaded into a single column see my attachment Is there something more to be done in case of TSVs.
    Attached Files Attached Files

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    Try the Jet OLEDB connection string. My suggestion was primarily for file to access transfer using INSERT INTO. Also, Excel can open tab-delimited files by default (no need to go through recordset) so no need to convert to xls unless you plan to include other features of Excel such as functions.
    Last edited by leinad31; Oct 23rd, 2007 at 12:47 AM.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    Try the Jet OLEDB connection string.
    instead of this
    Code:
      conn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
            "Dbq=" & strFolder & ";" & _
            "Extensions=asc,csv,tab,txt;"
    the usual Microsoft.Jet.OLEDB.4.0

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    Excel can open tab-delimited files by default (no need to go through recordset) so no need to convert to xls
    can you explain more. Or preferably post the codes using my earlier ex.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    If your using WinXP simply right click file then select Open With... then use Excel to open tab-delimited file. Which shows it can be opened using excel without having to setup a recordset (which is similar to using the data import feature of excel).

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    Which shows it can be opened using excel without having to setup a recordset
    Ok but how do I do it through code

  13. #13
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    You will need to change name of first column from ID (case sensitive) http://support.microsoft.com/kb/215591

    Input: sample.txt
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim oExcel As Object
    Dim oWorkbook As Object
    
       Set oExcel = CreateObject("Excel.Application")
       Set oWorkbook = oExcel.Workbooks.Open("c:\temp\sample.txt", , , , , , , , 1)  '1 indicates delimiter is tab
       oWorkbook.SaveAs "c:\temp\sample", 1   '1 indicates xls file type
       oWorkbook.Close
       oExcel.Quit
       Set oWorkbook = Nothing
       Set oExcel = Nothing
       Unload Me
    End Sub
    Output: sample.xls

    Additional reference http://msdn2.microsoft.com/en-us/lib...ffice.11).aspx

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    me too was reading the same links
    http://support.microsoft.com/kb/215591
    http://support.microsoft.com/kb/247412
    but these do not allow to retrieve records from the txtfile based on some criteria. am I correct or is it possible?

  15. #15
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    Yes, tab-delimited file is loaded into the worksheet as it is. If you want functionality of WHERE clause then I guess the CopyFromRecordset() implementation is the way to go.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Data Transfer from Text file to Excel & MS Access table

    then how to overcome prblm reported in #7

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Data Transfer from Text file to Excel & MS Access table

    For tab delimited files, you need to create schema.ini file located in same folder as textfile being read by Jet DB engine. Ini file provides format info (delimiter used) of textfiles http://msdn2.microsoft.com/en-us/library/ms709353.aspx
    Attached Files Attached Files

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