查询保留旧条件&子窗体不会刷新

lg40wkob  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(345)

我目前正在重新设计我公司的access数据库,从零开始,没有任何经验,因为上一次对它的结构进行编辑是在2006年,它基本上是一个有20万条记录的列表,太慢了(目前的测试与工作功能已经显示出显着的改善,耶!)
我试图创建一个显示查询结果的子窗体,但查询本身依赖于multiselect。查询作用于客户id,并基于所选客户返回结果(这很有效)
我一直在寻找解决办法,但是,我就是不知道该怎么办。我要处理的问题有两个方面,我怀疑是相关的,但首先是一点背景。
首先,子窗体只是拒绝直观地更新。虽然查询包含所需的结果,但子窗体根本不更新,我尝试过refresh,docmd.requery,随便你说,唯一有效的方法就是基本上“重新附加”源。无法从“surface”访问任何刷新显示结果的方式。
第二,查询似乎“保留”了以前选择的任何客户机。即使它可以从所有客户机(我写了一个'没有选择意味着一切')到特定的客户机,在选择之间,它保持了从上一个选择的标准,并提出了他们。
“dashboard”是嵌套子窗体选项卡generalsubform的窗体,qrytetrageneral是用作子窗体源的查询。多客户是多选形式。

Sub ListAll_Click() [access vba]

Set MyDB = CurrentDb
flgSelectAll = 0

Set qdef = MyDB.QueryDefs("qryTetraGeneral")

'strSQL is based on the SQL conversion of the SelectClientAll query
'the final AND clause will be appended by strWhere which is based on the MultiList Selection
'If there is no selection the query will default to All active Subs

 strSQL = "SELECT [EQ-TETRA_ISSI].ISSI,[EQ-TETRA_TEI].TEI, [EQ-TETRA_ISSI].Active," & _
    " [EQ-TETRA_TEI].Activation, Client_Table.[Customer Name], Client_Table.[Customer ID], [EQ-TETRA_REQFUL].ReqType, SERVICEREQUEST_TABLE.REQNUM," & _
    " SERVICEREQUEST_TABLE.REQDATE, SERVICEREQUEST_TABLE.RESPUSER" & _
    " FROM ((Client_Table INNER JOIN [EQ-TETRA_ISSI] ON Client_Table.[External Customer Index] = [EQ-TETRA_ISSI].[Customer Index])" & _
    " INNER JOIN SERVICEREQUEST_TABLE ON Client_Table.[External Customer Index] = SERVICEREQUEST_TABLE.Index) INNER JOIN ([EQ-TETRA_TEI]" & _
    " INNER JOIN [EQ-TETRA_REQFUL] ON [EQ-TETRA_TEI].TEI = [EQ-TETRA_REQFUL].TEI) ON (SERVICEREQUEST_TABLE.REQNUM = [EQ-TETRA_REQFUL].Reqnum)" & _
    " AND ([EQ-TETRA_ISSI].ISSI = [EQ-TETRA_REQFUL].ISSI)" & _
    " WHERE ((([EQ-TETRA_TEI].TEI)=[EQ-TETRA_REQFUL].[TEI]) AND (([EQ-TETRA_ISSI].Active)=True) AND (([EQ-TETRA_TEI].Activation)=True) AND ("

'extracts selection from list and creates SQL line to be added to strSQL

 For i = 0 To MultiCustomer.ListCount - 1
    If MultiCustomer.Selected(i) Then
        flgSelectAll = flgSelectAll + 1
        strIN = strIN & "([EQ-TETRA_ISSI].[Customer Index]=" & MultiCustomer.Column(0, i) & ")" & " OR "
    End If
 Next

 If flgSelectAll = 0 Then
    strSQL = "SELECT [EQ-TETRA_ISSI].ISSI,[EQ-TETRA_TEI].TEI, [EQ-TETRA_ISSI].Active," & _
    " [EQ-TETRA_TEI].Activation, Client_Table.[Customer Name], Client_Table.[Customer ID], [EQ-TETRA_REQFUL].ReqType, SERVICEREQUEST_TABLE.REQNUM," & _
    " SERVICEREQUEST_TABLE.REQDATE, SERVICEREQUEST_TABLE.RESPUSER" & _
    " FROM ((Client_Table INNER JOIN [EQ-TETRA_ISSI] ON Client_Table.[External Customer Index] = [EQ-TETRA_ISSI].[Customer Index])" & _
    " INNER JOIN SERVICEREQUEST_TABLE ON Client_Table.[External Customer Index] = SERVICEREQUEST_TABLE.Index) INNER JOIN ([EQ-TETRA_TEI]" & _
    " INNER JOIN [EQ-TETRA_REQFUL] ON [EQ-TETRA_TEI].TEI = [EQ-TETRA_REQFUL].TEI) ON (SERVICEREQUEST_TABLE.REQNUM = [EQ-TETRA_REQFUL].Reqnum)" & _
    " AND ([EQ-TETRA_ISSI].ISSI = [EQ-TETRA_REQFUL].ISSI)" & _
    " WHERE ((([EQ-TETRA_TEI].TEI)=[EQ-TETRA_REQFUL].[TEI]) AND (([EQ-TETRA_ISSI].Active)=True) AND (([EQ-TETRA_TEI].Activation)=True)) ORDER BY [EQ-TETRA_ISSI].ISSI;"
Else
    strWhere = Left(strIN, Len(strIN) - 3) & ")) ORDER BY [EQ-TETRA_ISSI].ISSI;"
    strSQL = strSQL + strWhere
End If

qdef.SQL = strSQL
Call CallRefreshForm([Forms]![Dashboard])

For Each ListObject In MultiCustomer.ItemsSelected
    MultiCustomer.Selected(ListObject) = False
Next ListObject

现在,不可否认的是,代码是由我的前辈留下的东西拼凑而成的,sql语句看起来一团糟,但是,它工作正常,并根据标准显示所需的结果,目前,这对我来说已经足够好了。当我知道所有需要的特性都在工作时,我可能会尝试将其缩小。

iyfjxgzm

iyfjxgzm1#

有几种方法可以简化这个过程。首先,在基于列表框构建sql时,不要使用大量的 IN/OR ,只需使用 IN 一次(我这里有点空码,所以请原谅任何轻微的错误)

For i = 0 To MultiCustomer.ListCount - 1
    If MultiCustomer.Selected(i) Then
        strSQL = strSQL & MultiCustomer.Column(0, i) & "," 
    End If
Next i
If Right(strSQL,1)="," Then strSQL=Left(strSQL,Len(strSQL)-1)
If Len(strSQL)>0 Then
    strSQL="SELECT [EQ-TETRA_ISSI].ISSI,[EQ-TETRA_TEI].TEI, [EQ-TETRA_ISSI].Active," & _
    " [EQ-TETRA_TEI].Activation, Client_Table.[Customer Name], Client_Table.[Customer ID], [EQ-TETRA_REQFUL].ReqType, SERVICEREQUEST_TABLE.REQNUM," & _
    " SERVICEREQUEST_TABLE.REQDATE, SERVICEREQUEST_TABLE.RESPUSER" & _
    " FROM ((Client_Table INNER JOIN [EQ-TETRA_ISSI] ON Client_Table.[External Customer Index] = [EQ-TETRA_ISSI].[Customer Index])" & _
    " INNER JOIN SERVICEREQUEST_TABLE ON Client_Table.[External Customer Index] = SERVICEREQUEST_TABLE.Index) INNER JOIN ([EQ-TETRA_TEI]" & _
    " INNER JOIN [EQ-TETRA_REQFUL] ON [EQ-TETRA_TEI].TEI = [EQ-TETRA_REQFUL].TEI) ON (SERVICEREQUEST_TABLE.REQNUM = [EQ-TETRA_REQFUL].Reqnum)" & _
    " AND ([EQ-TETRA_ISSI].ISSI = [EQ-TETRA_REQFUL].ISSI)" & _
    " WHERE [EQ-TETRA_TEI].TEI=[EQ-TETRA_REQFUL].[TEI] " _
    & " AND [EQ-TETRA_ISSI].Active=True " _
    & " AND [EQ-TETRA_TEI].Activation=True " _
    & " AND [EQ-TETRA_ISSI].[Customer Index] IN (" & strSQL & ")
Else
    strSQL="SELECT [EQ-TETRA_ISSI].ISSI,[EQ-TETRA_TEI].TEI, [EQ-TETRA_ISSI].Active," & _
    " [EQ-TETRA_TEI].Activation, Client_Table.[Customer Name], Client_Table.[Customer ID], [EQ-TETRA_REQFUL].ReqType, SERVICEREQUEST_TABLE.REQNUM," & _
    " SERVICEREQUEST_TABLE.REQDATE, SERVICEREQUEST_TABLE.RESPUSER" & _
    " FROM ((Client_Table INNER JOIN [EQ-TETRA_ISSI] ON Client_Table.[External Customer Index] = [EQ-TETRA_ISSI].[Customer Index])" & _
    " INNER JOIN SERVICEREQUEST_TABLE ON Client_Table.[External Customer Index] = SERVICEREQUEST_TABLE.Index) INNER JOIN ([EQ-TETRA_TEI]" & _
    " INNER JOIN [EQ-TETRA_REQFUL] ON [EQ-TETRA_TEI].TEI = [EQ-TETRA_REQFUL].TEI) ON (SERVICEREQUEST_TABLE.REQNUM = [EQ-TETRA_REQFUL].Reqnum)" & _
    " AND ([EQ-TETRA_ISSI].ISSI = [EQ-TETRA_REQFUL].ISSI)" & _
    " WHERE [EQ-TETRA_TEI].TEI=[EQ-TETRA_REQFUL].[TEI] " _
    & " AND [EQ-TETRA_ISSI].Active=True " _
    & " AND [EQ-TETRA_TEI].Activation=True "
End If
strSQL=strSQL & " ORDER BY [EQ-TETRA_ISSI].ISSI;"

我也不认为你需要 [EQ-TETRA_TEI].TEI=[EQ-TETRA_REQFUL].[TEI]WHERE 子句,因为您已经在使用它将两个表连接在一起。
当做,

相关问题