[英]Parameters in Excel External Data Union Query
I have two queries that pull data from SQL Server into Excel. 我有两个查询,可将数据从SQL Server提取到Excel。 Both work perfectly fine.
两者都工作得很好。 I tried to Union them together and pass in a parameter as a date, and now nothing works.
我试图将它们合并在一起并传递一个参数作为日期,但现在没有任何效果。 Here's a link to an article that describes how to use Microsoft Query, and pass in a parameter.
这是指向描述如何使用Microsoft Query以及传递参数的文章的链接。
http://dailydoseofexcel.com/archives/2004/12/13/parameters-in-excel-external-data-queries/ http://dailydoseofexcel.com/archives/2004/12/13/parameters-in-excel-external-data-queries/
All I want to do is get this working with a Union Query. 我要做的就是让这个与联合查询一起工作。 Is that possible?
那可能吗? Or, do I need a VBA solution to achieve this.
或者,我是否需要VBA解决方案来实现此目的。 I'm sure it's do-able, I just don't know exactly how to do it.
我确定它是可行的,只是我不知道该怎么做。 I'd appreciate any suggestions.
我将不胜感激任何建议。
Thanks! 谢谢!
Thanks everyone. 感谢大家。 I ended up doing it in VBA.
我最终在VBA中做到了。
Sub ImportFromDB()
' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection
' Provide the connection string.
Dim strConn As String
'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"
'Connect to the Pubs database on the local server.
strConn = strConn & "DATA SOURCE=SERVER_NAME;INITIAL CATALOG=Data_Base;"
'Use an integrated login.
strConn = strConn & "Trusted_Connection=Yes;"
'Now open the connection.
cnPubs.Open strConn
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset
Set sht = Worksheets("Impact Analysis")
With sht
.Range("N:U").ClearContents
.Range("N1").Resize(1, 8).Value = Array("CONTACT_ID", "CATEGORY", "COMPANY_CODE", "CUSTOMER_NO", "SECTOR", "DES", "ASOFDATE", "BALANCE")
Set rw = .Rows(2)
End With
With rsPubs
' Assign the Connection object.
.ActiveConnection = cnPubs
' Extract the required records.
.Open "SELECT * FROM MY_TABLE"
' Copy the records into cell A1 on Sheet1.
Worksheets("Impact Analysis").Range("N2").CopyFromRecordset rsPubs
' Tidy up
.Close
End With
cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.