Validate code..where

ian_ok

Registered User.
Local time
Today, 15:37
Joined
May 1, 2001
Messages
90
I have got this code from a previous posting, but am unsure where to place it on my form?

Ian

Dim currctl As Integer, numctls As Integer
Dim ctl As Control
numctls = Forms!frmMaterials.Count 'counts the number of controls on
form

For currctl = 0 To numctls - 1
Set ctl = Me(currctl)

' do we want to check this control?

If ctl.Tag = "validate" Then
If IsNull(ctl) Then
MsgBox "You have not filled in information that is required by this
database. Please fill in the fields highlighted in YELLOW.", 48, "Field
empty"
ctl.BackColor = 65535
ctl.SetFocus
Exit Sub
End If
End If
Next currctl


'If all information required is filled out goto a new record

DoCmd.GoToRecord , , acNewRec
Me.Title.SetFocus
 
That code was mine. It is a likely case that you need to put the code on an event where it will fire before the record is saved.

Do you have a "Save Record" command button??
Do you have your form cycled to current record only?

Put the code on the event of the command button, BEFORE the save record command.

If you DON'T have your form cycling to current record, the put the code ALSO on the OnCurrent event of your form.
 
Thank you jwindon, tho code is fantastic.

Just one question, does the code just work on a certain amount of fields with validate within the tag.

The reason for asking this, is that I tried in my current d/b and I got a runtime error saying something like the control number I specified is greater than the number of controls.

Yet it worked perfect on a test d/b I did with 2 fields.

Thanks once again.

Ian
 
Did you change this line to your form name?

numctls = Forms!frmMaterials.Count 'counts the number of controls on
form

Also, check to make sure that all your controls needed have the correct "tag".
 
Have sorted it now, I think it was because the close button I have is a pop up menu (different form) and wasn't looking at the right form.

Thanks once again.

Ian
 

Users who are viewing this thread

Back
Top Bottom