Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Want to read text files of random extensions into recordset

Hi,

This is an excerpt of some code I have been working on:
-------start------------
FileToOpen = Application.GetOpenFilename
If FileToOpen < False Then
'Set path
path = Left(FileToOpen, InStrRev(FileToOpen, "\"))
'Open connection to file
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path & ";Extended
Properties='text;HDR=NO;FMT=Delimited'"
'Set file
file = Right(FileToOpen, Len(FileToOpen) - InStrRev(FileToOpen, "\"))
'Open recordset with Earnie data
rsEarnie.Open "Select * From " + file, cn, adOpenStatic, adLockReadOnly,
adCmdText
-------end------------

It works fine when the source file has an extension of "txt", but I want it
to work regardless of the extension. The file that will be selected will be
produced by our payroll package and is created as payroll.001 or
payroll.002. Rather than ask users to rename the file with a "txt"
extension, is there a way to read the data in the file as text without the
file explicitly being named as a text file? I have tried adCmdUnknown and
all the others but they don't work. I get: "Run-time error'-2147217911
(80040e09': Automation error"

Any ideas?

TIA

Jarryd

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Want to read text files of random extensions into recordset


Well, you did say *-any-* ideas...

Copy the file with the odd extension, save it as a file with a .txt
extension, read that file, then delete it. Or, if no one else is going
to try to use the payroll files while you are messing with them, just
rename them to something like payroll.001.txt and then rename them back
when you are through

Lots of work - and slow - but if the .Open syntax is going to require a
.txt extension, you may not find a better way. Good luck on it,
though.

Actually, I searched for

jet odbc text file extension "not txt"

and got an instructive hit at

'Much ADO About Text Files'
(http://msdn.microsoft.com/en-us/library/ms974559.aspx)

but I don't know if you want to get into registry editing on every PC
that might need to read the payroll files...


--
jamescox
------------------------------------------------------------------------
jamescox's Profile: http://www.thecodecage.com/forumz/member.php?userid=449
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=126727

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Want to read text files of random extensions into recordset

Excellent response. I did try renaming and filecopy to a txt file, etc. but
I hit permissions issues cos you can't always be sure that Excel has rights
to the payroll file location. Seems to be a common problem. To be totally
honest I need to research more of the workarounds and see if there is a
solid one out there that works for all (or almost all) scenarios.

I will read the msdn article and have a think about it. I wish there was a
way to tell this idiot payroll system how to dump output files, but anyway.

Thanks again.

Jarryd

"jamescox" wrote in message
...

Well, you did say *-any-* ideas...

Copy the file with the odd extension, save it as a file with a .txt
extension, read that file, then delete it. Or, if no one else is going
to try to use the payroll files while you are messing with them, just
rename them to something like payroll.001.txt and then rename them back
when you are through

Lots of work - and slow - but if the .Open syntax is going to require a
txt extension, you may not find a better way. Good luck on it,
though.

Actually, I searched for

jet odbc text file extension "not txt"

and got an instructive hit at

'Much ADO About Text Files'
(http://msdn.microsoft.com/en-us/library/ms974559.aspx)

but I don't know if you want to get into registry editing on every PC
that might need to read the payroll files...


--
jamescox
------------------------------------------------------------------------
jamescox's Profile:
http://www.thecodecage.com/forumz/member.php?userid=449
View this thread:
http://www.thecodecage.com/forumz/sh...d.php?t=126727

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Want to read text files of random extensions into recordset

Hi all,

I have worked out that using the user TEMP folder to copy the file to as a
txt works. Sorting out the code now and will post when done so it can be
used if someone needs it.

Jarryd

"Jarryd" wrote in message
...
Excellent response. I did try renaming and filecopy to a txt file, etc.
but I hit permissions issues cos you can't always be sure that Excel has
rights to the payroll file location. Seems to be a common problem. To be
totally honest I need to research more of the workarounds and see if there
is a solid one out there that works for all (or almost all) scenarios.

I will read the msdn article and have a think about it. I wish there was
a way to tell this idiot payroll system how to dump output files, but
anyway.

Thanks again.

Jarryd

"jamescox" wrote in message
...

Well, you did say *-any-* ideas...

Copy the file with the odd extension, save it as a file with a .txt
extension, read that file, then delete it. Or, if no one else is going
to try to use the payroll files while you are messing with them, just
rename them to something like payroll.001.txt and then rename them back
when you are through

Lots of work - and slow - but if the .Open syntax is going to require a
txt extension, you may not find a better way. Good luck on it,
though.

Actually, I searched for

jet odbc text file extension "not txt"

and got an instructive hit at

'Much ADO About Text Files'
(http://msdn.microsoft.com/en-us/library/ms974559.aspx)

but I don't know if you want to get into registry editing on every PC
that might need to read the payroll files...


--
jamescox
------------------------------------------------------------------------
jamescox's Profile:
http://www.thecodecage.com/forumz/member.php?userid=449
View this thread:
http://www.thecodecage.com/forumz/sh...d.php?t=126727

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Want to read text files of random extensions into recordset

Hi all,

This is what I came up with:
----------Start----------
Sub...
Dim cn As New ADODB.Connection
Dim rsEarnie As New ADODB.Recordset
Dim iLastRow As Integer
Dim sPathUser As String

FileToOpen = Application.GetOpenFilename

If FileToOpen < False Then
'Get TEMP path and copy FileToOpen to it
sPathUser = Environ$("TEMP")
FileCopy FileToOpen, sPathUser + "\payroll.txt"

'Open connection to file
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sPathUser &
";Extended _
Properties='text;HDR=NO;FMT=Delimited'"

'Open recordset with Earnie data
rsEarnie.Open "Select * From payroll.txt", cn, adOpenStatic, adLockReadOnly,
adCmdText
....
rsEarnie.Close
cn.Close
Kill (sPathUser + "\payroll.txt")
End If

End Sub
----------End----------

HTH someone.

Cheers,

Jarryd

"Jarryd" wrote in message
...
Hi all,

I have worked out that using the user TEMP folder to copy the file to as a
txt works. Sorting out the code now and will post when done so it can be
used if someone needs it.

Jarryd

"Jarryd" wrote in message
...
Excellent response. I did try renaming and filecopy to a txt file, etc.
but I hit permissions issues cos you can't always be sure that Excel has
rights to the payroll file location. Seems to be a common problem. To
be totally honest I need to research more of the workarounds and see if
there is a solid one out there that works for all (or almost all)
scenarios.

I will read the msdn article and have a think about it. I wish there was
a way to tell this idiot payroll system how to dump output files, but
anyway.

Thanks again.

Jarryd

"jamescox" wrote in message
...

Well, you did say *-any-* ideas...

Copy the file with the odd extension, save it as a file with a .txt
extension, read that file, then delete it. Or, if no one else is going
to try to use the payroll files while you are messing with them, just
rename them to something like payroll.001.txt and then rename them back
when you are through

Lots of work - and slow - but if the .Open syntax is going to require a
txt extension, you may not find a better way. Good luck on it,
though.

Actually, I searched for

jet odbc text file extension "not txt"

and got an instructive hit at

'Much ADO About Text Files'
(http://msdn.microsoft.com/en-us/library/ms974559.aspx)

but I don't know if you want to get into registry editing on every PC
that might need to read the payroll files...


--
jamescox
------------------------------------------------------------------------
jamescox's Profile:
http://www.thecodecage.com/forumz/member.php?userid=449
View this thread:
http://www.thecodecage.com/forumz/sh...d.php?t=126727

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read of excel spreadsheet with ADO recordset ER Excel Programming 2 September 16th 08 01:47 PM
help with macro to read and sort data from multiple text files [email protected] Excel Programming 0 March 15th 07 11:02 PM
ADO recordset closed, cannot read Michael Kellogg Excel Programming 2 May 6th 05 10:22 PM
How to read multiple text files into excel? Together Excel Programming 3 February 29th 04 09:00 AM
Read/Write data to/from text files from a spreadsheet. lothario[_37_] Excel Programming 8 November 2nd 03 03:10 PM


All times are GMT +1. The time now is 03:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"