Stored procedure running in the background? - vba

Edited: Is it good practice to have a Store Procedure running retrieving data while the user keeps working while that data is compiled for them?
Old Question: I run a stored procedure to retrieve data from a server that takes about 10 - 15 minutes to complete. While running the stored procedure, my Excel workbook freezes until all the data has been returned. I might have an option to run this stored procedure in the background while allowing the Excel workbook to be workable. The idea is to give the user the possibility to work in the workbook while retrieving data instead of waiting 10 - 15 minutes just looking at the worksheet.
My question: I have never seen this idea before in any other application. I don't know if this means a good practice standard. Having the database loaded while working in the same workbook and when the data finishes alert the user that all the data has come through.
What do you guys think? Do you believe this could complicate something else?
I don't really like the idea but at lest the user wouldn't lose 10 - 15 minutes.
Thank you all :)

You need to create an instance of Excel application and run your stpred procedure there. In that case, your Excel application will still be available for the user, however I am expecting it to be slow. You have to modify your VBA code then to run in under a new instance of Excel application:
dim xlApp as Excel.Application
set xlApp=New Excel.Application
so here after to do your stuff you have to mention the application too since you have now more than one active application. For example to create a new workbook you would do:
xlApp.Workbooks.Add
I think you can probably open a new instance of Excel manually, then open your workbook that contains your VBA code using OPEN from FILE tab and then run your code, in that case the default Excel application will be available for the user to work. To open a new instance of Excel keep SHIFT key down and click on your Excel application icon (on your start menu or desktop etc, if you don't know where you can find it, simply go to Windows Start Menu and Search EXCEL)
Hope this helps!

Related

Refreshing Power Pivot automatically

I hope that you can help me.
That's my situation: daily I'm importing in Power Pivot some data through a query on a SQL database.
Actually every morning I open the Power Pivot and I refresh it for import the data of the previous day present in the database.
This action require 20 minutes because I have a lot of data to import.
I was wondering if there is a way to do this action during the night, maybe an automatic refresh, so that I can open the file in the morning and I alredy have the data of the previous day.
I hope that I was clear with my request, thanks in advice.
If the Excel workbook is on a machine that does not shut down, you can keep the workbook open and configure the query to automatically refresh ever x minutes.
Or you can keep the workbook open and run VBA code to refresh the query on a timer.
There are plenty of examples for VBA timers if you just care to search.
Or you can configure the queries to refresh automatically when the file is opened, then create a Windows Task Scheduler job to open the workbook at a specific time. Again, the computer running this must be turned on.
You see that there are many options and they are all well documented and just a short google search away.

How to save two workbooks as and mirror the connections that they contain

I need some conceptual help from those more experienced than me --
I know you guys usually like code but I have many pages of code on this project and my question is not regarding any code I have but rather how I might code something I have not been able to figure out.
So basically I have two workbooks whose main purpose is labor tracking. one is the control workbook that has all my code, the other is a workbook where reports are made by field workers. The reason the field workbook cannot have code is because it will be operated from an ipad which I believe cannot yet run VBA. I know how to establish the connections necessary, however for every project a new copy of both the workbooks need to be made. I already have functions that clear out, reset and save the main workbook however I am struggling with the connected workbook. I know how to save the connected workbook, but I was hoping to preserve the connections between the two old workbooks when I run the macro that saves them as and also mirror those connections between the new workbooks. Is this possible or will I need clear all connections and re establish them every time a new set of job books needs to be created?
===> EDIT: I believe that I need to embed the report book into the main book, but that still begs the question on how I save the embedded workbook as at the same time I save the main workbook as in order to create a new book with all the same connections

Running a WinForms application using Excel VBA

I have written a WinForms application in vb.NET using vs 2017. The application opens an excel Workbook which contains a document register, and updates the files as necessary.
I am very happy with the application, however one possible improvement I would like to pursue would be to attach the application to a macro in the Excel worksheet that the application updates, as the users of the application tend to open this worksheet by habit as it is.
I have the following code, which opens the application from the document register:
Sub AppMacro_Click()
Dim AppOpen
AppOpen = Shell("C:\Document Updater.exe", 1)
End Sub
The user can then run the application.
The problem with this method is that the application checks that no user has the workbook open, so that it can open the original copy (workbook is on a network drive), so if the user runs the macro, they will then have to close down the workbook before clicking the run button on the application. I would prefer if this didn't have to be the case.
What I would like to achieve is the following:
1) If the user runs the application using the macro, the application adapts and updates the opened workbook (as long as it isn't read-only)
2) If the user runs the application as usual, it still detects that no other user has the workbook open
What would be the best way to achieve this?
In general storing a workbook on a shared folder to be accessed/modified by multiple users is usually asking for trouble, you are better off using an Access DB if your users pool is fairly small or use full grown SQL (mySQL, SQL Server). Also since you have a working application in .NET where the user is very dependent on Excel, I would recommend you transition to an Excel Add-in. It is really easy and your solution would be more integrated with Excel

Prevent >1 person from running VBA macro at a time (shared wkb)

What I'm aiming at is getting a list of all currently running scripts in order to check if other users aren't running the same VBA macro at the same time (and if yes, then stop the code etc., similar to what the OP of the below question wanted). This would be for a shared workbook (I learned it's not designed for this type of work, but I need to try it).
https://stackoverflow.com/a/36116091/5947935
I've been trying to make the code in the above answer work in VBA, but it seems it's a vbs thing and I would like to avoid that.
I'm not an expert to say the least, so I'm having trouble understanding how to get this to work in Excel VBA. I don't even know if it's possible at all.
I've found this as well: VBA Getting program names and task ID of running processes and it works fine but it only lists the running processes.
I've no idea however how to merge the two... or even if the WMI is the correct way to go.
I'd appreciate any sort of help.
I used to create a "locking file" which was just an empty text file with the name of the workbook followed by the username and an extension of .LCK
First thing my code did on auto open was look for a locking file then report back to the user which user had it open then cancel the open.
If it didn't find a locking file then it created one and proceeded as normal. If it found one but it was the same username (ie that user had it crash on them) it proceeded with the open.
The last thing the code did was delete the file.
No codes here and theory not tested yet, but the idea of preventing a different user executing a Macro on a shared workbook requires some thinking.
I would create a hidden worksheet, and use one of the cells to store the Environ("USERNAME") when the macro is first started - to indicate who has it running, then clear it when complete, first-in-first-out.
Lets say named range MUser (macro user) is range A1 in that hidden worksheet
When the macro runs, it will first check if MUser is empty, if so then change it's value to Environ("USERNAME") and Save the file before next step (here I am not certain the value is updated on others session).
If MUser is not empty, either abort or retry in a few second.
When macro completes, MUser will be ClearContents, and save the File to free up the workbook for macro.
Idea is here but please test. Post your own code for us to troubleshoot. You may also use Workbook events to "lock" the macro execution this way. Or even use this hidden sheet to make a log record for debug. Also some fail-safe needs to be implemented (such as a time stamp at macro start and override the lock after some minutes).

Running a VBA excel macro at a certain time

Trying to figure out the best way to kick off an excel macro at a certain time. I found a few suggestions, but I still have plenty of questions on the idea and was hoping you guys could provide some clarification.
What I am trying to do: I have live excel workbooks that are connected to my database. So at 11:30PM on the last day of every month I want to go into several excel workbooks (I have one for every vendor that we sell), enable the security and editing and refresh the connection so all of the information in the sheets is up to date. Lastly I would like to then save a copy of that file in a sub folder inside the current folder.
Run on time method:
Here is this run on time method that I found on ozgrid. My question is, I am assuming that excel would need to be running at the time in order to execute the method? If so what would be one way of opening up excel and refreshing a workbook to be saved?
Running excel on Windows Task Scheduler Windows Task Scheduler
Essentially this method is using an Excel Controller to run excel using VBS...Seems more promising than the run on time method but I am not very familiar with how an excel controller works or VBS, so any help or ideas on this one would be greatly appreciated.
Any input is greatly appreciated. If my ideas are way out in left field could you guys help point me in the right direction?
Thanks for all the help in advance!
Turn the problem around. Create a workbook with normal Excel query tools and have it load the result set of the query when the workbook is opened (You don't specify a version, but see Refresh an external data connection for Excel 2013).
Then, either create a view in the database that contains the results you require, or to materialise the result of a view into a staging table that is then read by the workbook.
Users can then open the workbook whenever they require data, and the current results will be available. A parametrised version of the query could then access a history table to older periods.
This way all data is only prepared when required, and is always available without the scheduling complexity.