当我进入更新后方法时,全局whereAtt没有保持值。我是如何调用全局变量或设置代码的?
Option Compare Database
Option Explicit
Dim whereAtt As String
Private Sub cmbAnalyst_AfterUpdate()
If cmbAnalyst.ListIndex <> -1 Then
whereAtt = whereAtt & " And Analyst = 'me.cmbAnalyst'"
Call queryBuilder
End If
End Sub
Private Sub Form_Load()
whereAtt = "Select * from tblActionLog where LogID is not null"
cmbAnalyst.RowSource = "SELECT DISTINCT Analyst FROM tblActionLog"
Call queryBuilder
End Sub
Public Sub queryBuilder()
testTable.RowSource = whereAtt
End Sub
答案 0 :(得分:1)
您的代码会改变WHERE
语句中的SELECT
子句,就像这样......
whereAtt = whereAtt & " And Analyst = 'me.cmbAnalyst'"
该条件会将查询返回的行限制为仅存储tblActionLog.Analyst
中存储文字文本字符串'me.cmbAnalyst'的行。
我怀疑您希望tblActionLog.Analyst
与cmbAnalyst
组合框中所选值匹配的行。
如果这是正确的,请构造字符串以包含组合框的值而不是其名称:
whereAtt = whereAtt & " And Analyst = '" & Me.cmbAnalyst.Value & "'"
关于“当我进入后续更新方法时,whereAtt没有持有值”,我不明白那里发生了什么。看起来每次更新后都会向查询添加另一个“和Analyst = ...”段。但我不明白为什么 whereAtt 的值会完全消失。
我建议您在更改 whereAtt 的行之前和之后添加Debug.Print
语句:
Debug.Print "Before: " & whereAtt
Debug.Print "cmbAnalyst.Value: " & Me.cmbAnalyst.Value
whereAtt = whereAtt & " And Analyst = '" & Me.cmbAnalyst.Value & "'"
Debug.Print "After: " & whereAtt
希望在立即窗口中查看Debug.Print
输出时,情况会很明显。如果没有,请告诉我们Debug.Print
向您展示的内容。
答案 1 :(得分:0)
您需要使用:
Public whereAtt As String
要确保传递值,您还可以执行此操作:
Option Compare Database
Option Explicit
Public whereAtt As String
Private Sub cmbAnalyst_AfterUpdate()
If cmbAnalyst.ListIndex <> -1 Then
whereAtt = whereAtt & " And Analyst = 'me.cmbAnalyst'"
queryBuilder(whereAtt)
End If
End Sub
Private Sub Form_Load()
whereAtt = "Select * from tblActionLog where LogID is not null"
cmbAnalyst.RowSource = "SELECT DISTINCT Analyst FROM tblActionLog"
queryBuilder(whereAtt)
End Sub
Public Sub queryBuilder(whereAtt as String)
testTable.RowSource = whereAtt
End Sub