Custom Error message 3022 (1 Viewer)

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
Hi,
First of all please forgive me if I seem slow when it come to access as I have only been using it for a few weeks.
I am building a database to record appointments for a hairdressing salon, with 4 entities:
Customers, Stylists, Treatments, Appointments, Time.
I have set it up that when a double booking occurs the standard Access error message is changed to a custom error message.
This works fine when using the navigation buttons but when I try to add a double booking with the add new record button on the form it does not display the custom error message but displays the original built in error message.
Can anyone point me in the right direction.

James
 

Cronk

Registered User.
Local time
Today, 20:40
Joined
Jul 4, 2013
Messages
2,772
How are you trying to trigger the custom message? It's a bit hard to read your code from where I am.
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
As Cronk says, we can't really see what you have done.
Here is a link to a thread where the default error message for err.number 3022 has been replaced with a custom message.

Good luck.
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
Option Compare Database
Private Sub Form_Error(DataErr As Integer, Response As Integer)

If DataErr = 3022 Then
MsgBox ("This Appointment Slot Has Already Been Taken. Please Choose An Alternative Appointment Time, Date Or Choose An Alternative Stylist.")
Response = 0
End If

End Sub
'------------------------------------------------------------
' Add_Record_Click
'
'------------------------------------------------------------
Private Sub Add_Record_Click()
On Error GoTo Add_Record_Click_Err

On Error Resume Next
DoCmd.CancelEvent
If (MacroError <> 0) Then
Beep
MsgBox MacroError.Description, vbOKOnly, ""
End If


Add_Record_Click_Exit:
Exit Sub

Add_Record_Click_Err:
If DataErr = 3022 Then
MsgBox ("This Appointment Slot Has Already Been Taken. Please Choose An Alternative Appointment Time, Date Or Choose An Alternative Stylist.")
Response = 0
End If


End Sub


This is the code I am using
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
See the Error Handling Tips at the bottom of my signature.
I don't use macros. Every procedure (sub or function) gets error handling code.

The general layout is along this line:

Code:
Private Sub Command6_Click()
'
' Dim statements here
'
  On Error GoTo ErrHandler
'
' [COLOR="SeaGreen"]Your processing code here[/COLOR]
'

Err_Command6_Click:
110   If Err.Number = 3022 Then
120       MsgBox "That combination already exists, try again"
130       Resume Exit_Command6_Click
140   Else
150       MsgBox Err.Num & " " & Err.Description
160       Resume Exit_Command6_Click
170   End If
End Sub

Also look at the debugging tips to see techniques for reviewing your code to see what the code is actually doing. Step debug; debug.print; breakpoints.....

Good luck.
 

sneuberg

AWF VIP
Local time
Today, 03:40
Joined
Oct 17, 2014
Messages
3,506
The posted code looks a bit strange. Why is the event called Add_Record_Click but has DoCmd.CancelEvent for code?
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
I'm really sorry guys.
I am really new to this and its really got my head melted.

I have an appointment when i try to add a duplicate record it id great.
My custom error message for err 3022 works with the navigation buttons at the bottom.
But when I try to add a custom button for add record the old message err 3022 comes back.
I know this is a simple thing for yous as you probably work with vb all the time.
the code i have is this:

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


If DataErr = 3022 Then
MsgBox ("This appointment has already been taken, Please choose another.")
Response = 0
End If

End Sub


What do I do to enable this message to work with simple buttons.

Thanks in advance

James
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
James,

See the code layout I posted in post #5.
Also investigate the links in my signature --great reference for debugging and error handling tips.
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
I've added a button to add a new record and tried to add error handling.
but the code isn't working.
here is the new code.

Private Sub Command16_Click()

DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
MsgBox "This appointment has already been taken, Please choose another."
Resume Exit_Command16_Click
Else
MsgBox Err.Num & " " & Err.Description
Resume Exit_Command16_Click
End If

End Sub

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


If DataErr = 3022 Then
MsgBox ("This appointment has already been taken, Please choose another.")
Response = 0
End If

End Sub
 

sneuberg

AWF VIP
Local time
Today, 03:40
Joined
Oct 17, 2014
Messages
3,506
Try adding the On Error Goto as shown below

Code:
Private Sub Command16_Click()
[COLOR="Red"]
On Error GoTo Err_Command16_Click
[/COLOR]
DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
MsgBox "This appointment has already been taken, Please choose another."
Resume Exit_Command16_Click
Else
MsgBox Err.Num & " " & Err.Description
Resume Exit_Command16_Click
End If

End Sub
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
Try this:
Code:
Private Sub Command16_Click()

On Error goto Err_Command16_Click

DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
	MsgBox "This appointment has already been taken, Please choose another."
	Resume Exit_Command16_Click
Else
	MsgBox Err.Num & " " & Err.Description
	Resume Exit_Command16_Click
End If
Exit_Command16_Click:
End Sub

Let us know what happens.

Oops:sneuberg has posted. He shows the On Error, but I don't see an
Exit_Command16_Click label
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
Thanks for the quick reply.

I have added the code as you you suggested now the button won't add a record.

Heres my code now

Private Sub Command16_Click()

On Error GoTo Err_Command16_Click

DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
MsgBox "This appointment has already been taken, Please choose another."
Resume Exit_Command16_Click
Else
MsgBox Err.Num & " " & Err.Description
Resume Exit_Command16_Click
End If

End Sub

I really need this sorted for monday morning.
This is my first database so I am really a true beginner.
Any help at all is appreciated..
James
 

sneuberg

AWF VIP
Local time
Today, 03:40
Joined
Oct 17, 2014
Messages
3,506
Thanks for the quick reply.

I have added the code as you you suggested now the button won't add a record.

Heres my code now

Private Sub Command16_Click()

On Error GoTo Err_Command16_Click

DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
MsgBox "This appointment has already been taken, Please choose another."
Resume Exit_Command16_Click
Else
MsgBox Err.Num & " " & Err.Description
Resume Exit_Command16_Click
End If

End Sub

I really need this sorted for monday morning.
This is my first database so I am really a true beginner.
Any help at all is appreciated..
James

Sorry my bad, Try Jdraw's code.
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
James,

You do not have a label called Exit_Command16_Click. I'm surprised it even executed.

You have told Access to Resume Exit_Command16_Click, but there is no such place in your code. You need the label.

If you need to get it working you may have to post the db so others can see and test to resolve the issue(s).
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
Thanks
I really appreciate your patience.
But I dont understand.
Where di I put this code
I really dont know what I'm doing.
If you could post the entire code your suggesting this would help me .
I'm sorry for the hastle.

James
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
heres a copy of my db
 

Attachments

  • KlassicKutslatest020220161.accdb
    1.6 MB · Views: 80

jdraw

Super Moderator
Staff member
Local time
Today, 06:40
Joined
Jan 23, 2006
Messages
15,379
I posted the code.
Code:
Private Sub Command16_Click()
[COLOR="Red"][B]On Error goto Err_Command16_Click
[/B][/COLOR]
DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
	MsgBox "This appointment has already been taken, Please choose another."
	Resume Exit_Command16_Click
Else
	MsgBox Err.Num & " " & Err.Description
	Resume Exit_Command16_Click
End If
[COLOR="Red"][B]Exit_Command16_Click:[/B][/COLOR]
End Sub
 

jamesgtierney

Registered User.
Local time
Today, 11:40
Joined
Jan 28, 2016
Messages
24
Thanks jdraw.
But I really must be doing something wrong.

the button still not working.
Its really a simple database and i dont really code except for this bit in double booking.
Heres the code I have cut and pasted from your reply


Option Compare Database

Private Sub Command16_Click()

On Error GoTo Err_Command16_Click

DoCmd.GoToRecord , , acNewRec

Err_Command16_Click:
If Err.Number = 3022 Then
MsgBox "This appointment has already been taken, Please choose another."
Resume Exit_Command16_Click
Else
MsgBox Err.Num & " " & Err.Description
Resume Exit_Command16_Click
End If
Exit_Command16_Click:
End Sub

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


If DataErr = 3022 Then
MsgBox ("This appointment has already been taken, Please choose another.")
Response = 0
End If

End Sub.
I have also attached the database a couple of posts ago.
I've never used vb before. the last time i used code i was programming in basic.

Any help is appreciated.
I have been stuck on this for nearly 2 weeks.

James
 

Users who are viewing this thread

Top Bottom