[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Hardcoding SQL query into an excel sheet to run as a macro

Posted on 2010-11-19
8
Medium Priority
?
472 Views
Last Modified: 2012-05-10
Hi,

Is it possible to hard code this sql query in a master excel sheet to run as a macro.

Thanks


select d.Recordings,r.Extension, r.LastName, r.firstName  
from users r
join (
      select u.userid, count(*) as Recordings
      from users u join calllog c on u.userid=c.userid
      where starttime between dateadd(d, 0, datediff(d, 0, getdate())) and dateadd(d, 1, datediff(d, 0, getdate()))
      group by u.userid
) d on r.userId = d.userId

select count(*) as TotalRecordings
from calllog

where starttime between dateadd(d, 0, datediff(d, 0, getdate())) and dateadd(d, 1, datediff(d, 0, getdate()))
0
Comment
Question by:Sean
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34177339
Sheet1.QueryTables.Add(connectionStringGoesHere, Range("A1"), "select d.Recordings,r.Extension, r.LastName, r.firstName  
from users r
join (
      select u.userid, count(*) as Recordings
      from users u join calllog c on u.userid=c.userid
      where starttime between dateadd(d, 0, datediff(d, 0, getdate())) and dateadd(d, 1, datediff(d, 0, getdate()))
      group by u.userid
) d on r.userId = d.userId")
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34177346
Of course those line breaks will mess it up
Dim con as String
Dim sql As String

con = "Provider=blahblah..."
sql = "select d.Recordings,r.Extension, r.LastName, r.firstName  _
from users r _
join ( _
      select u.userid, count(*) as Recordings _
      from users u join calllog c on u.userid=c.userid _
      where starttime between dateadd(d, 0, datediff(d, 0, getdate())) and dateadd(d, 1, datediff(d, 0,  getdate())) _
      group by u.userid _
) d on r.userId = d.userId"

Sheet1.QueryTables.Add(con, Range("A1"), sql)
This puts the resulting table starting in A1

Open in new window

0
 
LVL 1

Author Comment

by:Sean
ID: 34177879
Tommy,
Please see attached.
Thanks
s
T1.jpg
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 34178076
Right, sorry. You either need to do
Set someVar = Sheet1.QueryTables.Add(con, Range("A1"), sql)
or just do
Sheet1.QueryTables.Add con, Range("A1"), sql

VBA is weird about the parentheses. If you use them, it wants you to do something with the return value.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34178094
Right, sorry. You either need to do
Set someVar = Sheet1.QueryTables.Add(con, Range("A1"), sql)
or just do
Sheet1.QueryTables.Add con, Range("A1"), sql

VBA is weird about the parentheses. If you use them, it wants you to do something with the return value.
0
 
LVL 1

Author Comment

by:Sean
ID: 34178780
Sorry Tommy,
Please see attached.
S
T1.jpg
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34181405
What's the error?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34181408
If you don't have an actual Sheet1 object you can use Sheets("SheetName")
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question