Solved

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

Posted on 2010-11-19
8
428 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
  • 6
  • 2
8 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
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
Comment Utility
Tommy,
Please see attached.
Thanks
s
T1.jpg
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
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
Comment Utility
Sorry Tommy,
Please see attached.
S
T1.jpg
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
What's the error?
0
 
LVL 37

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now