J
John Pritchard
Hi,
I'm getting an intermittent problem with the following
code:-
Sub CheckQry()
Dim i As Integer
With dbscredan
For i = 0 To .QueryDefs.Count - 1
If .QueryDefs(i).Name = "MainQry" Then
DoCmd.DeleteObject acQuery, .QueryDefs
(i).Name
End If
Next i
End With
End Sub
This works correctly most of the time but occasionally
fails with message :
7874 Unable to find object... on the DoCmd statement. The
dbscredan is a DAO connection. I'm using Access 2000
(9.0.3821) and can't find any reason for the problem.
The process I'm running is designed to dynamically create
a query from data in an excel spreadsheet. I was hoping to
allow users to call the Getdata process any number of
times in an access session - currently I limit them to one
load per session to avoid this problem. The code is quite
simple and is given below - Any help would be much
appreciated as this is my first Access application and I'd
like it to work well.
Thanks for any help
John P.
ps Full Code:-
' Global declaration
Dim dbscredan As DAO.Database
Function GetData(ByVal FullPath As String) As Boolean
Dim Centre As String
Dim TableName As String
Dim i As Integer
Dim qrystr As String
Dim xlapp As Excel.Application
On Error GoTo ErrorHandler
'Open database and workbook
Set dbscredan = DBEngine.Workspaces(0).Databases(0)
' Open Excel Application
Set xlapp = New Excel.Application
xlapp.Workbooks.Open filename:=FullPath
For i = 1 To xlapp.Worksheets.Count
Centre = xlapp.Worksheets(i).Name
TableName = Centre & "tbl"
' Check table exists if does delete it
Call CheckTable(TableName)
' Create the table for this tab (centre) with a
link back to the excel sheet
DoCmd.TransferSpreadsheet acLink,
acSpreadsheetTypeExcel9, TableName, _
FullPath, -1, Centre & "!"
' dynamically create sql string to place in main
qry
If i < xlapp.Worksheets.Count Then
qrystr = qrystr & "SELECT " & """" & Centre
& """" & " as centre,* FROM " & TableName & _
" Where Amount Is Not Null And batch Is Not
Null Union ALL "
Else
qrystr = qrystr & "SELECT " & """" & Centre
& """" & " as centre,* FROM " & TableName & _
" Where Amount Is Not Null And batch Is Not
Null;"
End If
Next i
' check if main query exists if so delete it
Call CheckQry
' create main qry
Call CreateMainQry(qrystr)
' Close workbook - and Excel application
xlapp.ActiveWorkbook.Close
xlapp.Quit
Set xlapp = Nothing
GetData = True
Exit Function
ErrorHandler:
Select Case Err.Number
Case 1004
MsgBox "File " & FullPath & " can not be found
or can not be accessed."
Case Else
MsgBox Err.Number & " : " & Err.Description
End Select
GetData = False
End Function
Sub CreateMainQry(ByVal qrystr As String)
Dim MainQry As QueryDef
With dbscredan
Set MainQry = .CreateQueryDef("MainQry", qrystr)
End With
End Sub
Sub CheckTable(ByVal TableName As String)
Dim i As Integer
With dbscredan
For i = 0 To .TableDefs.Count - 1
If .TableDefs(i).Name = TableName Then
DoCmd.DeleteObject acTable, TableName
End If
Next i
End With
End Sub
Sub CheckQry()
Dim i As Integer
With dbscredan
For i = 0 To .QueryDefs.Count - 1
If .QueryDefs(i).Name = "MainQry" Then
DoCmd.DeleteObject acQuery, .QueryDefs
(i).Name
End If
Next i
End With
End Sub
I'm getting an intermittent problem with the following
code:-
Sub CheckQry()
Dim i As Integer
With dbscredan
For i = 0 To .QueryDefs.Count - 1
If .QueryDefs(i).Name = "MainQry" Then
DoCmd.DeleteObject acQuery, .QueryDefs
(i).Name
End If
Next i
End With
End Sub
This works correctly most of the time but occasionally
fails with message :
7874 Unable to find object... on the DoCmd statement. The
dbscredan is a DAO connection. I'm using Access 2000
(9.0.3821) and can't find any reason for the problem.
The process I'm running is designed to dynamically create
a query from data in an excel spreadsheet. I was hoping to
allow users to call the Getdata process any number of
times in an access session - currently I limit them to one
load per session to avoid this problem. The code is quite
simple and is given below - Any help would be much
appreciated as this is my first Access application and I'd
like it to work well.
Thanks for any help
John P.
ps Full Code:-
' Global declaration
Dim dbscredan As DAO.Database
Function GetData(ByVal FullPath As String) As Boolean
Dim Centre As String
Dim TableName As String
Dim i As Integer
Dim qrystr As String
Dim xlapp As Excel.Application
On Error GoTo ErrorHandler
'Open database and workbook
Set dbscredan = DBEngine.Workspaces(0).Databases(0)
' Open Excel Application
Set xlapp = New Excel.Application
xlapp.Workbooks.Open filename:=FullPath
For i = 1 To xlapp.Worksheets.Count
Centre = xlapp.Worksheets(i).Name
TableName = Centre & "tbl"
' Check table exists if does delete it
Call CheckTable(TableName)
' Create the table for this tab (centre) with a
link back to the excel sheet
DoCmd.TransferSpreadsheet acLink,
acSpreadsheetTypeExcel9, TableName, _
FullPath, -1, Centre & "!"
' dynamically create sql string to place in main
qry
If i < xlapp.Worksheets.Count Then
qrystr = qrystr & "SELECT " & """" & Centre
& """" & " as centre,* FROM " & TableName & _
" Where Amount Is Not Null And batch Is Not
Null Union ALL "
Else
qrystr = qrystr & "SELECT " & """" & Centre
& """" & " as centre,* FROM " & TableName & _
" Where Amount Is Not Null And batch Is Not
Null;"
End If
Next i
' check if main query exists if so delete it
Call CheckQry
' create main qry
Call CreateMainQry(qrystr)
' Close workbook - and Excel application
xlapp.ActiveWorkbook.Close
xlapp.Quit
Set xlapp = Nothing
GetData = True
Exit Function
ErrorHandler:
Select Case Err.Number
Case 1004
MsgBox "File " & FullPath & " can not be found
or can not be accessed."
Case Else
MsgBox Err.Number & " : " & Err.Description
End Select
GetData = False
End Function
Sub CreateMainQry(ByVal qrystr As String)
Dim MainQry As QueryDef
With dbscredan
Set MainQry = .CreateQueryDef("MainQry", qrystr)
End With
End Sub
Sub CheckTable(ByVal TableName As String)
Dim i As Integer
With dbscredan
For i = 0 To .TableDefs.Count - 1
If .TableDefs(i).Name = TableName Then
DoCmd.DeleteObject acTable, TableName
End If
Next i
End With
End Sub
Sub CheckQry()
Dim i As Integer
With dbscredan
For i = 0 To .QueryDefs.Count - 1
If .QueryDefs(i).Name = "MainQry" Then
DoCmd.DeleteObject acQuery, .QueryDefs
(i).Name
End If
Next i
End With
End Sub