SetWarnings Problem

  • Thread starter Thread starter dave h
  • Start date Start date
D

dave h

Hi,

Here is a test scenario that represents the basics of my problem:

Create "table1" with an autonumber "keyfield" and a text "txtField" -
keyfield is the primary key
Create "frmTable1" with the form design wizard using table1 and all the
defaults options
Set the properties for the form to data entry=yes; allow additions=yes;
allow deletions=no; allow edits=no
In code, put this

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim response As Integer
response = MsgBox("Do you want to add a Person?", vbYesNoCancel)
If response = vbNo Then
Cancel = True
DoCmd.SetWarnings False
End If
End Sub

Test by :
putting at least one character in the txtField and then close the form by
clicking the X in the upper right hand corner of the form.
Respone "No" to the msgbox question.

Notice that, after you respone "no" you get a message: "you can't save this
record at this time" and then more instructions and some buttons.
How can I suppress this warning as SetWarnings did not do the trick?

Thanks, Dave H
 
dave h said:
Hi,

Here is a test scenario that represents the basics of my problem:

Create "table1" with an autonumber "keyfield" and a text "txtField" -
keyfield is the primary key
Create "frmTable1" with the form design wizard using table1 and all
the defaults options
Set the properties for the form to data entry=yes; allow
additions=yes; allow deletions=no; allow edits=no
In code, put this

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim response As Integer
response = MsgBox("Do you want to add a Person?",
vbYesNoCancel) If response = vbNo Then
Cancel = True
DoCmd.SetWarnings False
End If
End Sub

Test by :
putting at least one character in the txtField and then close the
form by clicking the X in the upper right hand corner of the form.
Respone "No" to the msgbox question.

Notice that, after you respone "no" you get a message: "you can't
save this record at this time" and then more instructions and some
buttons.
How can I suppress this warning as SetWarnings did not do the trick?

Thanks, Dave H

Here's one solution:

'----- start of code #1 -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim response As Integer

response = _
MsgBox("Do you want to add a Person?", _
vbYesNoCancel)

If response = vbNo Then
Me.Undo
End If

End Sub

'----- end of code #1 -----

And here's another solution:

'----- start of code #2 -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim response As Integer

response = _
MsgBox("Do you want to add a Person?", _
vbYesNoCancel)

If response = vbNo Then
Cancel = True
End If

End Sub

Private Sub Form_Error(DataErr As Integer, Response As Integer)

If DataErr = 2169 Then
Response = acDataErrContinue
End If

End Sub
'----- end of code #2 -----
 
Hi Dirk,

Both of your suggestions work perfectly and I thank you very much as this
was really bugging me.

However, two followup questions - How did you know the error number was 2169
(it does not show on the original warning)? And, how can I find the
specific description for 2169 (I tried "Error(dataErr) and it just gives a
generic description)?

Thanks again for your quick help. Dave H.
 
dave h said:
Hi Dirk,

Both of your suggestions work perfectly and I thank you very much as
this was really bugging me.

However, two followup questions - How did you know the error number
was 2169 (it does not show on the original warning)?

I created a test form to work with. figured the form's Error event
would fire, so I created an event procedure for it, with the line

Debug.Print DataErr

2169 was printed in the Immediate window when I caused the error to
occur.
And, how can I
find the specific description for 2169 (I tried "Error(dataErr) and
it just gives a generic description)?

There's a function, AccessError(), that returns the message for a given
error number. Be aware that the message text returned by the function
often contains placeholder characters representing variable information
that is filled in at run time.
Thanks again for your quick help. Dave H.

You're welcome.
 
Back
Top