Sql 将筛选的查询追加到表

Sql 将筛选的查询追加到表,sql,vba,ms-access,Sql,Vba,Ms Access,我为这些错误感到抱歉 这是我在这里的第一篇文章。 我将以另一种方式提问 我有这张桌子 例如,您可以看到,资产账户之间没有链接 和建立4个帐户, 但是资产账户的(id_ref)是固定资产账户和固定资产账户的父亲 是(building4)账户的父亲, 这称为treeview的递归方法。 因此,我可以通过调用SetQueryProperty函数来获取属于资产帐户的所有递归帐户 我的代码可以做到这一点 Private Sub Command4_Click() If Me.Combo5 = &q

我为这些错误感到抱歉 这是我在这里的第一篇文章。 我将以另一种方式提问
我有这张桌子

例如,您可以看到,资产账户之间没有链接
和建立4个帐户,
但是资产账户的(id_ref)是固定资产账户和固定资产账户的父亲
是(building4)账户的父亲,
这称为treeview的递归方法。
因此,我可以通过调用SetQueryProperty函数来获取属于资产帐户的所有递归帐户
我的代码可以做到这一点

    Private Sub Command4_Click()
If Me.Combo5 = "" Or IsNull([Combo5]) Then
MsgBox "enter id-ref field "
Me.Combo5.SetFocus
Exit Sub
End If
   Dim sInput As String
    Do
      sInput = Me.Combo5
      If Len(sInput) = 0 Then Exit Sub
      If IsNumeric(sInput) Then
         'build the query filter
         sInput = "Ref_ID In (" & TypeSubTypes(CLng(sInput)) & ")"
''                  'set the filter property
  SetQueryProperty "qry", "Filter", sInput
         'turn on the filter property
SetQueryProperty "qry", "FilterOnLoad", True
'open filterd query
 DoCmd.OpenQuery "qry"
 'insert data
 ' the problemis here as you see "insert into" statement  append all records in (qry-query) without making filtering
   
   DoCmd.RunSQL "INSERT INTO qry1 ([AccName],[db1],[cr1],[db22],[cr22],[db3],[cr3],[AccParent],[AccId],[Ref_ID],[archeive],[datearc])" & _
"SELECT [AccName],[db1],[cr1],[db22],[cr22],[db3],[cr3],[AccParent],[AccId],[Ref_ID],'snapshot ',now()" & _
"FROM qry"
'open (qry1)table after insert into
DoCmd.OpenTable "qry1"
' WHERE ??????????' i dont know how write the filter condition to append 'filtered query only correctly
         'turn of the Filter (for next open)
              Else
         'invalid input, clear it for next input try
         sInput = ""
      End If
   Loop While Len(sInput) = 0
 
End Sub
Function TypeSubTypes(IDn As Long) As Variant
   Dim rs As DAO.Recordset
   Dim lCount As Long
   Dim sSql As String
   Dim sResult As String
   sSql = "SELECT Ref_ID FROM TblAccounts WHERE AccParent=" & IDn
   Set rs = CurrentDb.OpenRecordset(sSql)
   With rs
      If Not .BOF Then
         'records to report
         .MoveFirst
         Do
         'lower levels exist?
            lCount = DCount("Ref_ID", "TblAccounts", "AccParent=" & !Ref_ID)
            If lCount = 0 Then
               'no lower level for this id
               sResult = sResult & "," & !Ref_ID
            Else
               'lower level(s) exist for this ID
               'note function calls itself to get the lower levels
               sResult = sResult & "," & TypeSubTypes(!Ref_ID)
            End If
            .MoveNext
         Loop Until .EOF
      End If
   End With
   rs.Close
   Set rs = Nothing
'   Debug.Print IDn & sResult
   TypeSubTypes = IDn & sResult
End Function
'**********************************************
'SET a Defined Query Property
'after LPurvis in
'http://www.UtterAccess.com/forum/Set-query-property-VBA-t1713084.html
Sub SetQueryProperty(strQueryName As String, strPropertyName As String, varPropVal)
On Error Resume Next
    Dim db As Database
    Dim qdf As QueryDef
    Dim prp As DAO.Property
    Set db = CurrentDb
    Set qdf = db.QueryDefs(strQueryName)
    With qdf
        Set prp = qdf.Properties(strPropertyName)
        If Err Then
            Set prp = .CreateProperty(strPropertyName, dbText, varPropVal)
            .Properties.Append prp
        End If         'Else
            prp.Value = varPropVal
    End With
     Set prp = Nothing
    Set qdf = Nothing
    Set db = Nothing
End Sub
但是结果“insert-into..”语句是

但是我需要这个结果

所有必要的代码都应作为文本,而不是链接和图像包含在问题中。您的查询显示拼写错误的查询名称-“g”而不是“q”。还需要在每一个连续的行前面留出空间,这样文本在编译时就不会同时运行。为什么要插入查询而不是表?代码正在构建和设置查询对象的过滤器。那么,为什么在SQL语句构造中需要额外的筛选条件呢?以有问题的文本表形式提供示例数据和所需结果。那么为什么在SQL语句构造中需要其他筛选条件?那么,向表(qry1)中添加过滤查询的正确代码是什么呢?以有问题的文本表的形式提供示例数据和所需结果?我会尽快这样做。我很惊讶!为什么我不能在这个论坛上传一个样本数据库?因为这样的模式是,一个问题所需的所有信息都应该在问题叙述中,而不是链接到其他网站或下载。