这是我正在尝试做的事情,我试图在excel vba中自动完成所有这些。
如果尚未创建连接,我就想创建一个连接,运行一个特定的SQL命令,在我的“数据”电子表格中抛出该表。
后来我会对它进行操纵。
我想在VBA中执行此操作的原因是因为当我尝试仅手动执行连接时,它将被删除,最终用户将不得不重置它。我希望这对最终用户来说尽可能简单。我注意到的另一件事是;除了不给我表[我不认为我告诉它]是它弹出默认表格对话框。
Workbooks("Tool4.xlsm").Connections.AddFromFile _
"C:\Users\Cutting_This_Out.odc" _
, True, False
With ActiveWorkbook.Connections("255.255.255.0").OLEDBConnection
.BackgroundQuery = False
.CommandType = xlCmdSql
.Connection = _
"OLEDB;Provider=SQLOLEDB.1;Integrated " _
& "Security=SSPI;Persist Security Info=True;Data Source=255.255.255.0; " _
& "Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation " _
& "ID=TRIMMED-PC;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=COSTrpt"
.CommandText = _
"SELECT [PRVDR_NUM],[hha2011cbsa_ms].CBSA,[CBSA_Name],[VISITS_IN],[ADJUSTED_REIMB] FROM [COSTrpt].[dbo].[hha2011cbsa_ms] INNER JOIN wage_index ON wage_index.CBSA = hha2011cbsa_ms.CBSA"
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = ""
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
End With
ActiveWorkbook.Connections("255.255.255.0").Refresh
End Sub
其他信息;我正在与MS-SQL-Server连接,我已经删除了一些私人信息。