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:
Dim fNo As Integer
Dim sTemp As String
fNo = FreeFile
Open app.Path & "\Text1.txt" For Input As fNo
Do While Not EOF(fNo)
Line Input #fNo, sTemp
Loop
oSheet.Range("A1").Value = sTemp
oWorkbook.SaveAs App.Path & "\TestBook.xls"
Close fNo
I request the members show me how to do the above or point to some tutorial.
Thankyou.
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
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
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.
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
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
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.
THANK YOU FOR RECOGNIZING MY CONTRIBUTION AND RATING ME!
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.
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).
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
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.
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