cancel a new record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form with 4 fields that need to be completed. If any of then are
blank access produces an error message.

How can I simply about the addition of the form if this happens.

I tried using .cancelupdate in the on error of the form but it didn't like it.
 
Keith

Take a look at the form's BeforeUpdate event. Add code in there that
validates (check for blank), and cancel the update if any of them are blank.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
This may sound pretty lame but I figured it fits the question and I could use
the help:

I have extensive experience doing VB and other programming but the VBA is
not quite the same and so I need a little help here specially with Access
portion.

I have a button on a form that when they click it will close the form, I
want to know how I can have the form check to see that the last record on the
screen was completed or not.

If not then message that you will lose it , wanna go back and finish it or
continue
they say no, it goes back and nothign happens
they say yes, it closes and discards anything entered on the LAST line and
we are done

any help would be greatly appreciated.
 
Please re-read my response. The form has a BeforeUpdate event. This is
triggered when you try to close the form. Do you checking there, and cancel
the Update (Cancel = True) if the validation fails.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Jeff,

Thank you for the reply but I actually figured this out in the meantime but
I do have a similarly annoying problem that I was hoping you can help with:

I need to validate 3 fields to ensure they are not empty:

although the lostfocus is not the most popular method, it works in this
instance as the best option, but i am open to suggestions. I have it check to
make sure the text is not empty. if no text it shows msgbox and then supposed
to setfocus back to that field, but for some reason it gives the msg and then
moves to the next field. how can I get it to stop doing that which forces the
person to stay on that field until they fill it.....

I would be grateful for your help, thank you.
 
Take a look at the .SetFocus method.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Well Jeff, I was hoping for a bit more but thanks for replying. I am very
familiar with the SetFocus, so I don't know what you mean by take a look. I
am aware of all the exceptions and I use it all the time in regular vb
programming, just wondering why it won't work on the forms in access, that's
the only reason I asked.

Anyway, I have to go home in about 15 minutes so I guess I will have to work
 
Michael

I'm not sure what you mean when you say "it won't work on the forms in
Access". I use it all the time in code-behind-forms. Maybe we're not
talking about the same thing. Where/how do you try using it? Can you post
the code in which you try it?

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Well I am sure I am using it how it was intended or at least pretty close but
sure here is the code:

<----Code Begins------->

Function cValidate(vObject As TextBox) As Integer
With vObject
If .Text = "" Then
MsgBox "This Field is Required!", vbCritical, "Missing"
cValidate = 1
Me.Repaint
.SetFocus
Exit Function
End If
End With
cValidate = 0
End Function

Private Sub CUSTOMER_NAME_BeforeUpdate(Cancel As Integer)
Cancel = cValidate(CUSTOMER_NAME)
End Sub

<-----End Code Snippet---->

This is how the code stands and its only been changed to this to try and see
if it would fix the problem. The problem was that despite setting the
SetFocus, the cursor would jump to the next field and ignore that and move
on. I was told and read that its because you need to cancel the update event,
so I set it up this way but I don't like it, that's why I wanted something
cleaner like I am used to.

hope this helps you help me out, I would appreciate it.

thanks,
Michael
 
Michael

We probably just have different definitions of "cleaner". The code you
posted looks like it would cancel an update of a control if there was a
zero-length string in the control. How do you handle a Null?

I must have lost track of the objective -- didn't you want to force a user
to fill in a control before s/he could go on to another?

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Jeff, Thank you for getting back to me but I solved the problem and this is
how in case it helps anyone in the future:

<-----CODE SNIP--->
//reusable for checking anything really
Function IsNothing(v As Variant) As Integer
IsNothing = False
Select Case VarType(v)
Case vbEmpty
IsNothing = True
Case vbNull
IsNothing = True
Case vbString
If Len(v) = 0 Then
IsNothing = True
End If
Case Else
IsNothing = False
End Select
End Function

//actual validation for the boxes
Function cValidate(cBox As TextBox) As Integer
With cBox
If IsNothing(cBox) Then
cValidate = -1
MsgBox .Name & " is Required!", vbCritical, "Missing"
Else
cValidate = 0
End If
End With
End Function

//one of the many fields that need the validation
Private Sub CUSTOMER_NAME_Exit(Cancel As Integer)
Cancel = cValidate(CUSTOMER_NAME)
End Sub

<----End Snip---->

this is working perfectly and accomplishing what I need, thanks for sticking
with me on this and all your assistance.

Michael
 
Michael

Thank YOU for posting back your solution. Anyone searching the groups for
solutions to a similar issue will be able to consider your approach now.

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
Back
Top