MS Access - 使用多个数据源的SQL追加查询

时间:2018-01-05 18:25:50

标签: sql vba ms-access

我正在启动下面的SQL附加查询,它运行正常。

AppendSQL = "INSERT INTO Netting_Determinations_List ([Counterparty ID], " & _
                "[Counterparty Name], [Counterparty Type], [Counterparty Subtype]) " & _
            "SELECT Repository_Redux.[Counterparty ID], " & _
                "Repository_Redux.[Counterparty Name], " & _
                "Repository_Redux.[Counterparty Type], " & _
                "[Counterparty Subtype] " & _
            "FROM Repository_Redux " & _
            "WHERE Repository_Redux.[Counterparty ID] IN (" & strCriteria & ")"

我面临的问题是我希望在上述查询中包含其他数据点,这些数据点在 的文本框中 。是否可以为同一查询中的那些运行整个SELECT命令,还是应该在上述追加查询之后运行更新查询以获取这些值?

编辑 - 以下是实施建议解决方案的修订程序:

Dim db As DAO.Database
Dim AppendQdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim AppendSQL As String

Set db = CurrentDb()
Set AppendQdf = db.QueryDefs("Qry_Append_Counterparty_Data")

For Each varItem In Me!Lst_CPList.ItemsSelected
   strCriteria = strCriteria & ",'" & Me!Lst_CPList.Column(0, varItem) & "'"
Next varItem

If Len(strCriteria) = 0 Then
   MsgBox "You did not select anything from the list." _
          , vbExclamation, "Nothing To Find!"
  Exit Sub
End If

strCriteria = Right(strCriteria, Len(strCriteria) - 1)

AppendSQL = "INSERT INTO Netting_Determinations_List ([Counterparty ID], [Counterparty Name], [Counterparty Type], [Counterparty Subtype], [DTCC_AVOX_Registered_LEI_CICI], [Data Point 1], " & _
        "[Data Point 2],[Data Point 3],[Data Point 4],[Data Point 5], [Matrix Legal Form], [Matrix Governing/Authorizing Power], [OnBoardings Color Determination], [Matrix Clarification]) " & _
        "SELECT Repository_Redux.[Counterparty ID], Repository_Redux.[Counterparty Name], Repository_Redux.[Counterparty Type], [Counterparty Subtype], [DTCC_AVOX_Registered_LEI_CICI], " & _
        "[Forms]![Frm_Master_Form]![Txt_Input_1] AS [Data Point 1], [Forms]![Frm_Master_Form]![Txt_Input_2] AS [Data Point 2], " & _
        "[Forms]![Frm_Master_Form]![Txt_Input_3] AS [Data Point 3], [Forms]![Frm_Master_Form]![Txt_Input_4] AS [Data Point 4], " & _
        "[Forms]![Frm_Master_Form]![Txt_Input_5] AS [Data Point 5], [Forms]![Frm_Master_Form]![Cbo_LegalForm] AS [Matrix Legal Form], " & _
        "[Forms]![Frm_Master_Form]![Cbo_Status] AS [Matrix Governing/Authorizing Power], [Forms]![Frm_Master_Form]![Txt_Color] AS [Color], " & _
        "[Forms]![Frm_Master_Form]![Txt_Matrix_Clarification] AS [Matrix Clarification] FROM Repository_Redux " & _
        "WHERE Repository_Redux.[Counterparty ID] IN (" & strCriteria & ")"

2 个答案:

答案 0 :(得分:2)

可以连接对文本框的引用。

AppendSQL = "INSERT INTO Netting_Determinations_List ([Counterparty ID], " & _
                "[Counterparty Name], [Counterparty Type], [Counterparty Subtype], " & _
                "[some field name]) " & _
            "SELECT Repository_Redux.[Counterparty ID], " & _
                "Repository_Redux.[Counterparty Name], " & _
                "Repository_Redux.[Counterparty Type], " & _
                "[Counterparty Subtype] " & _
                Me.textboxname & " AS F " & _
            "FROM Repository_Redux " & _
            "WHERE Repository_Redux.[Counterparty ID] IN (" & strCriteria & ")"

答案 1 :(得分:1)

您可以将表单字段放入查询中,如下所示:

SELECT [forms]![form1].[text0] AS myfield 
from test;

即使您不需要任何内容​​,也需要一个表来选择“来自”,但在您的情况下,这应该不是问题。