Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Retrieve data from any user desktop folder
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 12/13/2012 :  16:50:47  Show Profile  Reply with Quote
Hello all. I hope someone can help.
Below is the code for a test MACRO where I import worksheets and query them. I have the code to pull the information in from any users desktop in VBA but I cannot figure out how to get the SQL statements to work in the query. I need the data pulled from the any users workbook file and queried. The data file location is the same for all users, desktop\TEST folder, but the drive and user name will be different.

Sub Code_tester()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsSrc As Worksheet
Dim MyPath As String
Dim strFilename As String
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Dim oWsh As Object
Dim sPathDesktop As String
Set oWsh = CreateObject("WScript.Shell")
sPathDesktop = oWsh.SpecialFolders("Desktop") & "\TEST folder"
Set wbDst = Workbooks.Add(xlWBATWorksheet)
strFilename = Dir(sPathDesktop & "\PET*.xlsx", vbNormal)
If Len(strFilename) = 0 Then Exit Sub
Do Until strFilename = ""
Set wbSrc = Workbooks.Open(fileName:=sPathDesktop & "\" & strFilename)
Set wsSrc = wbSrc.Worksheets(1)
wsSrc.Copy After:=wbDst.Worksheets(wbDst.Worksheets.Count)
wbSrc.Close False
strFilename = Dir()
wbDst.Worksheets(1).Name = "Combined"
ChDir sPathDesktop
ActiveWorkbook.SaveAs fileName:=sPathDesktop & "\Combined", _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=d:\data\User Me\Desktop\TEST folder\Combined.xls;DefaultDir=d:\data\User Me\Desktop\TEST folder;DriverId=10" _
), Array("46;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$A$1")) _
.CommandText = Array( _
"SELECT `'Sheet1 (2)$'`.`sale #`, `'Sheet1 (2)$'`.`complaint #`, `'Sheet1 (2)$'`.`Complaint narrative`, `'Sheet1 (2)$'`.`Complaint resolution`, `'Sheet1 (2)$'`.`date of resolution`, `'Sheet1 (3)$'`.`sa" _
, _
"le #`, `'Sheet1 (3)$'`.animal, `'Sheet1 (3)$'`.disposition, `'Sheet1 (3)$'`.color, `'Sheet1 (3)$'`.`date of sale`" & Chr(13) & "" & Chr(10) & "FROM `d:\data\User Me\Desktop\TEST folder\Combined.xls`.`'Sheet1 (2)$'` `'Sheet1 (2)$" _
, _
"'`, `d:\data\User Me\Desktop\TEST folder\Combined.xls`.`'Sheet1 (3)$'` `'Sheet1 (3)$'`" & Chr(13) & "" & Chr(10) & "WHERE `'Sheet1 (2)$'`.`sale #` = `'Sheet1 (3)$'`.`sale #`" & Chr(13) & "" & Chr(10) & "ORDER BY `'Sheet1 (2)$'`.`sale #`" _
.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_Excel_Files"
.Refresh BackgroundQuery:=False
End With
End Sub

I need it to be able to look in any users desktop\TEST folder to run the query.
Any suggestions?

In (Som, Ni, Yak)

17689 Posts

Posted - 12/13/2012 :  20:12:04  Show Profile  Reply with Quote
This is not about SQL Server. Please post your question in an Excel / VBA in one of the Microsoft forum

Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000