Solved

DCount Not Working

Posted on 2010-08-26
10
315 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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
 
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

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

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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 …

831 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