|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册账号
×
*在Excel里面建立数据链接
Private Sub GetFirstQuery()
ThisWorkbook.Activate
Sheets("Data").Activate
Cells.Delete
Set C = ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=MS Access Database;DBQ=" & ThisWorkbook.Path & "\test.mdb;DefaultDir=;", Destination:=Range("A1"))
With C
.CommandText = Array("SELECT * FROM A Where ID <=1")
.Refresh BackgroundQuery:=False
End With
End Sub
根据参数进行查询
Private Sub GetData(Para1 As String, Para2 As String, Para3 As String)
Dim WhereText As String
ThisWorkbook.Sheets("Data").Activate
WhereText = IIf(Para1 <> "", " segment='" & Para1 & "' ", "")
WhereText = WhereText & IIf(Para2 <> "" And WhereText <> "", " And ", "") & IIf(Para2 <> "", " Channel='" & Para2 & "' ", "")
WhereText = WhereText & IIf(Para3 <> "" And WhereText <> "", " And ", "") & IIf(Para3 <> "", " Demo='" & Para3 & "' ", "")
With C
.CommandText = Array("SELECT * FROM A Where " & WhereText)
Debug.Print .CommandText
.Refresh BackgroundQuery:=False
End With
End Sub
实施查询的程序
Sub Test()
Call GetFirstQuery
Call GetData("18", "Mike", "2006")
End Sub |
|