我在access中创建了表单和子表单,一对多的关系。表格上有房间号和文本框号.在床上,我想按床号加床。
如何将子形式的记录数量限制为=< no.床上的?
例如:如果我没有。对于床的价值4,我想防止用户添加第5张床的亚形式,我是相当新的访问,所以详细的解释将不胜感激。
发布于 2016-09-04 10:53:05
以下是如何:
Public Sub SetFormAllowAdditions( _
ByVal frm As Form, _
ByVal lngRecordCountMax As Long)
' Limit count of records in (sub)form to that of lngRecordCountMax.
' 2004-10-06, Cactus Data ApS, CPH
'
' Call in (sub)form:
'
' Private Sub LimitRecords()
' Const lngRecordsMax As Long = 5
' Call SetFormAllowAdditions(Me.Form, lngRecordsMax)
' End Sub
'
' Private Sub Form_AfterDelConfirm(Status As Integer)
' Call LimitRecords
' End Sub
'
' Private Sub Form_AfterInsert()
' Call LimitRecords
' End Sub
'
' Private Sub Form_Open(Cancel As Integer)
' Call LimitRecords
' End Sub
Dim booAllowAdditions As Boolean
With frm
booAllowAdditions = (.RecordsetClone.RecordCount < lngRecordCountMax)
If booAllowAdditions <> .AllowAdditions Then
.AllowAdditions = booAllowAdditions
End If
End With
End Sub它适用于记录的常量最大计数。对于变量计数,替换以下行:
Const lngRecordsMax As Long = 5通过以下方式:
Dim lngRecordsMax As Long
lngRecordsMax = Nz(Me.Parent![no. of beds].Value, 1)另外,修改主窗体的当前事件,如下所示:
Private Sub Form_Current()
Forms(Me.Name)!NameOfYourSubformControl.Form.LimitRecords
End Sub并将LimitRecords从专用子函数更改为公共函数
Public Function LimitRecords()https://stackoverflow.com/questions/39315826
复制相似问题