PC Review


Reply
Thread Tools Rate Thread

a better way to query?

 
 
festdaddy
Guest
Posts: n/a
 
      4th Nov 2009
I'm trying to learn how to query a DB with VBA in 2007.

What I need to do: grab two pieces of information from a spreadsheet
(location, time), and use this info in a query to return some results.

What I did: I recorded the procedure. Using Ribbon->Data->From Other
Sources->From MS Query, then I manually entered my query paramaters.
This worked fine. I then edited the recorded macro, but this took
awhile to get right. I essentially had to write the whole query
string, instead of just changing a hard value (the one I manually
entered during recording) to a variable.

What I'm hoping someone can help me with is: I'm sure there is a
better way to do this. I've tried searching this group, and the
related results look very different. Could someone point me in the
right direction to understand this part of VBA a little better, or
perhaps just tell me an easier way to get a query done?

Thanks,
-Rob
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub process_data()

endrw = ActiveCell.SpecialCells(xlLastCell).Row
For orw = 3 To endrw
Sheets("output").Select

zc = Cells(orw, 3)
my = Cells(orw, 4)

selectstring = "SELECT dist_by_zip.Zip, dist_by_zip.YB,
dist_by_zip.Percentile, dist_by_zip.RC" _
& Chr(13) & "" & Chr(10) & _
"FROM M360.dbo.dist_by_zip dist_by_zip" _
& Chr(13) & "" & Chr(10) & _
"WHERE (dist_by_zip.Zip=" & Chr(39) & zc & Chr(39) & ") AND
(dist_by_zip.YB=" & Chr(39) & my & Chr(39) & ")"

Sheets("temp").Select

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array
("ODBC;DRIVER=SQL Server;SERVER=COMPUTERNAME;UID=MYIDNUM;APP=2007
Microsoft Office
system;WSID=COMPUTERNAME;DATABASE=M360;Trusted_Connection=Ye"), Array
("s")), Destination:=Sheets("temp").Range("$a$1")).QueryTable
.CommandText = Array(selectstring & Chr(13) & "" & Chr(10) &
"ORDER BY dist_by_zip", ".Percentile")
'original .commandtext line: .CommandText = Array( _
"SELECT dist_by_zip.Zip, dist_by_zip.YB,
dist_by_zip.Percentile, dist_by_zip.RC" & Chr(13) & "" & Chr(10) &
"FROM M360.dbo.dist_by_zip dist_by_zip" & Chr(13) & "" & Chr(10) &
"WHERE (dist_by_zip.Zip='51104') AND (dist_by_zip.YB='1920')" & Chr
(13) & "" & Chr(10) & "ORDER BY dist_by_zip" _
, ".Percentile")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_m360"
.Refresh BackgroundQuery:=False
End With

Next orw

End Sub
 
Reply With Quote
 
 
 
 
festdaddy
Guest
Posts: n/a
 
      6th Nov 2009
Thanks Joel. Do yuo happen to know of a source where I could learn a
little more about this? I've been using J. Walk's book (excel 2003
power programming w/VBA), but there isn't much documentation about
accessing SQL with VBA. I've done a couple of searches, but haven't
found much so far. It seems like this end of VBA is not all that well
documented?

Thanks again for your help,
-Rob
 
Reply With Quote
 
festdaddy
Guest
Posts: n/a
 
      6th Nov 2009
Thank you Joel. I'm just now figuring out that I need to start
learning about ADO. Do you happen to kow of any good VBA related ADO
books or other sources?

Thanks again for your help.
-Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS Access metadata repository generation query, matrix generationquery, table-generation query, array-generation query. Roy Roebuck Microsoft Access Database Table Design 1 20th Dec 2008 07:23 PM
Set sub (embedded?) query parameters using VBA, call sub query in primary query with parameters completed Kelii Microsoft Access 4 5th Feb 2007 03:01 AM
verfiy a query like Query Analyzer's parse query function =?Utf-8?B?a2V2aW4=?= Microsoft C# .NET 1 24th Dec 2005 01:33 AM
running delete query or make table query from macro or event proce =?Utf-8?B?dHJhdmVsZXI=?= Microsoft Access Queries 3 1st Mar 2005 12:01 AM
Error running query for report...but query works fine when opened separately John Microsoft Access Reports 1 21st May 2004 04:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:20 AM.