473,395 Members | 1,608 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,395 software developers and data experts.

Automation Access to Excel

Hi Guys,

I am a real newbie, so please bare with my ignorance :). Also this is a long set of questions and answers for any of them would be greatly appreciated.

1)

I am working on this project where i am looking to pick up filtered data from a table (in Access) and insert this into an already existing excel file. The problem is that the number of rows depending on the filter will be different everytime.

I was wondering how exactly i could insert them in the middle of two rows already in the excel file. Also if this can be done line by line through a loop.

The reason i would want to do it line by line is since the values might be in % or an absolute value, so i would need to treat them differently.

I really hope i have explained this properly. please do let me know if i can explain anything in more detail.

2)

This is also automation of excel through Access VBA.

What is the syntax for checking if an excel file is already open, and how can i edit that particular file. I have been using code to set the autofilter on an excel file but it only works when 1 excel file is open, if more than one is open then it gives an error.

Thanks again for your time.
Cheers!
Paradigm
Mar 3 '09 #1
13 2695
FishVal
2,653 Expert 2GB
Just for starters: Application Automation
Mar 3 '09 #2
NeoPa
32,556 Expert Mod 16PB
It's not possible to open a spreadsheet if it's already open.

I got around that by renaming the file first (to itself). An error code of 75 indicates the file's locked.
Expand|Select|Wrap|Line Numbers
  1. Name strName As strName
Mar 3 '09 #3
NeoPa
32,556 Expert Mod 16PB
I suppose I should clarify that last remark.

When an open is requested of a spreadsheet that is open, instead of failing the call, it opens it in ReadOnly mode with a notification when the file becomes available for full ReadWrite access. This is pretty well useless in a code-controlled environment, so I treated it as a failure. I did try all sorts of things to open it (specifically asking for ReadWrite access in the open for instance) but nothing gave me what was required. The rename tweak gave me the closest thing to what was required.
Mar 3 '09 #4
FishVal
2,653 Expert 2GB
Maybe the workbook could be set to be shared and used as such.
Mar 3 '09 #5
NeoPa
32,556 Expert Mod 16PB
I'm not familiar with that. What do you mean?
Mar 3 '09 #6
FishVal
2,653 Expert 2GB
I'm not familiar with that too. I know about this option but didn't use it.
MainMenu -> Tools -> Share WorkBook ..
Mar 3 '09 #7
NeoPa
32,556 Expert Mod 16PB
Ah. I see.

I would imagine that using that option is likely to trigger prompts to the operator as to what should be maintained and what discarded in certain circumstances. Not good for when controlling by code.

It may be possible to make it work, but I'd be very careful before implementing a solution based on that.
Mar 3 '09 #8
Thank you both very much for your inputs.

Let me explain in a bit more detail, i actually dont want to open the excel file again, i just want to know how i can check if the file is open.
If its not then i want to open it and add an autofilter on it, if its already open then all i need to is to add an autofilter on it. ( all of this through access ofcourse).

Hope that clears it up just a tad bit.

Also i understand the first question was a bit complicated but maybe i didnt explain it properly.
It weould probably be best to take step by step advice.

What would be the process of inserting a query into excel where the query may vary in terms of the number of rows. Insert such that the other rows in the excel file are adjusted accordingly.

Thanks again.
Paradigm
Mar 3 '09 #9
NeoPa
32,556 Expert Mod 16PB
@Paradigm
Please read through the article linked in post #2. It should answer all or most of this. If there are still issues you're unsure of, please explain what they are in particular and we will do what we can to help.
@Paradigm
I suggest you take this question and ask it again in it's own thread (as per the site guidelines). It would be quite messy to have two subjects going on in this thread, especially as it's not clear at this time exactly what you're after with this.

When reposting, please bear in mind that Excel doesn't have queries (as such - it has Get/Import External Data). It can have data pasted in from elsewhere. It can have the data from an Access recordset transferred directly. It would be helpful if, in your new thread, you made it clear what you envision happening.
Mar 4 '09 #10
Ok this is the code that i am using currently
Expand|Select|Wrap|Line Numbers
  1. Sub OPenMRZFile()
  2. Dim oexcelApp As Excel.Application
  3. Dim oexcelwb As Excel.Workbook
  4. Dim strExcelFile As String
  5. Dim Country As String
  6.  
  7. strExcelFile = "C:\Documents and Settings\abc\Desktop\ChangeTool\Resources\Data and Templates\MRZ.xls"
  8.  
  9.  
  10. Set oexcelApp = New Excel.Application
  11.  
  12. oexcelApp.Visible = True
  13.  
  14. 'input box on form
  15.  
  16. Country = Me.cmbcountrymrz 
  17. Set oexcelwb = oexcelApp.Workbooks.Open(strExcelFile)
  18.  
  19. With oexcelwb
  20.     Range("F4").AutoFilter Field:=6, Criteria1:=Country
  21. End With
  22.  
  23. Set oexcelApp = Nothing
  24.  
  25. End Sub
The code is executed through a command button in access.
The problem i am facing is that if someone clicks it a first time it works perfectly, but once the excel file is open if the button is clicked again then it opens up the file in a read only format and doesnt change the autofilter.

What i want to be able to do is
if the file is open, to only change the filter
and
if its not open to open it and execute the autofilter

As for the other question ill make another thread and try to explain it better.

Thanks again for your time and patience guys, much appreciated!

Regards
Paradigm
Mar 4 '09 #11
NeoPa
32,556 Expert Mod 16PB
@NeoPa
I tried to make this clear in my last post. Please reread and pay particular attention to the highlighted text. It was worded specifically to ensure that you didn't post a simple rewording of your problem.

I'm not trying to be unsympathetic, but we are here to help you understand and fix your problems, not to do that for you.

Please check out the article and respond with reference to that, if there are still issues you don't understand.
Mar 4 '09 #12
Stewart Ross
2,545 Expert Mod 2GB
Hi. You are making a fairly fundamental mistake in your approach to automation here. Access communicates with Excel through the Excel application object you instantiate in your code (object variable oexcelapp in your case). For Access to be able to interact with Excel on multiple command button presses the application object has to be persistent between calls to your subroutine. But as the object is in scope solely within your subroutine it cannot persist between calls, even if you had not set it to nothing in line 23 (resetting the object variable to its undefined state). Excel itself will still run and the workbook will still be open, as it has not been explicitly closed before the application object was destroyed. It is the communication path used by Access which is removed when the application object goes out of scope like this.

If you click the button again, another instance of Excel is opened - but as the workbook concerned is still open, running in the unlinked Excel instance you first opened, an attempt to open it again leads to the 'read-only' copy you mention, which NeoPa also discussed in his earlier posts.

Access cannot communicate with Excel directly unless it has established the link through an active application object instance. It has no way of establishing contact with another open instance of Excel running in parallel but sitting outside the scope of any of its application objects.

To be able to re-use the same Excel application object between button presses on a persistent basis you will need to have the application object global in scope to the form concerned. That is, it should be defined as a public variable in the header code of the form, not in a sub within it.

If you let the application object go out of scope you lose it and cannot re-establish communication. It is like cutting a tow rope between vehicles, then trying to continue pulling one from the other as if nothing had happened - somewhat futile.

I cannot imagine why you need to make autofiltering some kind of user-dependent choice from Access however. If you are going to automate Excel this way you need to recognise that Excel should either be running entirely within Access's control, creating, opening and closing workbooks using Access VBA commands, or you use Access to open the workbook then let the user do the rest in normal interaction with Excel - cutting the link in the process. Trying to do both at the same time - using Access for automation whilst the user interacts with Excel in realtime - is asking for object scope conflicts, occuring for example if the user closes the newly-open Excel workbook manually, conflicting with any further programmed attempts by Access to refer to that workbook.

-Stewart
Mar 4 '09 #13
FishVal
2,653 Expert 2GB
Hello, Paradigm.

You could use DDE to make your application behave as you've described.
However, DDE is well outdated and the only advantage is that it allows to find running application and communicate with it.

Example:
Expand|Select|Wrap|Line Numbers
  1.     Dim lngCh As Long
  2.  
  3.     On Error GoTo DDEInitFailed
  4.     lngCh = DDEInitiate("Excel", "<..path to desired workbook..>")
  5.     On Error GoTo 0
  6.  
  7.     ' write something in A1 cell
  8.     DDEPoke lngCh, "R1C1", "qqq"
  9.     DDETerminate lngCh
  10.  
  11.     Exit Sub
  12.  
  13. DDEInitFailed:
  14.     MsgBox "Workbook is not opened"
  15.  
  16.  
Mar 4 '09 #14

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

Similar topics

17
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my...
12
by: Cheval | last post by:
Has anyone had any problems with inter-office automation between MS Word and MS Access in Office 2003? I have recently installed office 2003 in a new folder and have left the older office 2000...
1
by: Jimmer | last post by:
I've got what should be an easy automation problem, but the solution simply isn't coming to me. I've got several public variables set up for automation as follows: Public gappExcel As...
5
by: Gary Cobden | last post by:
I have a problem with the following code, which leaves an instance of Excel visible in Task Manager. By a process of elimination I have got it down to the fact that something in the...
7
by: taylor.bryant | last post by:
I am running: Win XP SP2 Excel 2002, Access 2002 (Office XP SP3) Using Visual Basic (not VB.NET) At one point (prior to XP SP2?!? - I can't pin it down), this did not happen and I was easily...
4
by: Mark C | last post by:
I currently have an Excel spreadsheet with numerous text boxes and check boxes that I want to populate with data from an Access 97 database. I have used Excel automation in Access before but only...
1
by: u7djo | last post by:
Hi, I'm currently building a function in Access that creates an Excel spreadsheet but it doesn't look like the Excel object is being destroyed correctly as the Excel module is still showing in the...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
4
by: Keith Wilby | last post by:
How controllable from Access VBA is Excel? I'm currently using automation to dump 2 columns of data into an Excel spreadsheet so that the end user can create a line graph based on it. Could the...
4
by: Belgarath | last post by:
Hello all, I'm a newbie in Access... And before starting to explore this software, I would like to know if it's possible to automate everyday queries result in Excel . If it's possible can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.