【发布时间】:2017-03-15 23:10:38
【问题描述】:
我在工作中为我的团队构建了一个 MS Access 课程管理工具。我有一个表格,最终用户可以在其中添加特定课程的培训材料。表格中的一个字段要求提供该材料用于的特定医院。要填充此表单,用户单击一个按钮,该按钮会显示一个表单(“sbfrmcoursematerialsite”),其中包含我们系统中所有可能的医院的列表,用户可以选择该列表。使用以下 SQL 代码填充表单:
SELECT pklistEntSites.Site FROM pklistEntSites UNION SELECT " [Enterprise]" FROM pklistEntSites
ORDER BY pklistEntSites.Site;
此多选连接医院缩写并创建一个字符串以插入“sbfrmTrainingElements”中的“Site”字段(这是名为 txtSite 的 txtBox 的控制源)。我的问题是,当我单击“sbfrmcoursematerialsite”上的“确定”按钮时,我收到一条错误消息:无法执行此操作。 “sbfrmcoursematerialsite”表单中包含的我的 VBA 代码如下。连接函数正在工作,但错误出现在代码中的粗体行上。我是否没有正确引用要添加数据的子表单?
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:还要强调
sbfrmTrainingElements必须是表单frmFullCourseInfo上的子表单控件的名称。