我在访问中创建了表单和子表单,一对多关系。表单有房间ID和文本框号。床的形式,我希望按床名加床。
如何将子表单中的记录数限制为=<没有。床?
EG。如果我没有。床价值4我想阻止用户在子表格中添加第5张床。我是相当新的访问,所以详细的解释将不胜感激。
答案 0 :(得分:1)
以下是:
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)
此外,修改主窗体的 Current 事件,如下所示:
Private Sub Form_Current()
Forms(Me.Name)!NameOfYourSubformControl.Form.LimitRecords
End Sub
并将 LimitRecords 从私有子更改为公共功能:
Public Function LimitRecords()