编写SQL查询,从筛选的Date列中选择数据

时间:2016-06-29 12:13:10

标签: sql excel vba crosstab access

我正在尝试查询Access中的交叉表/记录集(在excel表上尝试了ADO),其中列中包含日期,例如。

Columns appearing as Dates

如果我想提取仅有2014年的列的有限数据,我该如何编写这样的查询?请注意,这些列可能会有所不同。

1 个答案:

答案 0 :(得分:1)

类似的东西,可能有一个更简单的方法

Sub Akward_Query()

Dim strSQL As String
Dim strSQL2 As String
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim intYear As Integer

intYear = 2014

strSQL = "Select top 1 * from tbl_Test"

Set rst = New ADODB.Recordset

rst.Open strSQL, CurrentProject.Connection, 1

strSQL2 = "Select "

For Each fld In rst.Fields

    If InStr(1, fld.Name, "Week Ending") > 0 And InStr(1, fld.Name, intYear)>0 Then

        strSQL2 = strSQL2 & "[" & fld.Name & "],"

    End If

Next fld

If Right(strSQL2, 1) = "," Then strSQL2 = Left(strSQL2, Len(strSQL2) - 1)

strSQL2 = strSQL2 & " from tbl_Test"

End Sub