Question How to Quit data entry on Main form when clicking on subform?

RxDBA1

New member
Local time
Today, 02:59
Joined
Jul 29, 2009
Messages
3
I have a form that contains a subform. If the user has selected to enter a new record, but does not enter anything on the new record, when they click on the subform which contains a list of their records they recieve an error message "The field'Entries.Subject' cannot contain a Null Value because the Required property for this field is set to True. Enter a value in this field."
Basically, I want to write a code that if the Main form loses focus, and the subject field is Null, do not save the record and do not prompt the user with validation rules.
Any idea on how to do that? I am a newbe at Access, and the Code I have put together (which is probably very wrong) so far is below:
Private Sub Form_LostFocus()
IF DatRequest.EditMode = dbEditAdd Then
If DatRequest.recordset("Subject").Value="" Then
DatRequest.Recordset.Quit
Else
DatRequest.Recordset.Save
End if

Endif

EndSub

Thanks in Advance.
 
How about keeping your SubForm disabled until all of the necessary controls have been completed on the MainForm? That will keep the SubForm from ever getting the focus.
 
Alternatively, you can put error-checking in the main form to disallow switching to subforms, which at least will provide a less "code jargon" message for the user.

Here's an example from my database:
Code:
Private Sub subformDistricts_Enter()
    If IsNull(Me.GroupName) Then
        MsgBox "Don't try to enter subform details before the group name.", vbCritical, "Improper Data Entry"
        Me.GroupName.SetFocus
        Exit Sub
    End If
    If IsNull(Me.LiaisonID) Then
        MsgBox "Don't try to enter subform details before assigning a liaison.", vbCritical, "Improper Data Entry"
        Me.LiaisonID.SetFocus
    End If
End Sub
(note that this is in the MAIN form's code entry for the subform - when you click on a subform once and just get the bounding box?)
 
Last edited:
When you are about to focus inside the sub-form, you have an EXIT event from the main form followed by an ENTER event in the sub-form followed by the GETFOCUS event on the control inside the sub-form. Look up EXIT and ENTER events to see if there is some place where you can override the event, which stops the whole chain of events that would normally fire based on what you describe. You can tell that it is possible to override an event because its call spec includes (Cancel as Integer) as a formal argument. Just run your test in that event and if you want to stop the cascade of events, include a line that says

Cancel = -1

That should do it. Just search for the events associated with a sub-form transition in the help files.
 
Thanks Everyone very helpful. I discovered my problem was related to the fact that I was defaulting a value on add new record, which was preventing users from being able to go to the subform. I decided to use an unbound combobox inplace of the default. :)
 

Users who are viewing this thread

Back
Top Bottom