Use VBS to Export Access Query to Tab-Delimited Text

I'd like to schedule a task to export a tab-delimited file based on an Access query. For several reasons, I'd like to do this by running a Visual Basic script. I have the code below, which is producing an error on Line 19 Char 1  ("a = rs.GetString"): Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

Can you help? Here is the code:

db = "path to database"
TextExportFile = "path to text file"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open _
   "Provider = Microsoft.Jet.OLEDB.4.0; " & _
   "Data Source =" & db

strSQL = "SELECT * FROM QueryName"

rs.Open strSQL, cn, 3, 3

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile(TextExportFile, True)

a = rs.GetString

f.WriteLine a

f.Close
davewalter1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
<Requested operation requires a current record.>
typically means that no records exist

Your coding seems a bit "loose" here
Can you post the *entire* code please...
I don't see how/where you are declaring your variables...

Can you state what this code is supposed to be doing, ...perhaps there is a simpler method...
0
davewalter1Author Commented:
Hi, thanks for the response.

I actually borrowed this code (I'm not a real programmer -- I muddle through), and that's all there is of it. All I need to do is run a query, pull all the records, and write them out to a tab-delimited text file.

I've been having problems with Access saving export specifications, or I would just use the wizard, save the specs, call them from a macro, and be good to go. But I can't get it to save export specs.

So for this and other reasons, I thought this would be a simple way to go. I'm not committed to this code, however, so if you know a better approach, I'm all ears!

Thanks again.
0
Rey Obrero (Capricorn1)Commented:
try

rs.Open strSQL, cn, 3, 3

if  not rs.eof then

Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.CreateTextFile(TextExportFile, True)

a = rs.GetString

f.WriteLine a

f.Close

else
   msgbox "No records"

end if
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davewalter1Author Commented:
Thanks for your suggestions, but I've gone a different route. I finally got the Access extraction process to work well enough to get a macro to work, and I'm running that macro as a scheduled task.

Capricorn, your code returned the error message box, but I suspect that's my fault because of the paths I was using to get to the database. So I'm trusting that your solution would have worked had I done my part, and I'm awarding you the points.

Thanks again for your trouble!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.