A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Using Forms
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

checking for existing record



 
 
Thread Tools Display Modes
  #1  
Old May 31st, 2010, 04:01 AM posted to microsoft.public.access.forms
mbparks
external usenet poster
 
Posts: 22
Default checking for existing record

Hi Tom,
I'm still having trouble with this. In your reply you have " &
me.myCasenumberControlName". My control is a text box called Case Number.
I've entered it as "myCaseNumber", "my [Case Number]", Case_Number and [Case
Number]. None are working. I get a compile error message - Expected: End of
Statement.
Please help.

"Tom van Stiphout" wrote:

On Sat, 22 May 2010 22:32:01 -0700, mbparks
wrote:

I'm assuming you meant "move to the record with the correct
casenumber". This is accomplished with the bookmark technique:
with me.recordsetclone
.findfirst "[case number]=" & me.myCasenumberControlName
if not .nomatch then
me.bookmark = .bookmark
end if
end with

-Tom.
Microsoft Access MVP


In the current form, move to the record with the correct case number.

"Tom van Stiphout" wrote:

On Sat, 22 May 2010 18:23:01 -0700, mbparks
wrote:

I'm not sure I follow you. You write: "If it is blank I would like the
existing record to open". Open how? Do you mean "open another form
with the correct casenumber preselected", or do you mean "in the
current form, move to the record with the correct casenumber"? Or?

It is nearly impossible and not recommended to use ONE control both
for lookups as for data-entry.

-Tom.
Microsoft Access MVP


My table contains a field labled "Case Number" and another field labled "Date
Completed". I have created an input form and included code to check for
duplicate case numbers. A msgbox appears with a warning. I would like the
code to check for a duplicate case number and then check the "Date Completed"
field to see if it is blank. If it is blank I would like the existing
record to open. If the date completed field is not blank I would like to
continue entering data in the form to create a new record.
The code I am currently using is:

Private Sub Case_Number_BeforeUpdate(Cancel As Integer)
If DCount("*", "Copy of DIV 3 ICT Database", "[Case Number] = '" & Me![Case
Number] & "'") 0 Then
MsgBox "This item already exists in the table."
Cancel = True
Me.Undo
End If
End Sub

How can I incorporate the changes?
Any help is greatly appreciated.


  #2  
Old May 31st, 2010, 04:38 AM posted to microsoft.public.access.forms
Lord Kelvan
external usenet poster
 
Posts: 637
Default checking for existing record

This code should do what you want

Also as a note for future reference don’t use spaces in any table name
form name field in a table anything as it creates problems when
programming use camel case like this CopyOfDIV3ICTDatabase

You forgot to put [] around Copy of DIV 3 ICT Database because of the
spaces in your code

Private Sub Case_Number_BeforeUpdate(Cancel As Integer)
If isnull(Dlookup("[Case Number]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
DoCmd.GoToRecord , , acNewRec
else
If isnull(Dlookup("[Date Completed]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
Me.Filter = "id = 2"
Me.FilterOn = True
Me.Requery
else
Cancel = True
Me.Undo
End If
end if
End Sub

Hope this helps

Regards
Kelvan
  #3  
Old May 31st, 2010, 06:21 AM posted to microsoft.public.access.forms
mbparks
external usenet poster
 
Posts: 22
Default checking for existing record

I'm still having a problem. I copied your code but still having problems.
The code window opens and the first line of code is highlighted.
Please help.

"Lord Kelvan" wrote:

This code should do what you want

Also as a note for future reference don’t use spaces in any table name
form name field in a table anything as it creates problems when
programming use camel case like this CopyOfDIV3ICTDatabase

You forgot to put [] around Copy of DIV 3 ICT Database because of the
spaces in your code

Private Sub Case_Number_BeforeUpdate(Cancel As Integer)
If isnull(Dlookup("[Case Number]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
DoCmd.GoToRecord , , acNewRec
else
If isnull(Dlookup("[Date Completed]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
Me.Filter = "id = 2"
Me.FilterOn = True
Me.Requery
else
Cancel = True
Me.Undo
End If
end if
End Sub

Hope this helps

Regards
Kelvan
.

  #4  
Old May 31st, 2010, 01:46 PM posted to microsoft.public.access.forms
Daniel Pineault
external usenet poster
 
Posts: 658
Default checking for existing record

Assuming what Lord Kelvan gave as a sub is what you are looking for, he
simply forgot to add a closing ' at the end of the DLookUp criteria. Try

Private Sub Case_Number_BeforeUpdate(Cancel As Integer)
On Error GoTo Error_Handler

If IsNull(DLookup("[Case Number]", "[Copy of DIV 3 ICT Database]", _
"[Case Number] = '" & Me.[Case Number] & "'")) Then
DoCmd.GoToRecord , , acNewRec
Else
If IsNull(DLookup("[Date Completed]", "[Copy of DIV 3 ICT
Database]", _
"[Case Number] = '" & Me.[Case Number] & "'")) Then
Me.Filter = "id = 2"
Me.FilterOn = True
Me.Requery
Else
Cancel = True
Me.Undo
End If
End If

Error_Handler_Exit:
On Error Resume Next
Exit Sub

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: Case_Number_BeforeUpdate" & vbCrLf
& "Error Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Sub

Also, I strongly encourage you to use proper code indenting and most
importantly error handling.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.



"mbparks" wrote:

I'm still having a problem. I copied your code but still having problems.
The code window opens and the first line of code is highlighted.
Please help.

"Lord Kelvan" wrote:

This code should do what you want

Also as a note for future reference don’t use spaces in any table name
form name field in a table anything as it creates problems when
programming use camel case like this CopyOfDIV3ICTDatabase

You forgot to put [] around Copy of DIV 3 ICT Database because of the
spaces in your code

Private Sub Case_Number_BeforeUpdate(Cancel As Integer)
If isnull(Dlookup("[Case Number]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
DoCmd.GoToRecord , , acNewRec
else
If isnull(Dlookup("[Date Completed]","[Copy of DIV 3 ICT Database]",
"[Case Number] = '" & [Case
Number].value)) Then
Me.Filter = "id = 2"
Me.FilterOn = True
Me.Requery
else
Cancel = True
Me.Undo
End If
end if
End Sub

Hope this helps

Regards
Kelvan
.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:47 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.