Required field on form not working

  • Thread starter Thread starter MW
  • Start date Start date

MW

Registered User.
Local time
Today, 12:35
Joined
Jan 8, 2002
Messages
14
I've got a form where users are required to enter data in certain fields (name, location, etc.). The "required" property on this field in the table is set to "yes" However, in the form, if the user does not put data in it, there is no message telling them that they must put something in there. It just goes to the next field and allows them to close the form with no messages whatsoever. And since there were required fields with no data in them, it did not save the record into the table. If the user enters data and then takes it out of the required field, it stops them from leaving the field blank and forces them to put something in it, but if they never put anything in it, the form allows them to continue on. How can I set the form to force them to enter data in the required fields and not allow them to continue unless they do? This works properly in the table, but not in the form. I thought the form would read this from the table properties and behave accordingly.

Thanks for any help you can give.
 
Try Form Before update

Put this on Form Properties before update

Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings (Not dataOK)
End Sub

Private Function dataOK() As Boolean
'Assume success
dataOK = True

'Now test emplyness
If Nz(Me![1st_rjct], "") <> "" And Nz(Me![1st_rjct_reason], "") = "" Then
MsgBox "You have not entered the first reject reason", vbOKOnly + vbExclamation
dataOK = False

If me.txtfield1="" then
MsgBox "you must populated field1", vbOKOnly + vbExclamation
dataok = false

elseIf me.txtfield2="" then
MsgBox "you must populated field2", vbOKOnly + vbExclamation
dataok = false

elseIf me.txtfield3="" then
MsgBox "you must populated field3", vbOKOnly + vbExclamation

dataok = false
End If

============================================

Good Luck
Dianna
 
Could you explain what this line does:

'Now test emplyness
If Nz(Me![1st_rjct], "") <> "" And Nz(Me![1st_rjct_reason], "") = "" Then
MsgBox "You have not entered the first reject reason", vbOKOnly + vbExclamation
dataOK = False

I've implemented all of the statements below this one to fit my needs. But, I'm unsure how to implent this section. What I'm doing is with this real estate management software I'm working on have it not save the enter new tenant form unless all the fields have data entered into it. I'm pretty new to Access and VB, so sorry if I sound dumb.

Thanks!
David Somers
 
dsomers said:
Could you explain what this line does:

'Now test emplyness
If Nz(Me![1st_rjct], "") <> "" And Nz(Me![1st_rjct_reason], "") = "" Then

It's quite messy.

The Nz() function tests a value to see whether it is Null. If it is Null then it replaces it with "" which would be better as vbNullstring.

So, it's saying:

If the field 1st_rjct is not null and the other field is null then do something.

Condense it like this:

Code:
If Not IsNull(Me.[1st_rjct]) And IsNull(Me.[1st_rjct_reason] Then
 
Re: Try Form Before update

If you encounter, in code, the phrase "" then it can be replace with the Access constant vbNullString. This is slightly faster when compiled.
 
So is this section required in order for the rest of the code to work? I guess a better question for my understaning is what is [1st_rjct] and [1st_rjct_reason]? I just want to test all the fields on my new tenants form to see if their empty or not and either allow them to save or not.

Thanks!
David Somers
 
Here is the code I'm using:
Private Sub Save_Click(Cancel As Integer)
On Error GoTo Err_Save_Click

DoCmd.SetWarnings (Not dataOK)
DoCmd.GoToRecord , , acNewRec

Exit_Save_Click:
Exit Sub

Err_Save_Click:
MsgBox Err.Description
Resume Exit_Save_Click

End Sub


Private Sub Close_Click()
On Error GoTo Err_Close_Click

DoCmd.SetWarnings (Not dataOK)
DoCmd.Close

Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings (Not dataOK)
End Sub


Private Function dataOK() As Boolean
'Assume success
dataOK = True

'Now test for emptiness
If Me.ContactFirstName = "" Then
MsgBox "You have not entered a First Name.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.ContactLastName = "" Then
MsgBox "You have not entered a Last Name.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Previous_Address = "" Then
MsgBox "You have not entered a Previous Address.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Text26 = "" Then
MsgBox "You have not entered a City.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Text28 = "" Then
MsgBox "You have not entered a State/Province.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Text30 = "" Then
MsgBox "You have not entered a ZIP Code.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.DOB = "" Then
MsgBox "You have not entered a Date of Birth.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.SSN = "" Then
MsgBox "You have not entered a Social Security Number.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.DL__ = "" Then
MsgBox "You have not entered a Drivers License Number.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Text42 = "" Then
MsgBox "You have not entered a Building Number.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Text44 = "" Then
MsgBox "You have not entered a Unit Number.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Lease_Start_Date = "" Then
MsgBox "You have not entered a Lease Start Date.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Lease_End_Date = "" Then
MsgBox "You have not entered a Lease End Date.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.Deposit = "" Then
MsgBox "You have not entered a Security Deposit amount.", vbOKOnly + vbExclamation
dataOK = False

ElseIf Me.MonthlyRent = "" Then
MsgBox "You have not entered a Monthly Rent amount.", vbOKOnly + vbExclamation
dataOK = False

dataOK = False
End If

End Function

Now, as you can see, I don't have the above mentioned section in the function. And when I click on my save or close buttons, I get this error: The Expression On Click you entered as the event property setting produced the following error: Procedure declaration does not match description of event or procedure having the same name.

Any ideas on what I'm doing wrong?

Thanks!
David Somers
 
Any ideas on why my code isn't working?

Thanks!
David Somers
 

Users who are viewing this thread

Back
Top Bottom