【发布时间】:2015-06-28 02:26:40
【问题描述】:
我在 Access 中有一个要从 Excel 查询的表。我需要有关 sql 语句的帮助。 首先,我需要根据 Where 子句中指示的 SampleType 匹配条件过滤表。只有 3 个选项:“mel”、“col”或“lun”。最终目标是将所选 SampleType(s) 的 SampleID 列中不同记录的数量传递给变量并将其输出到 UserForm 上。 SampleType 列有重复值,只需要计算不同的值。根据我在网上找到的 'COUNT(DISTINCT(' + column_name + ')) 会起作用,但我不确定如何使用它。
提前感谢您的帮助
Dim var6 As String
Dim var7 As String
Dim var8 As String
var6 = "mel"
var7 = “lun”
var8 = “col”
SQLwhere = "WHERE "
If CheckBox5 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var6 & "%" & "' AND "
‘count a total district records in column SampleID.
End If
If CheckBox6 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var7 & "%" & "' AND "
End If
If CheckBox7 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var & "%" & "' AND "
End If
StrSql = "SELECT * FROM [Table1] "
'Remove the last AND applicable
If SQLwhere = "WHERE " Then
SQLwhere = ""
Else
SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5)
End If
StrSql = StrSql & SQLwhere
【问题讨论】:
-
您实际上是在以一种不清楚的方式提出多个问题。请缩小您的帖子范围并突出显示您要解决的关键问题。最好的问候,
标签: sql excel vba ms-access subquery