Run-time Error 3012 - Object '_TempQuery_' Already Exists. (1 Viewer)

ckell23

New member
Local time
Today, 08:35
Joined
Jan 26, 2010
Messages
6
I'm stumped on this error message. I modified this code from samples provided on another website and had things working fine.

Do not recall making any changes to the code, but now it is not working. No references to Run-time Erro 3012 anywhere.

Private Sub cmdExportToExcel_Click()
Dim dbs As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim strSQL As String, strQDF As String
Set dbs = CurrentDb
strSQL = "SELECT jobCodes.[Job Code], jobCodes.jobtitle, tblJobFunction.JobFunction AS JobFamilyCode, tblJobFunction.[Long Name] AS JobFamily, tblJobFamily.[Job Family] AS FuncitonCode, tblJobFamily.Descr AS Function, jobCodes.Headcount, jobCodes.Grade, tblProposedGrade.ProposedGrade, tblUsers.Name, tblProposedGrade.EffectiveDate, jobCodes.Status, tblProposedStatus.ProposedStatus, jobCodes.[Comp Plan]"
strSQL = strSQL & " FROM ((((((tblJobFamily RIGHT JOIN jobCodes ON tblJobFamily.[Job Family] = jobCodes.[Job Family]) INNER JOIN qryProposedJobGradeMaxDate ON jobCodes.[Job Code] = qryProposedJobGradeMaxDate.JobCode) INNER JOIN tblProposedGrade ON (qryProposedJobGradeMaxDate.JobCode = tblProposedGrade.JobCode) AND (qryProposedJobGradeMaxDate.MaxOfEffectiveDate = tblProposedGrade.EffectiveDate)) INNER JOIN tblUsers ON tblProposedGrade.UserID = tblUsers.UserID) LEFT JOIN tblJobFunction ON jobCodes.[Job Funct] = tblJobFunction.JobFunction) LEFT JOIN qryProposedStatusMaxDate ON jobCodes.[Job Code] = qryProposedStatusMaxDate.JobCode) LEFT JOIN tblProposedStatus ON (qryProposedStatusMaxDate.JobCode = tblProposedStatus.JobCode) AND (qryProposedStatusMaxDate.MaxOfEffectiveDate = tblProposedStatus.EffectiveDate)"
strSQL = strSQL & " GROUP BY jobCodes.[Job Code], jobCodes.jobtitle, tblJobFunction.JobFunction, tblJobFunction.[Long Name], tblJobFamily.[Job Family], tblJobFamily.Descr, jobCodes.Headcount, jobCodes.Grade, tblProposedGrade.ProposedGrade, tblUsers.Name, tblProposedGrade.EffectiveDate, jobCodes.Status, tblProposedStatus.ProposedStatus, jobCodes.[Comp Plan]"

strSQL = strSQL & " " & Form_frmJobViewSub.formFilter


strQDF = "_TempQuery_"
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
qdfTemp.Close
Set qdfTemp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strQDF, "C:\Documents and Settings\ckell52\My Documents\CompAnalysisTool_JobExtract.xls"
dbs.QueryDefs.Delete strQDF
dbs.Close
Set dbs = Nothing

End Sub


The error occurs at this line...
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
 

dcb

Normally Lost
Local time
Today, 15:35
Joined
Sep 15, 2009
Messages
529
Every time you run it you create a query - as the query already exists in the DB the err is being thrown back...
 

vbaInet

AWF VIP
Local time
Today, 14:35
Joined
Jan 22, 2010
Messages
26,374
I'm stumped on this error message. I modified this code from samples provided on another website and had things working fine.

Do not recall making any changes to the code, but now it is not working. No references to Run-time Erro 3012 anywhere.

Private Sub cmdExportToExcel_Click()
Dim dbs As DAO.Database
Dim qdfTemp As DAO.QueryDef
Dim strSQL As String, strQDF As String
Set dbs = CurrentDb
strSQL = "SELECT jobCodes.[Job Code], jobCodes.jobtitle, tblJobFunction.JobFunction AS JobFamilyCode, tblJobFunction.[Long Name] AS JobFamily, tblJobFamily.[Job Family] AS FuncitonCode, tblJobFamily.Descr AS Function, jobCodes.Headcount, jobCodes.Grade, tblProposedGrade.ProposedGrade, tblUsers.Name, tblProposedGrade.EffectiveDate, jobCodes.Status, tblProposedStatus.ProposedStatus, jobCodes.[Comp Plan]"
strSQL = strSQL & " FROM ((((((tblJobFamily RIGHT JOIN jobCodes ON tblJobFamily.[Job Family] = jobCodes.[Job Family]) INNER JOIN qryProposedJobGradeMaxDate ON jobCodes.[Job Code] = qryProposedJobGradeMaxDate.JobCode) INNER JOIN tblProposedGrade ON (qryProposedJobGradeMaxDate.JobCode = tblProposedGrade.JobCode) AND (qryProposedJobGradeMaxDate.MaxOfEffectiveDate = tblProposedGrade.EffectiveDate)) INNER JOIN tblUsers ON tblProposedGrade.UserID = tblUsers.UserID) LEFT JOIN tblJobFunction ON jobCodes.[Job Funct] = tblJobFunction.JobFunction) LEFT JOIN qryProposedStatusMaxDate ON jobCodes.[Job Code] = qryProposedStatusMaxDate.JobCode) LEFT JOIN tblProposedStatus ON (qryProposedStatusMaxDate.JobCode = tblProposedStatus.JobCode) AND (qryProposedStatusMaxDate.MaxOfEffectiveDate = tblProposedStatus.EffectiveDate)"
strSQL = strSQL & " GROUP BY jobCodes.[Job Code], jobCodes.jobtitle, tblJobFunction.JobFunction, tblJobFunction.[Long Name], tblJobFamily.[Job Family], tblJobFamily.Descr, jobCodes.Headcount, jobCodes.Grade, tblProposedGrade.ProposedGrade, tblUsers.Name, tblProposedGrade.EffectiveDate, jobCodes.Status, tblProposedStatus.ProposedStatus, jobCodes.[Comp Plan]"

strSQL = strSQL & " " & Form_frmJobViewSub.formFilter


strQDF = "_TempQuery_"
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
qdfTemp.Close
Set qdfTemp = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
strQDF, "C:\Documents and Settings\ckell52\My Documents\CompAnalysisTool_JobExtract.xls"
dbs.QueryDefs.Delete strQDF
dbs.Close
Set dbs = Nothing

End Sub


The error occurs at this line...
Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)


That line creates a new query called strQDF. If that query already exists it will throw up an error if you try to create it again.
 

ckell23

New member
Local time
Today, 08:35
Joined
Jan 26, 2010
Messages
6
Makes sense. There is a line of code at the end that deletes that query, but there must have been a problem with the path name which caused the code to stop before finishing out the last steps.

Got it going now.

Thanks for the ultra quick replies!
 

aparnag

Registered User.
Local time
Today, 08:35
Joined
Apr 19, 2010
Messages
27
can u clarify how u solved it, as i am gettin the same 3012 err.
Can anyone tell me what it is and how to solve it.
If I dont name my query the code works but as soon as I put a name it gives the error
sqltxt = " SELECT [M&I Data].[Acct+Note No], [M&I Data].Balance, [M&I Data].[Maturity Date] FROM [M&I Data];"
Set qry = db.CreateQueryDef("", sqltxt)

It works but if I name the query it gives me error..

Really need help
Thanks in advance
AG
P.S:- I am newbie to access and vba
 

Users who are viewing this thread

Top Bottom