Solved

DCount Not Working

Posted on 2010-08-26
10
312 Views
Last Modified: 2012-06-21
Hi Experts,
Can you please help me with the following. I don't know why the following code isn't working.

According to the sample data, a msgbox should appear but it doesn't if i were to repeatedly click on the cmdbutton.

Thanks
Ric

Private Sub Command9_Click()

Dim rs As dao.Recordset

Set rs = CurrentDb.OpenRecordset("tblClassMeeting")

    With rs

         .AddNew

         !ClassProductID = Me.ClassProductID

         !ClassDate = Me.ClassDate

         .Update

    End With

rs.Close

Call ClassMeetingUpdate_AfterUpdate

End Sub



Private Sub ClassMeetingUpdate_AfterUpdate()

If DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = " & Me.ClassDate) > 0 Then

        MsgBox "Duplicate Entry"

Cancel = True

      DoCmd.RunCommand acCmdUndo

      DoCmd.GoToRecord , , acNewRec

   End If

End Sub

Open in new window

ClassMeetingForm.jpg
ClassMeetingSampleData.jpg
0
Comment
Question by:RiCzN
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 250 total points
ID: 33533263
......and [ClassDate] =#" & format(Me.ClassDate, "yyyy-mm-dd") & "#")
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33533270
Probably need delimiters on the date:

DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = #" & Me.ClassDate)  & "#"
0
 
LVL 2

Expert Comment

by:bgrandjean
ID: 33533331
You need #s around your date to indicate it is a date in the where condition, try it like this:

If DCount("[ClassMeetingID]", "tblClassMeeting", "[ClassProductID]=" & Me.ClassProductID & " and [ClassDate] = #" & Me.ClassDate & "#") > 0 Then
0
 

Author Comment

by:RiCzN
ID: 33533413
Still have problem with the following line:

DoCmd.RunCommand acCmdUndo
0
 

Author Comment

by:RiCzN
ID: 33533539
When i use Me.Undo, the last record would Undo but using the DoCmd doesn't. Would it be a problem if i used Me.Undo instead?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 250 total points
ID: 33533549
There is nothing to UNDO.  You need to put the DCOUNT logic BEFORE you update the recordset.

Also, I recommend you reconsider the primary key for ClassMeeting table.  It should prevent the duplication you are concerned about.  Alternatively, you can add a no-duplicates index on ClassProductID + ClassDate.    
0
 
LVL 2

Expert Comment

by:bgrandjean
ID: 33533582
I think you are too late to do an Undo at this point.  You should do the check for an existing item before you do the add.
0
 

Author Comment

by:RiCzN
ID: 33533619
How do i set up no-duplicates for ClassProductID + ClassDate?
Is it just a matter of selecting Index: Yes (No duplicates) for the two fields?
IndexNoDuplicates.jpg
0
 

Author Closing Comment

by:RiCzN
ID: 33533842
Thanks
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33534025
Creating an index like THAT only allows you to have one column in the index. In other words, you will end up with a separate unique index on each column. From table design, you need to open the Indexes menu.  There, you can create an index with multiple columns.  
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now