Mandatory / Required field

noks

Registered User.
Local time
Today, 12:29
Joined
Mar 2, 2007
Messages
51
Hi guys

I'm trying to force the user to enter the Customername, Contact person and (tel or cell nbr). Everything's fine for the customer and contact person as i hav made the required fields on the table.

The problem is i the user doesn't enter the customername or contact person it complains but closes the screen and i don't want the application to do dat. I want it to complain then go back to the field that still needs to be entered.

I also need help with enforcing data in the tell or cell nbr field (i can't make dem required as it's fine not 2 hav 1 if the other is there)

Here's ma code

Private Sub Form_BeforeUpdate(Cancel As Integer)
'Before closing this form you have to check if the mandotory fields (*) have been entered else complain and don't allow the user to continue
If ValidateRecord = False Then
MsgBox ("Please enter the Customer Name or Contatct")
Me.CustomerName.SetFocus
On Error GoTo Err_cmdMainScreen_Click
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "New_Customer"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_cmdMainScreen_Click:
Exit Sub

Err_cmdMainScreen_Click:
MsgBox Err.Description
Resume Exit_cmdMainScreen_Click
Else
MsgBox ("Done")
End If

End Sub

Ma field types are text by d way.

Thanks
 
Code:
If ValidateRecord = False Then
MsgBox ("Please enter the Customer Name or Contatct")
[B]Cancel = True[/B]
Me.CustomerName.SetFocus
 
Unfortunately it still get's closed afta complainin :-(
 
So, if the user is closing the form and it needs to not close, then you'll need to put some code in the form's Unload event to cancel the close if they close it before putting in the data.

I would create a boolean flag in a standard module's general declarations section called blnClose (Public blnClose As Boolean) and then in the BeforeUpdate Event, set the flag after the Cancel = True, so
Cancel = True
blnClose = True

and then in the form's unload event put

If blnClose Then
Cancel = True
End If

Then in your form's OnCurrent event put blnClose = False so that it resets.
 
K i've tried this but am not getting anywhere. Can u kindly let me know how to set up the flag?
 
mod02.png



mod03.png



mod04.png



mod05.png
 
Thanks Bob 4 everythin. I found out that it skipped ma save command and went straight to the close command

Here's another problem

I modified ma original code so that it can let the user know specifically what's required and not entered...

I want the application to allow the user the option of entering the phone nbr or cell nbr but 1 of them has to be there.
Currently it's complainin if both r not the

Here's ma code

ElseIf IsNull(Me.PHONE_NO) Or IsNull(Me.CELL_NO) Then
MsgBox ("Please enter the Telephone or Cell Number")
Cancel = True
Me.PHONE_NO.SetFocus
Exit Sub

I've tried changin the or to and but still it complains with the same error msg.
Pls kindly advise
 
You need:

to change
Code:
ElseIf IsNull(Me.PHONE_NO) Or IsNull(Me.CELL_NO) Then
to
Code:
ElseIf (IsNull(Me.PHONE_NO) Or Me.PHONE_NO = "") [B]AND[/B] (IsNull(Me.CELL_NO) or Me.CELL_NO = "") Then
 
I got it... here's ma code

ElseIf IsNull(Me.PHONE_NO) Then
If IsNull(Me.CELL_NO) Then
MsgBox ("Please enter the Telephone or Cell Number")
Cancel = True
Me.PHONE_NO.SetFocus
Exit Sub
End If
End If

Thanks Bob 4 everything
 
Anada Q, how do i mark a thread closed i've searched but can't seem to get anythin bout dat...
 
Anada Q, how do i mark a thread closed i've searched but can't seem to get anythin bout dat...
They are actually open all of the time unless a moderator decides to close it to anymore posts. So, you get your answer and then you can forget about it, but you might have someone post something 2 years down the line. Maybe not the best, but it seems to work okay.
 
I think it would b best if a person can b able 2 specify if an issue / thread has been solved so that people can search directly the resolved 1s and if they can't find an answer then search d forum...
Ne way dat's ma 2cents worth opinion...

Thanks again
Cheez 4 now :-)
 
Submit that in the SiteSuggestions section of the forum so the site owner and moderators can see. It's not likely going to happen as I don't know whether this software supports that, but you never know.
 
Please help

I'm still having problems with keeping the form open despite trying the recommendations above. Please see my code below.

Option Compare Database
Public blnClose As Boolean

Private Sub Form_Current()
blnClose = False
End Sub

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2169 Then Response = acDataErrContinue
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnClose Then
Cancel = True
End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim EnoughInfo As Boolean
EnoughInfo = True


If Len(Me!txtDispoID & "") = 0 Then EnoughInfo = False
If Len(Me!ErrorFoundDept & "") = 0 Then EnoughInfo = False
If Len(Me!TypeOfError & "") = 0 Then EnoughInfo = False
If Len(Me!txtCheckerID & "") = 0 Then EnoughInfo = False
If Len(Me!txtPOReg & "") = 0 Then EnoughInfo = False
If Len(Me!txtErrorDate & "") = 0 Then EnoughInfo = False
If Len(Me!txtComments & "") = 0 Then EnoughInfo = False

If EnoughInfo = False Then
MsgBox ("Please review required fields")
Response = MsgBox("Do you want to exit?", vbYesNo)
If Response = vbYes Then
MsgBox "Entry was not saved", vbOKOnly
Me.Undo
DoCmd.SetWarnings False
Else
If Response = vbNo Then
Cancel = True
Me.txtComments.SetFocus
DoCmd.SetWarnings False
End If
End If
End If

End Sub
 
Krivits - Have you put a breakpoint to follow the whole process through to see what is getting set and when so that you might be able to see where it's really falling down.
 
The code is excuting all the way through. It's just that instead of the form staying open when you select "No", it closes.

I noticed in your instructions above you recommended declaring blnClose in a module delcaration versus directly on the form. Is there a reason for that? Though I've tried doing it both ways and my form still closes.
 
I'm not seeing anywhere where you set

blnClose = True (which is the part which keeps it from closing) So change to
Code:
If Response = vbYes Then
     MsgBox "Entry was not saved", vbOKOnly
     Me.Undo
     DoCmd.SetWarnings False
Else
    If Response = vbNo Then 
    Cancel = True
    [color=red][b]blnClose = True[/b][/color]
    Me.txtComments.SetFocus
End If

Also, why are you setting Warnings to False? You are going to end up with no warnings whatsoever (including when you try to save forms, etc.) That should be used extremely carefully and always reset right away and always having DoCmd.SetWarnings True in an error handler so if it glitches before they get reset it will reset them.
 
Thanks Bob!

I had a print record button on my form that was functioning before but is not longer working. Even if the form is completely filled out, I am getting the message "The PrintOut Action was cancelled". I just used a standard print event attached to a button. Is there a conflict with the beforeupdate event?


Private Sub cmdPrint_Click()
On Error GoTo Err_btnPrint_Click

DoCmd.PrintOut

Exit_btnPrint_Click:
Exit Sub

Err_btnPrint_Click:
MsgBox Err.Description
Resume Exit_btnPrint_Click


End Sub
 
If you click that button, it would run the before update event as losing focus from the form (which would happen if you are printing) automatically saves the record (which begins by using the BeforeUpdate event and because you are canceling the save, you are therefore canceling the print).
 

Users who are viewing this thread

Back
Top Bottom