Data Validation

  • Thread starter RaajDave via AccessMonster.com
  • Start date
R

RaajDave via AccessMonster.com

Hey Guys!

Following problems need to be solved:

No.1:
I have designed a data-entry form in which I put a data validation on the
click event of SAVE button, which checks if the user leaves the field blank:
If Len(txtPartyName.Text) <= 0 Then
MsgBox “party name cannot be left blank.â€,
vbinformation+vbokonly, “DataValidationâ€
Txtpartyname.setfocus
Exit sub
End if
But this code throws an error “YOU CAN’T REFERENCE A PROPERTY OR METHOD FOR A
CONTROL UNLESS THE CONTROL HAS THE FOCUS.â€

No.2:
I have also put a command button named cmdClose for cancellation of the data
entry. The code in it’s click event is as follows:
DoCmd.Close , , acSaveNo
But if user has not entered anything in the field and clicks the cmdClose, it
throws an error “THE COMMAND OR ACTION UNDO IS NOT AVAILABLE NOW.â€
 
A

Allen Browne

Can we assume this form is bound to a table/query?

If so, use the BeforeUpdate event of the *form* (not control.) That's the
only way to ensure the code fires regardless of what triggers the writing of
the record to the table. Like this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtPartyName) Then
Cancel = True
Me.txtPartyName.SetFocus
MsgBox "...
'Me.Undo
End If
End Sub

#2: To undo the record and close the form:

Private cmdClose_Click()
If Me.Dirty Then Me.Undo
DoCmd.Close acForm, Me.Name
End Sub
 
R

RaajDave via AccessMonster.com

Hey Allen,

Thanks for your interest and prompt reply.

First of all, the form is surely bound to a table named MastParty. However,
none of them is working properly.

No.1: Though i have added the code you have supplied, the same error occurs.

No.2: A bit change is noticed. After putting your code into the click event
of the cmdClose button, blank text fields are displayed instead of displaying
its last record values.

Regards,
 
A

Allen Browne

Re #1: If you still receive the error message that says you cannot access
the TEXT property unless the control has the focus, then you must have
included the Text property in your expression. Omit it.

Re #2, I don't understand what you want to occur. Is this button intended
to:
a) cancel any changes, and
b) close the form?
That's what I thought you wanted. Hence, when it undoes a new record, it
will indeed show blank controls before closing the form. If that's not what
you want, please explain what you want the button to do.
 
R

RaajDave via AccessMonster.com

Hey, here is the description for my problem(s)

Prob.No.:1

I added the following code at the place you have advised:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtPartyName) Then
Cancel = True
Me.txtPartyName.SetFocus
MsgBox "Party Name cannot be left blank.", vbInformation + vbOKOnly,
"Data Validation"
End If
End Sub

But it still throws an error “YOU CAN’T REFERENCE A PROPERTY OR METHOD FOR A
CONTROL UNLESS THE CONTROL HAS THE FOCUS.â€

Prob.No.:2

Yes, It was my mistake that I wrote cmdClose button in my post(s). CmdClose
button closes the form and cmdCancel button cancels data-entry session and
displays the record without closing the form.

The form is used to display/ modify the records. When the user opens the
form, it only displays the records one-by-one (having record navigation
buttons]. But i have locked all the textboxes so that user cannot change the
data directly.
Any record modification / addition is to be made by selecting relevant
command buttons. For that purpose, i have four command buttons: cmdAdd,
cmdChange, cmdDelete and the last one is for closing the form cmdClose. I
have also put another 2 command buttons named cmdSave and cmdCancel which are
hidden at the beginning.

When user clicks cmdAdd or cmdChange button, cmdSave & cmdCancel buttons are
displayed and rest of the four command buttons are hidden [cmdAdd, cmdChange,
cmdDelete, cmdClose].

Now, if the user clicks cmdAdd for adding a new record but does not enter any
data and simply clicks cmdCancel button, that button should cancel data-entry
session [without closing the form] and first record should be displayed on
the screen as it does while opening the form. And for that, i added the code
[in cmdCancel_Click Event] you have given but not working. It gives the
error ““THE COMMAND OR ACTION UNDO IS NOT AVAILABLE NOW.â€
 
A

Allen Browne

Can I encourage you to learn to work with the way Access works first, and
then decide what command buttons you need to add?

For example, your cmdSave button is probably useless, as there are dozens of
ways that Access can save the record, e.g. by tabbing past the last control
(which moves to the next record), by pressing Shift+Enter, by choosing Save
on the menu/ribbon, by right-clicking on any control and filtering, by
applying a filter through the menus/toolbars, by changing the sort order, by
requerying the form, by closing the form, by closing Access, and so on.

Similarly, your undo button is meaningless if the record has already been
saved.

Similarly, if the user has been editing a record and tabs through past the
end to another one (or uses the navigation buttons), they will still be able
to edit, so your cmdChange button is not useful.

So, instead of adding buttons, consider learning about which events fire
when things happen: particularly Form_Current, Form_BeforeUpdate,
Form_BeforeInsert, Form_Delete, Form_BeforeDelConfirm, and
Form_AfterDelConfirm. By using the events that Access triggers for you when
things happen, you can perform your validation or whatever you need to do.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

RaajDave via AccessMonster.com said:
Hey, here is the description for my problem(s)

Prob.No.:1

I added the following code at the place you have advised:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.txtPartyName) Then
Cancel = True
Me.txtPartyName.SetFocus
MsgBox "Party Name cannot be left blank.", vbInformation + vbOKOnly,
"Data Validation"
End If
End Sub

But it still throws an error “YOU CAN’T REFERENCE A PROPERTY OR METHOD FOR
A
CONTROL UNLESS THE CONTROL HAS THE FOCUS.â€

Prob.No.:2

Yes, It was my mistake that I wrote cmdClose button in my post(s).
CmdClose
button closes the form and cmdCancel button cancels data-entry session and
displays the record without closing the form.

The form is used to display/ modify the records. When the user opens the
form, it only displays the records one-by-one (having record navigation
buttons]. But i have locked all the textboxes so that user cannot change
the
data directly.
Any record modification / addition is to be made by selecting relevant
command buttons. For that purpose, i have four command buttons: cmdAdd,
cmdChange, cmdDelete and the last one is for closing the form cmdClose. I
have also put another 2 command buttons named cmdSave and cmdCancel which
are
hidden at the beginning.

When user clicks cmdAdd or cmdChange button, cmdSave & cmdCancel buttons
are
displayed and rest of the four command buttons are hidden [cmdAdd,
cmdChange,
cmdDelete, cmdClose].

Now, if the user clicks cmdAdd for adding a new record but does not enter
any
data and simply clicks cmdCancel button, that button should cancel
data-entry
session [without closing the form] and first record should be displayed on
the screen as it does while opening the form. And for that, i added the
code
[in cmdCancel_Click Event] you have given but not working. It gives the
error ““THE COMMAND OR ACTION UNDO IS NOT AVAILABLE NOW.â€
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top