Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on an application that should allow user select a code from a list box, and this value should be passed as a parameter to a sub-form(continuous) which is tied to an SQL Query to populate the form with values returned from the query.

How do I tie the listbox on the parent form to the continuous form (child form) in such a way that a change in the listbox is registered in the subform and it(child form) is re-populated accordingly?

The code below is what I currently have in my Parent form to trigger the change in the subform. But I am still missing the bit where I trigger the reloading of the information in the subform with this.

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])

   );

This is a field in my sub-form which I would like to tie to my query as source of parameter for the query.

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

The code below is code in my parent form I test connects directly to the Query that I created and passes the parameter directly to the query. However, since my sub-form is itself tied to the query, I need a way to get to the subform query and pass the information I need passed and invoke an update on the form afterwards.

  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
share|improve this question
Why do you feel the need to do this, against all the common ways of setting up a subform? – Remou Aug 13 '12 at 16:25
Are you really using a DAP? They went out of date sometime ago. – Remou Aug 13 '12 at 16:28
I am relatively new to ACCESS so I am possible unaware of the common ways to handle this sort of situation with subforms – Kobojunkie Aug 13 '12 at 16:39
You might like to look at the Northwind sample database, the code is not great, but it is very useful on ways to manage subforms. in general, you set the recordsource of a subform to a table name (not the best), a query or an SQL string. The subform is further limited by link child and master fields, so the query or sql can be quite simple. – Remou Aug 13 '12 at 16:43
Working from the above example, the record source would be the sql of spgetAttributeByUNSPSC and unspscstring could either be a reference to a control, a link field, or something added to the sql on load. – Remou Aug 13 '12 at 16:45
show 6 more comments

1 Answer

up vote 0 down vote accepted

As long as the listbox allows only one item, you can use the link child and master fields of the subform to do this.

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

Alternatively, you can set the Record Source of the form object of the subform control in VBA:

sSQL = "SELECT ID,Stuff FROM Table WHERE ID=" & Me.MyNumericListBox
Me.MySubformControlName.Form.RecordSource = sSQL
share|improve this answer
what I thought to do was pass the parameter from my parent form and then have the child form take that and re-run the query. Is that not how it should be done? – Kobojunkie Aug 13 '12 at 15:37
I am afraid the code you posted could not run in MS Access. – Remou Aug 13 '12 at 15:50
I just now debugged that piece of the code. Please see new code and comment at the end. – Kobojunkie Aug 13 '12 at 16:34
I saw your new code and added comments above. – Remou Aug 13 '12 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.