嗯 我现在的情况是这样的(查询SERVER服务器上的DB数据库的SA表):
Sub TEST1()
'
'X = Cells(1, 1).Value
Sheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=SERVER.DB;APP=Microsoft Office 2003;WSID=TEST;DATABASE=DB;Trusted_Connection=Yes" _
, Destination:=Range("A1"))
.CommandText = Array( _
"SELECT SA.T2, SA.T8, SA.T1, SA.T3, SUM(AMOUNT*-1)" & Chr(13) & "" & Chr(10) & "FROM DB.dbo.SA SA" & Chr(13) & "" & Chr(10) & "WHERE (SA" _
, _
".CODE='504') AND (SA.PERIOD=2006006)" & Chr(13) & "" & Chr(10) & "GROUP BY SA.T2, SA.T8,SA.T1, SA.T3" _
)
.Name = "Query from SERVER"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
这个是我用EXCEL的数据库查询 记录下来再改过的[em04],我希望上面查询条件的红色部分能够实现更改 但是试过几种办法,要么不能取到单元格的内容,要么就提示语法出错。
[em04]我对EXCEL和VBA都很不熟,这是第一次用,麻烦各位讲的稍微详细些,谢谢!