Avatar of Butterfield_Cayman
Butterfield_Cayman
 asked on

VBS file calling complex access query getting errors

i have a vbs file that is exporting a access query into an excel spreadsheet.

I have a number of problems due to the complexity of the query. I'm using left, right, instring, and various other functions in the qry. When I run the vbs file I recieve the following error: error: "invalod procedure call" code: "80040E14" source: "Microsoft JET Database Engine". I've tried cleaning the qry but am loosnig too much of the functionality.

I see a number of options for my solution:
1) if someone could send me a fix that would allow me to run this qry that would be the ideal solution
2) if not, as an alternate, i could write a create table qry, and a delete table qry and then run the createtbl qry, export the table, and then delete it after. I just need to know how to run queries in access from a vbs file.

the first is my prefered solution and if someone can assist me in finding that by the end of the day I'll up the points. Attached is my vbs snippett and my access qry.
========vbs code===============
 
Set conn = CreateObject ("ADODB.CONNECTION")
Set rs = CreateObject("ADODB.RECORDSET")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Competencies.mdb"
 
sql = "SELECT * INTO [Excel 8.0;Database=c:\book.xls].[NewSheet] FROM [TomTest]"
 
rs.Open sql, conn, 3, 3
 
 
============== access qry==================
SELECT [EMPLOYEE MASTER].Active AS STATUS, [EMPLOYEE MASTER].[Employee ID] AS USERID, ([EMPLOYEE MASTER.Employee ID]+(Left([LASTNAME],4))) AS USERNAME, Right([EMPLOYEE MASTER.employee name],(Len([EMPLOYEE MASTER.employee name])-(InStr([EMPLOYEE MASTER.employee name],",")+1))) AS FIRSTNAME, Left([EMPLOYEE MASTER.employee name],(InStr([EMPLOYEE MASTER.employee name],",")-1)) AS LASTNAME, [EMPLOYEE MASTER].Sex AS GENDER, [EMPLOYEE MASTER].Email AS EMAIL, [Find Supervisor ID].[EMPLOYEE MASTER_Employee ID] AS USERID2, "1581" AS HR, [Find Supervisor ID].[Employee ID] AS MANAGER, [EMPLOYEE MASTER].[Job Title] AS TITLE, "KY" AS LOCATION, "EST" AS TIMEZONE, TomEmpPayGrade.Code, IIf([Value] In ("1","2","3","4"),"GRADE01-04",IIf([Value] In ("5","6","7","8"),"GRADE05-08",IIf([Value] In ("9","10","11"),"GRADE09-11","GRADE12-15"))) AS jobcodetom, TomEmpPayGrade.Value AS CUSTOM01, DIVISIONS.[Div Name] AS DIVISION, DEPARTMENTS.[Dept Name] AS DEPARTMENT, [EMPLOYEE MASTER].[Vacation Days], [EMPLOYEE MASTER].[Vacation Remaining], ([EMPLOYEE MASTER.Vacation Days])-([EMPLOYEE MASTER.Vacation Remaining]/7.5) AS CUSTOM04, [EMPLOYEE MASTER].[Sick Remaining], [EMPLOYEE MASTER].[Sick Days], [EMPLOYEE MASTER.Sick Days]-([EMPLOYEE MASTER.Sick Remaining]/7.5) AS CUSTOM03, [EMPLOYEE MASTER].[Start Date] AS HIREDATE, [EMPLOYEE MASTER].[Current Job Start Date] AS CUSTOM02, TomEmpPayGrade.Value
FROM ((DEPARTMENTS INNER JOIN (DIVISIONS INNER JOIN [EMPLOYEE MASTER] ON DIVISIONS.ID = [EMPLOYEE MASTER].[Div ID]) ON DEPARTMENTS.ID = [EMPLOYEE MASTER].[Dept ID]) INNER JOIN [Find Supervisor ID] ON [EMPLOYEE MASTER].[Employee ID] = [Find Supervisor ID].[EMPLOYEE MASTER_Employee ID]) INNER JOIN TomEmpPayGrade ON [EMPLOYEE MASTER].[Employee ID] = TomEmpPayGrade.[Employee ID]
WHERE ((([EMPLOYEE MASTER].Active)=True));

Open in new window

Microsoft AccessVB Script

Avatar of undefined
Last Comment
Patrick Matthews

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Patrick Matthews

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Butterfield_Cayman

ASKER
alright, this looks like it might work, but there are two problems:

1) I'm using access 2003, so when opening the database get this msg: opening "c:/competencies.mdb"

This file may not be safe if it contains code that was intended to harm your computer.
Do you want to open this file or cancel the operation?

And then I have to hit the open button. Is there anywhere to bypass this?

2) this code expects c:\books.xls to exist, and that the fields from the query are already there. Ideally I'd like the file to be deleted shortly after being exported. is there a way around this?

Thanks for the help
Patrick Matthews

Butterfield_Cayman said:
>>1) I'm using access 2003, so when opening the database get this msg: opening "c:/competencies.mdb"
>>
>>This file may not be safe if it contains code that was intended to harm your computer.
>>Do you want to open this file or cancel the operation?
>>
>>And then I have to hit the open button. Is there anywhere to bypass this?

Yes.  Open Access, and go to Tools|Macro|Security.  Change the setting to Low.

>>2) this code expects c:\books.xls to exist, and that the fields from the query are already there.
>>Ideally I'd like the file to be deleted shortly after being exported. is there a way around this?

Actually, it should work just fine if the file does not exist...
Butterfield_Cayman

ASKER
1 - brilliant - worked a treat cheers
2 - nope, still no joy. Recieving the attached error which says: "The Microsoft Het database engine could not find the object C:\TomTest.xls'. Make sure the object exists and that you spell its name and the path correctly."

any ideas.

Thanks again for the help.
AccessError.bmp
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Patrick Matthews

Butterfield_Cayman,

That's very odd.  Please paste back the vbs file exactly as you have it now.

Regards,

Patrick
Butterfield_Cayman

ASKER
yea, I'm getting some funky results. I've used transfer spreadsheet in a vba module before, and I'm sure it worked as you expected. I've tried putting this code in a module and am getting the same result. what's more, when I create tomtest.xls and run the query, it's seems to append a single blank row to my table, but do nothing to my spreadsheet???? here's my code below:

Dim acApp

Set acApp = CreateObject("Access.Application")
With acApp
    .OpenCurrentDatabase "C:\mdb\Competencies_2008-04-11.mdb"
    .DoCmd.TransferSpreadsheet 0, 8, "tbltomtest", "C:\TomTest.xls", True
    .CloseCurrentDatabase
    .Quit
End With

Set acApp = Nothing

MsgBox "Done"
Butterfield_Cayman

ASKER
ok, have created a new database, and created one table in it, and wrote the transfertext line of code in a module and got it working from there. obviously there is some problems in my main database. when I try and run the transfer text from the script am still recieving some odd results. it's as if the script is trying to write back to the table. in one set of test, the script kept entering in blank records into my table, when I changed the data source from a table to a qry I recieved a "operation must be an updateable query" error - code 800a0c01.

any ideas?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Patrick Matthews

Butterfield_Cayman said:
>>any ideas?

'fraid not.  That is definitely not how that method is supposed to behave...
Butterfield_Cayman

ASKER
looks like we had the constants of the export and import back to front. 0 being import, 1 being export.
this works a treat:

.DoCmd.TransferSpreadsheet 1, acSpreadsheetTypeExcel9, "tomtest", "c:/tomtest2.xls", True

but this doesn't work

.DoCmd.TransferSpreadsheet acexport, acSpreadsheetTypeExcel9, "tomtest", "c:/tomtest2.xls", True

anyway, thanks a lot for all your help, as promised I've upped the points.
Butterfield_Cayman

ASKER
see my final comments. one of the constants was wrong by tthe looks of things.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Patrick Matthews

Well, *that* certainly explains it :)

Glad to help,

Patrick