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

Run SQL Query, then Save

I am trying to automate an excel report so that it automatically posts a SQL query then saves the file every 24 hours. Currently I can do one or the other (post sql or save) but I cannot run both at the same time.

So far, I have an ODBC connection between my excel file and my MS SQL database and have the SQL queries refreshing every 1440 minutes.

I also have a VB timer set to run every 24 hours to save the workbook every morning. The timer starts when I click a button. Here is the script I use for that

Sub Save()
ActiveWorkbook.Save
timer1
End Sub

Sub SaveName()
ActiveWorkbook.SaveAs Filename:="C:\..."
timer1
End Sub

Sub timer1()
Application.Wait Now + TimeValue("24:00:00")
Save
End Sub

Private Sub CommandButton1_Click()
timer1
End Sub



However, when I click the button to run the timer it slows everything down so that my MS SQL query timer does not count down.

My question is how to get both of these to work at the same time. Should I run the SQL query within VB as well so both functions use the same timer. If so, how would I do that. If I can figure this out, I will be able to run several reports that update, save and e-mail themselves out every day, week, month etc...

Onthefly
Sep 26 '06 #1
10 8477
CaptainD
135 100+
I am trying to automate an excel report so that it automatically posts a SQL query then saves the file every 24 hours. Currently I can do one or the other (post sql or save) but I cannot run both at the same time.

So far, I have an ODBC connection between my excel file and my MS SQL database and have the SQL queries refreshing every 1440 minutes.

I also have a VB timer set to run every 24 hours to save the workbook every morning. The timer starts when I click a button. Here is the script I use for that

Sub Save()
ActiveWorkbook.Save
timer1
End Sub

Sub SaveName()
ActiveWorkbook.SaveAs Filename:="C:\..."
timer1
End Sub

Sub timer1()
Application.Wait Now + TimeValue("24:00:00")
Save
End Sub

Private Sub CommandButton1_Click()
timer1
End Sub



However, when I click the button to run the timer it slows everything down so that my MS SQL query timer does not count down.

My question is how to get both of these to work at the same time. Should I run the SQL query within VB as well so both functions use the same timer. If so, how would I do that. If I can figure this out, I will be able to run several reports that update, save and e-mail themselves out every day, week, month etc...

Onthefly
Excel has an Autosave add-on that comes with it, why not use it. Should be listed under tools, if not you'll need the disk to add it.
Sep 26 '06 #2
That is what I originally tried, and it works but the autosave feature saves it as an .xar file and it saves it like a hidden file which prevents my re-occurring e-mail software program from recognizing it. Using the Autosave feature would be my first choice if I could figure out a way around this issue.
Sep 26 '06 #3
CaptainD
135 100+
Ok, I re-read your post, and you have the Excel file Updating / inserting the Excel changes to the MS Sql Database every night, correct?

Why not just perform the updates when they're made. I created one at work that uses ADO and when you leave a cell, updates that records field

If I do not have a value in the 1st column then that indicates a new record so it does an insert to the table
Sep 26 '06 #4
Actually I have it the other way around. I have an excel spreadsheet that is pulling info from my MS SQL database everynight and posting the results in the spreadsheet (and it erases the results that were their the day before). So my question is, how do I then save the changes from the new query after the update took place so I can e-mail out the new figures.
Thanks for your help by the way....
Sep 26 '06 #5
CaptainD
135 100+
Actually I have it the other way around. I have an excel spreadsheet that is pulling info from my MS SQL database everynight and posting the results in the spreadsheet (and it erases the results that were their the day before). So my question is, how do I then save the changes from the new query after the update took place so I can e-mail out the new figures.
Thanks for your help by the way....
We both learned on this one.

Create a module with this procedure in it

Expand|Select|Wrap|Line Numbers
  1. Public Sub SaveData()
  2.     ThisWorkbook.Save
  3.     MsgBox "Saved", VBOkOnly, "Data Saved"
  4. End Sub
  5.  
and in the worksheet add this

Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2. 'This will run the code 1 minute after the worksheet is opened
  3.     Application.OnTime Now + TimeValue("00:01:00"), "SaveData", , True
  4.  
  5. 'Or Use this to set a time, here it is 1 PM
  6. '     Application.OnTime TimeValue("13:00:00"), "SaveData", , True
  7. End Sub
  8.  
  9.  
Sep 27 '06 #6
Hey, I think this will work! But I must be missing something because as it tries to save I get this error:

The Macro:"C:Documents and Settings\....'!SaveData' cannot be found.

Sorry, I am fairly new with VB so I am probably missing something simple.. any idea?
Sep 27 '06 #7
CaptainD
135 100+
Hey, I think this will work! But I must be missing something because as it tries to save I get this error:

The Macro:"C:Documents and Settings\....'!SaveData' cannot be found.

Sorry, I am fairly new with VB so I am probably missing something simple.. any idea?
In the VBA Editor add a module, I call mine Utils since I usually add other functions and subs to it to do different things. Then place the "Public Sub SaveData()" I gave you there, not in the worksheet area. Then it should see it.
Sep 27 '06 #8
This is awesome, it works perfectly!

Thanks!
Sep 27 '06 #9
CaptainD
135 100+
This is awesome, it works perfectly!

Thanks!
No problem, glad to help, and that was a method I was not aware of so I learned something as well.
Sep 27 '06 #10
Quick question, will this loop the time so it saves it every day at the same time without me having to re-open this?

I'm glad you learned as well. The cool thing about this set up is that I now have about 25 excel reports that automatically update themselves every day/week/month and e-mail themselves to the business owners at a specified time, so I don't have to touch them.
Sep 28 '06 #11

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

Similar topics

6
by: Ryan | last post by:
I came across a situation that I've been unable to explain and was hoping somebody had an answer: I had written an update query which was taking about 8 seconds to run and considered it too...
5
by: Carl | last post by:
Please can anyone tell me how I can create a macro to save the results of a query as an excel file? The query is called Student List and I would like to save it to "My Documents". We have had...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
7
by: Nicolae Fieraru | last post by:
I have two tables, they contain: Table1: ID1, Name1, Address1, Purchase1 Table2: ID2, Name2, Address2, Purchase2 I need a query which creates Table3 with content from Table1 and Table2. The...
2
by: Smartin | last post by:
Using Access 97 I am trying to create a form that allows for user entry of search terms and displays a table of results. I thought I could accomplish this using a subform but it isn't quite...
4
by: Macroman | last post by:
MS Access XP, running on Win XP, Processor 2.4Ghz , 512Mb RAM, 40Gb Hard drive Table 1 has 167,000 records and contains the following fields tblone_custID tblone_easting tblone_northing ...
1
by: eighthman11 | last post by:
Hello everyone. I have a table linked to an excel spreadsheet. I have an append query based on this link table. I receive an error when I run the append query "numeric field overflow". I have...
3
by: Nathan Guill | last post by:
I have an interface that works with an Access back-end. I would like to store and/or load user defined query strings per each user (i.e. no user can access another's queries). The idea I had was...
3
by: Wayne | last post by:
I'm trying to automate the export of a query to a text file using code. If I export the query manually I get the exact result that I want i.e. If I select the query then choose File/Export from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
0
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...
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.