【问题标题】:Simple access query简单的访问查询
【发布时间】:2013-02-19 17:06:17
【问题描述】:

我正在尝试在 Access 中创建查询。

假设,例如,我有四个字段:数字 1-26、字母 A-Z、26 个名称和 26 个城市,因此一条记录可能是:2, B, Jane, New York

我想创建并保存一个新查询: 数字字段、字母字段和名称字段。我希望在“A”或“B”上过滤字母字段,并且名称字段具有表达式,因此它始终为 0。

这将成为一个循环,因此它将创建 13 个查询(A/B、C/D 等)。

似乎在 VBA 中使用这个过程而不是在 Access 宏构建器中会更好,因为我不仅需要循环这个过程,而且我还需要 2 个相似的表(相同的字段名称,不同的值)运行它。

【问题讨论】:

  • 我认为您不想创建多个查询。您想创建一个查询并使用宏/VBA 使用多个输入参数运行它。
  • 使用查询设计窗口创建查询,然后在条件行添加参数,例如:[enter a number: ] 看看你是否得到了你想要的,然后转向更高级的方法,比如就像使用参数形式一样。

标签: ms-access vba


【解决方案1】:

您可以使用记录集在 VBA 中运行查询,然后从那里处理数据:

Sub YourQueries(ByVal pstrCol1 As String, ByVal pstrCol2 As String, ByVal pstrCol3 As String, ByVal pstrCol4 As String)
Dim rs As Recordset
Dim strSQL As String

' Change types above to match what's actually in the table

strSQL = "SELECT YourColumn1, YourColumn2, YourColumn3, YourColumn4 "
strSQL = strSQL & " WHERE "
strSQL = strSQL & "YourColumn1='" & pstrCol1 & "'"
strSQL = strSQL & " AND YourColumn1='" & pstrCol1 & "'"
strSQL = strSQL & " AND YourColumn1='" & pstrCol1 & "'"
strSQL = strSQL & " AND YourColumn1='" & pstrCol1 & "'"

Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
While Not rs.EOF
    For i = 0 To 3
     Debug.Print rs.Fields(i) & " is Column" & Format(i)
    Next i
    rs.MoveNext
Wend
rs.Close
Set rs = Nothing
End Sub

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多