UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Limit records in subform in Access 97    
 
   
pamount
post Jul 23 2004, 12:53 AM
Post #1

New Member
Posts: 1



Hello

I have a subform on a form and I want to limit the records that can be entered in it to be no more than 100. I've tried doing this in VBA with the code below. What I get when I test this is the msgbox appearing twice and I can't delete the extra record (and it wont undo itself). How should I change the VBA?

Thanks
pamount

Option Compare Database
Option Explicit

Sub Form_Current()
Dim rst As DAO.Recordset
Set rst = Me.RecordsetClone
If rst.RecordCount >= 100 Then
Me.AllowAdditions = False
Me.Undo
MsgBox "Not allowed more than 100 plans in bag", vbOKOnly
Else
Me.AllowAdditions = True
End If
End Sub
Go to the top of the page
 
+
mishej
post Jul 23 2004, 01:15 AM
Post #2

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Do you have a .Requery or .Refresh some where in your form's code? This would fire the OnCurrent event a second time.

Perhaps a field/control has a default value being set that forces the allocation of a new AutoNumber. If a main form has a .Requery it will force a save of any record in a subform with pending data (.Data=True).

Any pending data in a subform is written (saved) before any data in the main form is saved. So if you move off a main record without saving (move to another record) the subform changes are saved even if you don't save the data in the main form.

You can use the .Dirty property to determine if there is a pending change and then clear it prior to moving to the next record. That may or may not help your problem; you may need to "step" through your code with the debugger to determine what's going on.
Go to the top of the page
 
+
mishej
post Jul 23 2004, 01:21 AM
Post #3

Retired Moderator
Posts: 11,289
From: Milwaukee, WI



Oh, and welcome to Utter Access.

To get back to your original problem of checking the number of records in the subform - you should do that in the OnCurrent event of the main form:
CODE
Sub Form_Current()
Dim rst As DAO.Recordset

Set rst = Me![color="red"]subFormControlName[/color].Form.RecordsetClone

If rst.RecordCount >= 100 Then
   Me![color="red"]subFormControlName[/color].Form.AllowAdditions = False
   Me![color="red"]subFormControlName[/color].Form.Undo
   MsgBox "Not allowed more than 100 plans in bag", vbOKOnly
Else
   Me![color="red"]subFormControlName[/color].Form.AllowAdditions = True
End If

End Sub
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th June 2013 - 05:11 AM