QueryDef Object Variable/With Block Error

Pisteuo

Registered User.
Local time
Today, 08:57
Joined
Jul 12, 2009
Messages
72
In the below code I am receiving an "object variable or With block variable not set" error at this ErrHandling line:

Set qryCheckTable = .CreateQueryDef("", CheckTableSQL)

The code body works fine. Only the error handling is broken. Any suggestions? Thank you.
Full Code:
Dim db As DAO.Database

Dim DeleteBlanksSQL As String
Dim DeleteDuplicatesSQL As String
Dim AppendSQL As String
Dim DeleteImportSQL As String

Dim DeleteBlanks As DAO.QueryDef
Dim DeleteDuplicates As DAO.QueryDef
Dim Append As DAO.QueryDef
Dim DeleteImport As DAO.QueryDef

Set db = CurrentDb()

DeleteBlanksSQL = "DELETE * FROM tblImport WHERE IsNull([Project ID]);"

DeleteDuplicatesSQL = "DELETE tblForecast.fldPrimary FROM tblForecast" & _
" WHERE [fldPrimary] IN (SELECT fldPrimary FROM qryJoinDuplicates);"

AppendSQL = "INSERT INTO tblForecast" & _
" SELECT [Project ID] AS fldID, [Package ID] AS fldPackageID," & _
" [Title of Change] AS fldTitle, [Change Description] AS fldDescription," & _
" [Status] AS fldStatus, [Contract Change Type] AS fldContractType," & _
" [DCF Change Type] AS fldDCFType, [Scope Transfer] AS fldTransfer," & _
" [PCR No] AS fldPCR, [DCF Mod No] AS fldDCFModNo, [Delay Related] AS fldDelay," & _
" [Value] AS fldValue, [Worst] AS fldWorst, [Most] AS fldMost, [Best] AS fldBest," & _
" [Assumption] AS fldAssumption, [Trend No] AS fldTrendNo, [Cont Usage No] AS fldContNo" & _
" FROM tblImport;"

DeleteImportSQL = "DELETE * FROM tblImport;"

Set DeleteBlanks = db.CreateQueryDef("", DeleteBlanksSQL)
DeleteBlanks.Execute
DeleteBlanks.Close

Set DeleteDuplicates = db.CreateQueryDef("", DeleteDuplicatesSQL)
DeleteDuplicates.Execute
DeleteDuplicates.Close

Set Append = db.CreateQueryDef("", AppendSQL)
Append.Execute
Append.Close

Set DeleteImport = db.CreateQueryDef("", DeleteImportSQL)
DeleteImport.Execute
DeleteImport.Close

db.Close

Exit_ErrHandler:
Exit Sub

ErrHandler:

With db

Dim DeleteImportError As DAO.QueryDef
Dim qryCheckTable As DAO.QueryDef
Dim rstCheckTable As DAO.Recordset
Dim DeleteImportErrorSQL As String
Dim CheckTableSQL


DeleteImportErrorSQL = "DELETE * FROM tblImport;"
CheckTableSQL = "SELECT * FROM tblImport;"

Set qryCheckTable = .CreateQueryDef("", CheckTableSQL)
Set rstCheckTable = qryCheckTable.OpenRecordset

If rstCheckTable.RecordCount > 0 Then
Set DeleteImportError = .CreateQueryDef("", DeleteImportErrorSQL)
DeleteImportError.Execute
DeleteImportError.Close

Else

MsgBox (" An Error Occurred." & vbCrLf & _
" Research error than re-import." & vbCrLf & _
"Ensure package files are not duplicated.")
End If

.Close

End With

Resume Exit_ErrHandler

End Sub
 
Are you the author of the code?
Where's the entire code? Ensure that it's properly indented. The one you posted wasn't.
 
Here is full code (problem code is in bold). The indents are not posting.

Private Sub btnImportForecast_Click()

On Error GoTo ErrHandler

Dim strFileName As String
Dim strTableName As String
Dim strWorksheet As String

strFileName = Dir("Z:\Projects\Kusile Folder\Project Control\CCFS\*.xlsm")
strTableName = "tblImportForecast"
strWorksheet = "Forecast!A:R"

Do Until strFileName = ""

DoCmd.TransferSpreadsheet acImportForecast, acSpreadsheetTypeExcel9, strTableName, _
"Z:\Projects\Kusile Folder\Project Control\CCFS\" & strFileName, True, strWorksheet

strFileName = Dir()

Loop


Dim db As DAO.Database

Dim DeleteBlanksSQL As String
Dim DeleteDuplicatesSQL As String
Dim AppendSQL As String
Dim DeleteImportForecastSQL As String

Dim DeleteBlanks As DAO.QueryDef
Dim DeleteDuplicates As DAO.QueryDef
Dim Append As DAO.QueryDef
Dim DeleteImportForecast As DAO.QueryDef

Set db = CurrentDb()

DeleteBlanksSQL = "DELETE * FROM tblImportForecast WHERE IsNull([Project ID]);"

DeleteDuplicatesSQL = "DELETE tblForecast.fldPrimary FROM tblForecast" & _
" WHERE [fldPrimary] IN (SELECT fldPrimary FROM qryJoinForecastDuplicates);"

AppendSQL = "INSERT INTO tblForecast" & _
" SELECT [Project ID] AS fldID, [Package ID] AS fldPackageID," & _
" [Title of Change] AS fldTitle, [Change Description] AS fldDescription," & _
" [Status] AS fldStatus, [Contract Change Type] AS fldContractType," & _
" [DCF Change Type] AS fldDCFType, [Scope Transfer] AS fldTransfer," & _
" [PCR No] AS fldPCR, [DCF Mod No] AS fldDCFModNo, [Delay Related] AS fldDelay," & _
" [Value] AS fldValue, [Worst] AS fldWorst, [Most] AS fldMost, [Best] AS fldBest," & _
" [Assumption] AS fldAssumption, [Trend No] AS fldTrendNo, [Cont Usage No] AS fldContNo" & _
" FROM tblImportForecast;"

DeleteImportForecastSQL = "DELETE * FROM tblImportForecast;"

Set DeleteBlanks = db.CreateQueryDef("", DeleteBlanksSQL)
DeleteBlanks.Execute
DeleteBlanks.Close

Set DeleteDuplicates = db.CreateQueryDef("", DeleteDuplicatesSQL)
DeleteDuplicates.Execute
DeleteDuplicates.Close

Set Append = db.CreateQueryDef("", AppendSQL)
Append.Execute
Append.Close

Set DeleteImportForecast = db.CreateQueryDef("", DeleteImportForecastSQL)
DeleteImportForecast.Execute
DeleteImportForecast.Close

db.Close

Exit_ErrHandler:
Exit Sub

ErrHandler:

With db

Dim DeleteImportForecastError As DAO.QueryDef
Dim qryCheckTable As DAO.QueryDef
Dim rstCheckTable As DAO.Recordset
Dim DeleteImportForecastErrorSQL As String
Dim CheckTableSQL


DeleteImportForecastErrorSQL = "DELETE * FROM tblImportForecast;"
CheckTableSQL = "SELECT * FROM tblImportForecast;"

Set qryCheckTable = .CreateQueryDef("", CheckTableSQL)
Set rstCheckTable = qryCheckTable.OpenRecordset

If rstCheckTable.RecordCount > 0 Then
Set DeleteImportForecastError = .CreateQueryDef("", DeleteImportForecastErrorSQL)
DeleteImportForecastError.Execute
DeleteImportForecastError.Close

Else

MsgBox (" An Error Occurred." & vbCrLf & _
" Research error than re-ImportForecast." & vbCrLf & _
"Ensure package files are not duplicated.")
End If

.Close

End With

Resume Exit_ErrHandler

End Sub
 
I believe I asked more than one question Pisteuo. Are you the author of the code or not?
 
I apologize. Yes, I put the code together.
 
The only reason why I asked is so that I know your level of knowledge of coding.

What happens when you comment out the On Error Go To code line?
 
Quick note: DoCmd.TransferSpreadsheet should be followed by acImport. I made some global changes before posting the code in the thread. I apologize for not scrubbing the published code.

Back to business:
The code executes as planned when I comment out the error handler. And it executes fine when errors do not occur. However the code errors when I introduce the error environment - attempts to transfer duplicate records via docmd.transferspreadsheet into a no-duplicate indexed table.

I am attempting to run an error handler after the Access warning that duplicate records were deleted during the import.
 
Alright the error is thrown before the db object is set. So set the db object in the error handler as well.
 
That worked. I declared my database only once and set the object twice. Thank you!
 

Users who are viewing this thread

Back
Top Bottom