MS Access通过参数化查询动态填充连续表单子表单

时间:2012-08-13 14:45:17

标签: vba ms-access vbscript

我正在开发一个应该允许用户从列表框中选择代码的应用程序,并且该值应作为参数传递给子表单(连续),该子表单与SQL查询绑定以填充表单查询返回的值。

如何将父表单上的列表框绑定到连续表单(子表单),以便在子表单中注册列表框中的更改并相应地重新填充它(子表单)?

以下代码是我目前在父表单中触发子表单更改的代码。但我仍然错过了用这个触发重新加载子表单中信息的位置。

storedproc

SELECT tblACCOUNT.ACCOUNT_LABEL, tblACCOUNTValue.[ACCOUNTVALUE_VALUE]
FROM ((tblUPS INNER JOIN tblProductUPS ON tblProductUPS.[PRODUCTUPS_UPS] = tblUPS.[UPS_CODE]) INNER JOIN tblACCOUNT ON tblUPS.UPS_ID = tblACCOUNT.ACCOUNT_UPSID) INNER JOIN tblACCOUNTValue ON tblACCOUNTVALUE.[ACCOUNTVALUE_ACCOUNTID]= tblACCOUNT.[ACCOUNT_ID]
WHERE TBLPRODUCTUPS.[PRODUCTUPS_PRODUCTID] =   (SELECT tblProductLevel.[PRODUCTLEVEL_ID] 

FROM  tblProductLevel WHERE tblProductLevel.[PRODUCTLEVEL_Code] IN ( [UPSIDS])

   );

这是我的子表单中的一个字段,我想将其作为查询的参数来源与我的查询联系起来。

Public Property Let qString(unstring As String)
    If Not IsNull(unstring) And Len(unstring) > 0 Then
            Currentstring = unstring
    End If
End Property

下面的代码是我的父表单中的代码我测试直接连接到我创建的Query并将参数直接传递给查询。但是,由于我的子表单本身与查询绑定,我需要一种方法来获取子表单查询并传递我需要传递的信息,然后在表单上调用更新。

  Private Sub LoadSubform(unspscstring As String)
On Error GoTo Err_LoadSubform_Change

            Dim dbs As Database
            Dim strSQL As String
            Dim strSelect  As String
            Dim strQueryName As String
            Dim qryDef As QueryDef
             Dim rst As Recordset
             Dim prmOne As DAO.Parameter

            Set dbs = CurrentDb
            'then we'll open up the query:
            Set qryDef = dbs.QueryDefs("spgetAttributeByUNSPSC")
            'Now we'll assign values to the query using the parameters option:
                'link your DAP.Parameters to the query
                'Set prmOne = qryDef.Parameters!param_one
                'prmOne = unspscstring
             qryDef.Parameters(0) = unspscstring
             'Now need to somehow trigger an update on the subform

            'Close all objects
            rst.Close
            qryDef.Close
            Set rst = Nothing
            Set qryDef = Nothing
Bye_LoadSubform_Change:
                     Exit Sub

Err_LoadSubform_Change:
Beep:                      MsgBox Error$, 16, "Select Failed"
                     Resume Bye_LoadSubform_Change

End Sub

2 个答案:

答案 0 :(得分:1)

只要列表框只允许一个项目,您就可以使用子表单的链接子字段和主字段来执行此操作。

Link Child Fields: ID; ListFieldMatch
Link Master Fields : ID; MyListBox

或者,您可以在VBA中设置子窗体控件的窗体对象的记录源:

sSQL = "SELECT ID,Stuff FROM Table WHERE ID=" & Me.MyNumericListBox
Me.MySubformControlName.Form.RecordSource = sSQL

答案 1 :(得分:0)

sSQL =“SELECT ID,Stuff FROM Table WHERE ID =”& Me.MyNumericListBox Me.MySubformControlName.Form.RecordSource = sSQL

给出错误2467运行时错误

“您输入的表达式是指已关闭或不存在的对象。