我为工作组建立了一个MS Access课程管理工具。我有一个表格,最终用户可以为特定课程添加培训材料。表格中的一个字段要求特定医院使用该材料。为了填充这个表单,用户点击一个按钮,弹出一个表单(“sbfrmcoursematerialsite”),列出我们系统中所有可能的医院列表,用户可以多选。MS Access VBA:正确引用一个子表格
SELECT pklistEntSites.Site FROM pklistEntSites UNION SELECT " [Enterprise]" FROM pklistEntSites
ORDER BY pklistEntSites.Site;
这多选地连接了医院的缩写,并在“sbfrmTrainingElements”创建一个字符串插入到“网站”领域(这是一个名为txtSite txtBox控制源:形式使用该SQL代码填充)。我的问题是,当我点击“sbfrmcoursematerialsite”上的Ok按钮时,出现错误消息:无法执行此操作。我的VBA代码包含在“sbfrmcoursematerialsite”表单中。串联函数正在工作,但错误在代码中以粗体显示。我没有正确引用要添加数据的子表单吗?
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
尝试添加* Form *对象参考:'[Forms]![frmFullCourseInfo]![sbfrmTrainingElements] .Form![Site]' – Parfait
@Parfait:同时强调'sbfrmTrainingElements'必须是子窗体的名称_control_形式'frmFullCourseInfo'。 – Gustav