473,786 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking Excel to Access Query

31 New Member
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query and it seems to me that Excel is far easier to create graphs than Access.

When I use the Get External Data feature of Excel and the dialog box with the tables and queries appears, the query I need is not listed. Is this because the query has calculated fields or some other reason? I can find the underlying table but then the data is not filtered and of course, there are no calculated fields.

I believe connections can be made using vba but I have no idea of how to do this.

This is my sql for the query if that helps.


Expand|Select|Wrap|Line Numbers
  1. SELECT tblStaffPerformanceStats.SRID, tblStaffPerformanceStats.RYear, tblStaffPerformanceStats.StaffCode, tblStaffPerformanceStats.PercentageRate, tblStaffPerformanceStats.ChargeRate, tblStaffPerformanceStats.WkDays, tblStaffPerformanceStats.MaxBillableHrs, tblStaffPerformanceStats.ActualBillableRevenue, tblStaffPerformanceStats.ActualBillableHrs, tblStaffPerformanceStats.HrsWrittenOff, tblStaffPerformanceStats.HrsWrittenBack, tblStaffPerformanceStats.HrsNonAttendance, tblStaffPerformanceStats.HrsOfficeAdmin, tblStaffPerformanceStats.HrsProfessionalDev, tblStaffPerformanceStats.HrsFirmDev, [WkDays]*DLookUp("[DailyAttendHrs] ","[tblStaff]","[SCode]  = '" & [Forms]![afmStaffReviewCentre]![cboLastname] & "'") AS MaxAvailableHrs, ([WkDays]*DLookUp("[DailyAttendHrs] ","[tblStaff]","[SCode]  = '" & [Forms]![afmStaffReviewCentre]![cboLastname] & "'"))*[ChargeRate] AS [Max$], [MaxBillableHrs]*[PercentageRate] AS TargetedHrs, Round(([MaxBillableHrs]*[PercentageRate])*[ChargeRate],0) AS TargetedRevenue, [ActualBillableHrs]-[HrsWrittenOff]+[HrsWrittenBack] AS ActualRecoverableHrs, [HrsNonAttendance]*[ChargeRate] AS [Non-Attendance$], [HrsOfficeAdmin]*[ChargeRate] AS [OfficeAdmin$], [HrsProfessionalDev]*[ChargeRate] AS [ProfDev$], [HrsFirmDev]*[ChargeRate] AS [FirmDev$], [ActualBillableRevenue]+([HrsOfficeAdmin]*[ChargeRate])+([HrsFirmDev]*[ChargeRate])+([HrsProfessionalDev]*[ChargeRate]) AS Total, Format(DSum("[ActualBillableHrs]-[HrsWrittenOff]+[HrsWrittenBack]","qryStaffPerformanceCalculator","[SRID]<=" & [SRID] & ""),"000.0") AS ActualHrsCumulative, Format(DSum("[TargetedHrs]","qryStaffPerformanceCalculator","[SRID]<=" & [SRID] & ""),"000.0") AS TargetedlHrsCumulative, Format(DSum(" [ActualBillableRevenue]","qryStaffPerformanceCalculator","[SRID]<=" & [SRID] & ""),"000.0") AS ActualFeesCumulative, Format(DSum(" [TargetedRevenue]","qryStaffPerformanceCalculator","[SRID]<=" & [SRID] & ""),"000.0") AS TargetedlFeesCumulative, [ActualBillableRevenue]+([ChargeRate]*[HrsNonAttendance])+([HrsOfficeAdmin]*[ChargeRate])+([ChargeRate]*[HrsProfessionalDev])+([HrsFirmDev]*[ChargeRate]) AS TotalAcual
  2. FROM tblStaffPerformanceStats
  3. WHERE (((tblStaffPerformanceStats.RYear) Between #7/1/2008# And #6/30/2009#) AND ((tblStaffPerformanceStats.StaffCode)=[Forms]![afmStaffReviewCentre]![cboLastname]))
  4. ORDER BY tblStaffPerformanceStats.SRID;
NB: The date range is currently hard coded but eventually will be selectable when everything is working how I want it.

I would appreciate suggestions on how to set up a connection between excel and my access query and if vba is the way to go, some sample code I could adapt.

I should also mention that although I am using Access 2007 and Excel 2007, the end user will be using Access 2007 runtime and probably Excel 2003.

Thanks
Feb 9 '09
15 5259
OzNet
31 New Member
Thanks again for your suggestions

This is what I have tried:

In a module
Expand|Select|Wrap|Line Numbers
  1. Function TDXLOut(sql As String)
  2. CurrentDb.QueryDefs("MyQuery").sql = sql
  3. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "MyQuery", "C:\ExcelTest\book1.xlsx", True
  4. End Function
Attached to a button
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command4_Click()
  2. Dim strSQL As String
  3. strSQL = "SELECT tblStaffPerformanceStats.SRID, tblStaffPerformanceStats.RYear, tblStaffPerformanceStats.StaffCode, " & _
  4. "tblStaffPerformanceStats.PercentageRate, tblStaffPerformanceStats.ChargeRate " & _
  5. "FROM tblStaffPerformanceStats " & _
  6. "WHERE (((tblStaffPerformanceStats.RYear) >= (DMax('[RYear]', '[tblStaffPerformanceStats]') - 366)) " & _
  7. "And ((tblStaffPerformanceStats.StaffCode) = Forms!afmStaffReviewCentre!cboLastname)) " & _
  8. "ORDER BY tblStaffPerformanceStats.SRID;"
  9.  
  10. TDXLOut (strSQL)
  11. End Sub
I get this error:
Runtime error 3265
Item not found in this collection

Do I have to select a reference to make this work?

Then I tried this:

Expand|Select|Wrap|Line Numbers
  1. Function OTXLOut(sql As String)
  2. CurrentDb.QueryDefs("MyQuery").sql = sql
  3. DoCmd.OutputTo acOutputQuery, "MyQuery", acFormatXLS, "C:\ExcelTest\book1.xlsx"
  4. End Function
and called it using a similar snippet of code to the first one.

I get the same error message.
Feb 12 '09 #11
OzNet
31 New Member
Dim db As DAO.Database
Stewart, I have already tried that after posting the original piece of code. It did not help.
Feb 12 '09 #12
ChipR
1,287 Recognized Expert Top Contributor
The runtime error you are getting says: "MyQuery" is not found in the QueryDefs collection.
Feb 12 '09 #13
mshmyob
904 Recognized Expert Contributor
First don't use reserved words like sql it can confuse things.

Try something like the following

button click code

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command4_Click() 
  2.  
  3. Dim qdfTemp As QueryDef
  4. Set qdfTemp = CurrentDb.CreateQueryDef("MyQuery", "your SQL code goes here")
  5.  
  6. ' call your function
  7. fExportExcel
  8.  
  9. End Sub
  10.  
function code

Expand|Select|Wrap|Line Numbers
  1. Function fExportExcel()
  2.  
  3. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "MyQuery", "c:\temp\test.xls", 0
  4.  
  5. ' delete your MyQuery definition from your project
  6. CurrentDb.QueryDefs.Delete "MyQuery"
  7.  
  8. End Function
  9.  
Hope this helps

cheers,

@OzNet
Feb 12 '09 #14
OzNet
31 New Member
Mshmyob, your example is working and gives me something to build on. I really appreciate your help.

Many thanks
Feb 13 '09 #15
mshmyob
904 Recognized Expert Contributor
You're welcome. Good luck.

cheers,
Feb 13 '09 #16

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

Similar topics

4
3233
by: Mark T. | last post by:
Hi, I've got a simple DB (Office Win XP Professional). Basically, it's like a check book register - deposit, deduction and running sum. I'd like to somehow link it to an Excel spreadsheet that will do the calculations and then keep the data updated in the form and report in the DB. Apparently this isn't done in Access because the "cell" where you enter the data is actually a field. I have a tabular form that looks just like a...
0
5569
by: AOstarello | last post by:
Hi all, I'm trying to link to some OLAP cubes that were created using the MS SQL Server Analysis Tools. The cubes reside on an Analysis Server. I'm trying to find a simple way for users to query the cubes without a lot of programming overhead. I can easily get the results I want by using MS Excel 2003. Through the menus, I choose: Data > Import External Data > New Database Query. Then I can select the OLAP Cubes tab, create a data...
0
1098
by: chemdawg | last post by:
Need help in linking access query to excel. The link I have now does not updated my main table or reports. What am i missing here?
6
1845
by: oyk | last post by:
I hope someone could help me on this. I have linked an excel worksheet to access but the some data shown in the access table has the #Num errors. Presumably, this must be because the data in the particular column in excel are of mixed data-type. For example, there are numbers as well as texts like "abcd" etc. in the same columns I tried changing the cell format in excel to text but to no avail as the problem still exist. Really need...
6
4298
by: jalmar | last post by:
Hello again: My second question is: I am linking an excel spreadsheet into Access, I have it linked and at first had problems getting the information imported into Access-it wasn't importing-I had a msg in the populated fields which read #NUM- I was told to use quotations before and after the numbers in Excel and it would import correctly, which did happen but when I run my query off that linked table, it creates far too many...
1
9778
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
2
2098
by: Andrew W | last post by:
Hi All, I'm trying to link to data on a website with Access. When I try File/ Get External Data/ link tables/ HTML/, I get the following error message: Database or object is read-only. I find this odd because I can get this data thru Excel's web query function.
1
1317
by: karthik28 | last post by:
I tried linking an excel spreadsheet to an access database. It shows me a window with the data but the header row in missing. However, when I open the excel file, save it without making any changes and try linking it again, it works fine. Please suggest a solution so that I dont have keep saving the excel file before linking it. Thanks KS
1
2328
by: agarwasa2008 | last post by:
Hi, I have a perfect file called "Products.xls" that I link to my MS Access 2003 database and everytthing looks good in that file. When I view the same file after linking in my database the data values of records in "tblProducts" are incorrect. For example the tblProducts has a field called "PackageSize" as listed below. THe problem is that it displays the text correctly but if the field has numbers only it messes it up. I have set...
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.