Solved

After update error

Posted on 2011-03-01
4
203 Views
Last Modified: 2012-08-14
I need help raising an error event after i do a dlookup on a field to check if the value is already in the table.  i ca't raise event and force it to go to form error.
0
Comment
Question by:Shen
  • 2
4 Comments
 
LVL 75
ID: 35007544
Not quite sure what you are after, but I suppose you could define you own Error constant then use the Err.Raise method of the Err object - see VBA Help for details.  You can't control what is generated by the Form Error event per se ... and not specific error is generated by DLookup().

mx
0
 
LVL 77

Expert Comment

by:peter57r
ID: 35007775
It sounds like you have some error handling in place so it's difficult to know what a relevant response is but you can do stuff like this..

Private Sub Form_Error(DataErr As Integer, Response As Integer)    ' extracted from help
    Const conDuplicateKey = 3022
    Dim strMsg As String
 
    If DataErr = conDuplicateKey Then
        Response = acDataErrContinue
        strMsg = "Each employee record must have a unique " _
            & "employee ID number. Please recheck your data."
        MsgBox strMsg
    End If
End Sub


Private Sub ID_BeforeUpdate(Cancel As Integer)  

If IsNull(DLookup("ID", "Employeesb", "ID= " & Me.  ID)) Then
'do nothing, new number ok
Else
' call the Form error handler
Form_Error 3022, 0
End If

End Sub
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 35008330
Perhaps I am missing something, ...or not understanding something...
But you don't really have to "Raise" and error to see if a value exists.

Just use Pete's code like so:
If IsNull(DLookup("ID", "Employees", "ID= " & Me.ID)) Then
'do nothing, new number ok
Else
msgbox "The number exists already."
exit sub
End If

You can set a Public Variable in the code as well, if you need to "keep" a flag for the error...

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35018301
...Thanks, but actually you should split the points with Pete.

I just "ganked" his code and used it in a slightly different manner.

Please click the Request Attention link and ask that the points be split instead.

;-)

Jeff
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA Access 2016 syntax 6 43
Archiving Access table older than 6 months 9 39
Access 2003 query lost it's only join 7 27
Field naming convntions - MS Access 7 28
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question