I am trying to export a query held within a table as T-SQL out to excel.
The idea is to have admins write the Queries, which will be exported by other personnel.
The View and Table do create correctly as I can see them within ssms, however I get a runtime error 7874 - cannot find object.
The only thing i can think of is that access is lagging behind sql server, and the database window is not being refreshed.
This is an ADP file running on Access 2007 on a SQL Server 2008 Standard Server. -
DoCmd.SetWarnings Warningsoff
-
Dim strsql As String
-
Dim strsql2 As String
-
Dim strexport As String
-
Dim strexcel As String
-
Dim str As String
-
-
'set the name for the view and the exported file
-
strsql2 = Me.txtEXPName & " - " & ENVIRON("USERNAME") & DatePart("d", Date) & DatePart("m", Date) & DatePart("yyyy", Date)
-
strsql = Me.txtEXPName & ENVIRON("USERNAME")
-
strexport = Me.txtEXPName & " - " & DatePart("d", Date) & "-" & DatePart("m", Date) & "-" & DatePart("yyyy", Date)
-
strexcel = "C:\Business Support\Exports\" & Me.txtEXPName & " - " & DatePart("d", Date) & "-" & DatePart("m", Date) & "-" & DatePart("yyyy", Date) & ".xls"
-
-
'determine if the Folder exists
-
If FolderExists("C:\Business Support\Exports") = False Then
-
MkDir "C:\Business Support\Exports"
-
Else
-
End If
-
-
'determine if the output file already exists
-
If FileExists(strexcel) = True Then
-
MsgBox "The Output File " & strexcel & vbCrLf & "Already exists" & vbCrLf & vbCrLf & "Please rename or delete the file before proceeding", vbCritical, "File exists"
-
Exit Sub
-
Else
-
End If
-
-
'If the view exists on the server, delete it
-
CurrentProject.Connection.Execute "IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[" & strsql & "]')) DROP VIEW [dbo].[" & strsql & "]"
-
CurrentProject.Connection.Execute "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & strsql2 & "]')) DROP TABLE [dbo].[" & strsql2 & "]"
-
-
'Create view on the server
-
CurrentProject.Connection.Execute "CREATE VIEW " & strsql & " AS " & Me.txtExport & ""
-
-
CurrentProject.Connection.Execute "SELECT * INTO " & strsql2 & " FROM " & strsql & ""
-
-
CurrentProject.Application.RefreshDatabaseWindow
-
-
'Transfer the data to MS 2003 Compatible Excel File
-
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strsql2, strexcel, True
-
-
'delete the view from the server
-
CurrentProject.Connection.Execute "DROP VIEW " & strsql & ""
-
CurrentProject.Connection.Execute "DROP Table " & strsql2 & ""
-
CurrentProject.Application.RefreshDatabaseWindow
-
DoCmd.SetWarnings Warningson
-
-
MsgBox "Export Complete" & vbCrLf & vbCrLf & "File is located at the following location" & vbCrLf & strexcel & vbCrLf & vbCrLf & "Press OK to open the file", vbOKOnly, "Export Completed"
-
-
'open the excel application
-
Dim xlApp As Excel.Application
-
Dim xlWB As Excel.Workbook
-
Set xlApp = New Excel.Application
-
With xlApp
-
Set xlWB = .Workbooks.Open(strexcel, , False)
-
.Visible = True
-
Rows("1:1").Select
-
Selection.Font.Bold = True
-
With Selection.Font
-
.ThemeColor = xlThemeColorDark1
-
.TintAndShade = -0.249977111117893
-
End With
-
With Selection.Font
-
.ColorIndex = xlAutomatic
-
.TintAndShade = 0
-
End With
-
With Selection.Interior
-
.Pattern = xlSolid
-
.PatternColorIndex = xlAutomatic
-
.ThemeColor = xlThemeColorDark1
-
.TintAndShade = -0.149998474074526
-
.PatternTintAndShade = 0
-
End With
-
Cells.Select
-
Selection.Columns.AutoFit
-
Range("A1").Select
-
With ActiveWindow
-
.SplitColumn = 0
-
.SplitRow = 1
-
End With
-
ActiveWindow.FreezePanes = True
-
ActiveWorkbook.Save
-
End With
-
Presuming you are using an ODBC connection to your SQL-server back end it would be worth reducing the ODBC refresh interval for the database. Although the Help entry for this setting is concerned with data entry form refreshing when records are changed by other users in a multi-user environment, I reckon it could be that your database needs to refresh its ODBC status at more frequent intervals. At least it will eliminate another potential problem from consideration.
-Stewart
15 2870 - It's probably a dumb question, but I must ask it, do you have a Reference set to the Microsoft Excel Object Library?
- Have you tried to set a Breakpoint then Single-Step through the code to see exactly where the Error is being generated?
Hiya,
not such thing as a dumb question :)
I have the excel library referenced.
I have set a breakpoint, and the error is coming up (occasionally) at the docmd.transferspreadsheet line.
If i run the script again (after the error), the spreadsheet is produced.
Cheers and Thanks
Leon
Hi. I'm sure this is nothing to do with Access as such; it is the Excel automation that is causing the problem. Reason is that you are referring implicitly in many locations to the current active worksheet object (lines 56 through 81). This works when running code in Excel, as the currently-active worksheet or workbook is taken for the implicit references. It does not work reliably when running code referencing the Excel object from outside of Excel itself.
You MUST refer explicitly to the automation server object whenever you want to set or refer to a range or selection. You have a WITH in line 53 which will allow you to do this without referring to xlApp all the time, but you then do not use the properties of that object when referring to selections, rows etc (lines 56 onwards).
For example, lines 56 to 61 can be replaced as follows: - with .activesheet.Rows("1:1").Font
-
.Bold = True
-
.ThemeColor = xlThemeColorDark1
-
.TintAndShade = -0.249977111117893
-
End With
-
(In this example I have also done away with the use of the Selection method - its use slows down code running in an automation server).
To summarise, you must refer to the xlApp object explicitly throughout. Use With to help you in this, but when you do make sure you use the correct properties of the xlApp object - its ActiveWorkbook, ActiveSheet, Workbooks() etc properties - whenever you access ranges or range properties.
There are a number of threads dealing with the errors which arise when trying to use implicit references. This thread linked here gives more background on the reason for what can be very frustrating errors experienced with code running in Access that apparently works OK in Excel itself.
-Stewart
Really nice pickup, Stewart!
Thanks for your comment, ADezii!
Echidna, I should also have mentioned that you have not explicitly saved the file and closed Excel at the end of your routine. This may be why you are experiencing an error on the TransferSpreadsheet method rather than on the Excel components (which I can assure you will happen at some time even if you haven't experienced it so far!).
Unless you close Excel explicitly, on each occasion you run the routine you will create a new instance of Excel which will remain active until you physically close it, along with the file you opened. Should the name of the file still open be the same for whatever reason as the one you are passing to TransferSpreadsheet you will get a transfer failure, as you cannot save a file under a name in use by another application at the same time.
-Stewart
Many Thanks guys :) will give it a go this morning :)
Did not think of the excel automation causing an issue...
I recorded a macro and copied the VB straight into Access.
Cheers
Leon
I have changed the code within the excel formatting, but the issue - error 7874 is still there.
I commented out the excel formatting, and the problem still exists.
It seems that the table exists on the server, however, there seems to be a delay in the Database window being populated with the Table created.
Cheers
Leon
Presuming you are using an ODBC connection to your SQL-server back end it would be worth reducing the ODBC refresh interval for the database. Although the Help entry for this setting is concerned with data entry form refreshing when records are changed by other users in a multi-user environment, I reckon it could be that your database needs to refresh its ODBC status at more frequent intervals. At least it will eliminate another potential problem from consideration.
-Stewart
Hi, sorry for not responding in a while, I have been given another priority one project for the past couple of weeks.
I set the odbc refresh interval to one second and it works a charm now... many thanks :)
Cheers
Leon
NeoPa 32,556
Expert Mod 16PB
Leon,
I'm sure you have the right solution there. However, I'd be careful about using a value as short as one second. This could be creating quite a lot of traffic on your networks. I'd advise playing with values nearer to your original setting until you find it works reliably for you.
Thanks for the tip :)
the advantage of having a standalone testing environment :)
It was set originally to 5 seconds (odbc default), i am thinking of taking the original idea of 'holding' the application for 5 seconds to allow the odbc to refresh itself, but everything i try does not work (short of killing the connection and re-establishing the link).
Is there some way within an adp of determining if the created Table exists?
Cheers
Leon
NeoPa 32,556
Expert Mod 16PB @Echidna
I don't do any work in ADPs myself, but would you not have the TableDefs collection available. That can tell you all the tables available within the CurrentDb.
Hiya,
Got it to work... finally :)
One of the VFP developers suggested a "try catch", which got me thinking :)
Here is what i came up with -
CurrentProject.Application.RefreshDatabaseWindow
-
-
Me.txtdisplay = "Processing..."
-
Screen.MousePointer = 11
-
gcdate = Now()
-
Do While Now() < gcdate + 0.00003
-
DoEvents
-
Loop
-
Screen.MousePointer = 1
-
Me.txtdisplay = ""
-
It gives the application time to refresh the window without the need to reduce the odbc connection attributes.
Cheers, and thanks for all the help!!
Leon
NeoPa 32,556
Expert Mod 16PB
Nice work Leon.
An alternative approach (only whole seconds supported mind) would be to use : - Do While Now() < GCDate + #0:0:3#
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Chuck |
last post by:
Here is my setup.
Netgear Router with a webserver and database server NAT'd behind the
firewall.
Microsoft Windows 2000, IIS 5 - Web Server
Microsoft Windows 2000, MySQL - Database Server
...
|
by: Mike MacSween |
last post by:
Sorry if this is a bit off topic, but I can't seem to find a 'remote
control' newsgroup on my news server. And anyway I know Tony has some
experience of this.
The app is split FE/BE. I'd like...
|
by: Raphi |
last post by:
Hi,
I've been using an Access application I wrote for an office with the
front-end stored on all computers and the back-end on one of them serving as
an Access file server.
Now we're moving...
|
by: Ananda Sim |
last post by:
For those who haven't RSSed the blog:
http://blogs.msdn.com/access/archive/2005/10/13/480870.aspx
|
by: Greg Linwood |
last post by:
How does one access HTML elements from code-behind?
I've got a mix of asp:Labels & <font> elements - I'd like to continue using
<font> elements, but be able to access them from code-behind to set...
|
by: William Parker |
last post by:
I have a web control I made called header.ascx. It has its own properties
and methods I defined. But I cannot figure out how to access this control
from my code behind page.
I can create the...
|
by: N. Demos |
last post by:
I have a user control with code behind of which two instances are
created/declared in my aspx page. The aspx page has code behind also, as
I need to access methods of the usercontrols on page...
|
by: riscy |
last post by:
I'm having odd behaviour of VS 2003 pro recently. It happen when Dell
9200 returned from repairs with new motherboard to fix audio out
socket problem (as they say). I installed Visual Assist X and...
|
by: Hank |
last post by:
We have just recently migrated the data from our Access 2000
backend to Postgres. All forms and reports seem to run correctly but,
in many cases, very slowly. We do not want to switch over until...
|
by: MadTurtle |
last post by:
The basic scenario:
I have a tab control that is really just a Datalist which contains an ItemTemplate and SelectedItemTemplate. A LinkButton is contained in each of these templates with a CSS...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |