简体   繁体   中英

MS Access VBA: Properly Referencing a SubForm

I have built an MS Access Curriculum Management tool for my group at work. I have a form where an end user is able to add Training materials for a specific course. One of the fields in the form asks for the specific hospital that the material is used for. To populate this form, the user clicks a button that brings up a form ("sbfrmcoursematerialsite") with a list of all possible hospitals in our system that the user is able to multiselect. The form is populated using this SQL code:

SELECT pklistEntSites.Site FROM pklistEntSites UNION SELECT "          [Enterprise]" FROM pklistEntSites
ORDER BY pklistEntSites.Site;

This multiselect concatenates the hospital abbreviations and creates a string to insert into the "Site" field in the "sbfrmTrainingElements" (which is the control source for a txtBox named txtSite). My issue is that when I click the Ok button on the "sbfrmcoursematerialsite" I get an error stating: Cannot Perform this Operation. My VBA code contained within the "sbfrmcoursematerialsite" form is below. The concatenation function is working but the error occurs on the line in bold in the code. Am I not properly referencing the subform that I would like add the data to?

Private Sub cmdOk_Click()
Dim teID As Integer
Me.Refresh
teID = Me.txtTrainingElementID 'Document Training Element ID

DoCmd.SetWarnings False
'Update the Site field in tblTrainingElements for the specific training      element
'DoCmd.RunSQL "UPDATE tblTrainingElements SET [Site] = '" &     Me.txtSelectedSites & "' WHERE [Training Element ID] = " & teID & ";"
'Debug.Print Me.txtSite.Value
Debug.Print [Forms]![frmFullCourseInfo]![sbfrmTrainingElements]! [Site].Value
**[Forms]![frmFullCourseInfo]![sbfrmTrainingElements]![Site].Value =     Me.txtSelectedSites**
DoCmd.SetWarnings True
'Close the site select form
DoCmd.Close
End Sub

Private Sub Form_Load()
'Show current site selection
Me.txtSelectedSites = [Forms]![frmFullCourseInfo]! [sbfrmTrainingElements]![Site]

'Pass the training element ID from the last screen to current form
Dim i As Integer
i = CInt(Me.OpenArgs)
Me.txtTrainingElementID.Value = i

End Sub

Private Sub lboAllSites_Click()
   Dim strSelected As String
   Dim varItem As Variant

    With Me.lboAllSites
        For Each varItem In .ItemsSelected
            strSelected = strSelected & "," & .ItemData(varItem)
        Next varItem
        Me.txtSelectedSites = Mid(strSelected, 2)
    End With
End Sub

Must reference the subform container control. I always give the container a name different from the object it holds, like ctrTraining.

[Forms]![frmFullCourseInfo]![ctrTraining]![Site]

Thank you for your help. I found out what the issue was. The field "Site" actually had a lookup associated with it (lookup was created by a colleague who has since left the project). I tried to remove the lookup just by deleting the information in the lookup tab, despite this it still preserved some sort of lookup functionality. What I had to do was delete this field, and create a new one with the same name. Everything is working fine now. Thanks again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM