473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Access Lagging behind SQL Server

53
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.

Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings Warningsoff
  2. Dim strsql As String
  3. Dim strsql2 As String
  4. Dim strexport As String
  5. Dim strexcel As String
  6. Dim str As String
  7.  
  8. 'set the name for the view and the exported file
  9. strsql2 = Me.txtEXPName & " - " & ENVIRON("USERNAME") & DatePart("d", Date) & DatePart("m", Date) & DatePart("yyyy", Date)
  10. strsql = Me.txtEXPName & ENVIRON("USERNAME")
  11. strexport = Me.txtEXPName & " - " & DatePart("d", Date) & "-" & DatePart("m", Date) & "-" & DatePart("yyyy", Date)
  12. strexcel = "C:\Business Support\Exports\" & Me.txtEXPName & " - " & DatePart("d", Date) & "-" & DatePart("m", Date) & "-" & DatePart("yyyy", Date) & ".xls"
  13.  
  14. 'determine if the Folder exists
  15. If FolderExists("C:\Business Support\Exports") = False Then
  16. MkDir "C:\Business Support\Exports"
  17. Else
  18. End If
  19.  
  20. 'determine if the output file already exists
  21. If FileExists(strexcel) = True Then
  22. MsgBox "The Output File " & strexcel & vbCrLf & "Already exists" & vbCrLf & vbCrLf & "Please rename or delete the file before proceeding", vbCritical, "File exists"
  23. Exit Sub
  24. Else
  25. End If
  26.  
  27. 'If the view exists on the server, delete it
  28. CurrentProject.Connection.Execute "IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[" & strsql & "]')) DROP VIEW [dbo].[" & strsql & "]"
  29. CurrentProject.Connection.Execute "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" & strsql2 & "]')) DROP TABLE [dbo].[" & strsql2 & "]"
  30.  
  31. 'Create view on the server
  32. CurrentProject.Connection.Execute "CREATE VIEW " & strsql & " AS " & Me.txtExport & ""
  33.  
  34. CurrentProject.Connection.Execute "SELECT * INTO " & strsql2 & " FROM " & strsql & ""
  35.  
  36. CurrentProject.Application.RefreshDatabaseWindow
  37.  
  38. 'Transfer the data to MS 2003 Compatible Excel File
  39. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strsql2, strexcel, True
  40.  
  41. 'delete the view from the server
  42. CurrentProject.Connection.Execute "DROP VIEW " & strsql & ""
  43. CurrentProject.Connection.Execute "DROP Table " & strsql2 & ""
  44. CurrentProject.Application.RefreshDatabaseWindow
  45. DoCmd.SetWarnings Warningson
  46.  
  47. 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"
  48.  
  49. 'open the excel application
  50.     Dim xlApp As Excel.Application
  51.     Dim xlWB As Excel.Workbook
  52.     Set xlApp = New Excel.Application
  53.     With xlApp
  54.        Set xlWB = .Workbooks.Open(strexcel, , False)
  55.     .Visible = True
  56.     Rows("1:1").Select
  57.     Selection.Font.Bold = True
  58.     With Selection.Font
  59.         .ThemeColor = xlThemeColorDark1
  60.         .TintAndShade = -0.249977111117893
  61.     End With
  62.     With Selection.Font
  63.         .ColorIndex = xlAutomatic
  64.         .TintAndShade = 0
  65.     End With
  66.     With Selection.Interior
  67.         .Pattern = xlSolid
  68.         .PatternColorIndex = xlAutomatic
  69.         .ThemeColor = xlThemeColorDark1
  70.         .TintAndShade = -0.149998474074526
  71.         .PatternTintAndShade = 0
  72.     End With
  73.     Cells.Select
  74.     Selection.Columns.AutoFit
  75.     Range("A1").Select
  76.     With ActiveWindow
  77.         .SplitColumn = 0
  78.         .SplitRow = 1
  79.     End With
  80.     ActiveWindow.FreezePanes = True
  81.     ActiveWorkbook.Save
  82.     End With
  83.  
Mar 4 '10 #1

✓ answered by Stewart Ross

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
ADezii
8,834 Expert 8TB
  1. It's probably a dumb question, but I must ask it, do you have a Reference set to the Microsoft Excel Object Library?
  2. Have you tried to set a Breakpoint then Single-Step through the code to see exactly where the Error is being generated?
Mar 4 '10 #2
Echidna
53
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
Mar 4 '10 #3
Stewart Ross
2,545 Expert Mod 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. with .activesheet.Rows("1:1").Font 
  2.         .Bold = True 
  3.         .ThemeColor = xlThemeColorDark1 
  4.         .TintAndShade = -0.249977111117893 
  5. End With 
  6.  
(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
Mar 4 '10 #4
ADezii
8,834 Expert 8TB
Really nice pickup, Stewart!
Mar 4 '10 #5
Stewart Ross
2,545 Expert Mod 2GB
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
Mar 4 '10 #6
NeoPa
32,556 Expert Mod 16PB
Some useful basics and some sample code for Application Automation.
Mar 4 '10 #7
Echidna
53
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
Mar 5 '10 #8
Echidna
53
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
Mar 5 '10 #9
Stewart Ross
2,545 Expert Mod 2GB
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
Mar 5 '10 #10
Echidna
53
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
Mar 16 '10 #11
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.
Mar 16 '10 #12
Echidna
53
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
Mar 16 '10 #13
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.
Mar 16 '10 #14
Echidna
53
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

Expand|Select|Wrap|Line Numbers
  1. CurrentProject.Application.RefreshDatabaseWindow
  2.  
  3.     Me.txtdisplay = "Processing..."
  4.     Screen.MousePointer = 11
  5.         gcdate = Now()
  6.         Do While Now() < gcdate + 0.00003
  7.             DoEvents
  8.         Loop
  9.     Screen.MousePointer = 1
  10.     Me.txtdisplay = ""
  11.  
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
Mar 16 '10 #15
NeoPa
32,556 Expert Mod 16PB
Nice work Leon.

An alternative approach (only whole seconds supported mind) would be to use :
Expand|Select|Wrap|Line Numbers
  1. Do While Now() < GCDate + #0:0:3#
Mar 16 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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 ...
12
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...
56
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...
7
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
2
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...
6
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...
2
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...
1
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...
10
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...
1
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...
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.