Results 1 to 9 of 9
  1. #1
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79

    Access VBA - DCount error

    Hi

    hope this is the right place for this topic. I am creating a staff attendance system through a listbox and a textbox which automates todays current date.

    this is my dcount code

    If (DCount("[StaffID]", _
    "tblAttendance", _
    "[StaffID] = " & Me.lstStaffAttendance & " And " & _
    "[StaffFirstName] = " & Me.lstStaffAttendance & " AND " & _
    "[AbsentDate] = #" & Me.txtTodaysDate & "#" & " or " & _
    "[AttendDate] = #" & Me.txtTodaysDate & "#")) > 0 Then


    MsgBox "This Employee Already Exists"
    Cancel = True
    Me!lstStaffAttendance.Undo


    there is an error stating runtime error 3075, syntax error missing operator in query expression '[StaffID] = AND [StaffFirstName] = And [AbsentDate] = #01/02/2015# or [AttendDate] = #01/02/2015#. I know the error lies on staffID line because I removed other code lines and error started from there.

    could someone please guide me, ive been working on this for so long I don't have a clue anymore. I think due to this error, the dcount does not prevent the duplication. this section of code below enable to add the information from listbox to table incase if you need it.

    Set ctl = Me.lstStaffAttendance
    Const StaffFirstName As String = 1
    Const StaffSurname As String = 2
    For Each varItem In ctl.ItemsSelected
    rs.AddNew
    rs!StaffID = ctl.Column(StaffID, varItem)
    rs!StaffFirstName = ctl.Column(StaffFirstName, varItem)
    rs!StaffSurname = ctl.Column(StaffSurname, varItem)
    rs!WorkStarted = txtTimeIn
    rs!WorkFinished = txtTimeOut
    rs!AttendDate = txtTodaysDate
    rs.Update
    Next varItem
    MsgBox "Done!"


    thank you I really appreciate it

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Text type fields need apostrophe delimiters for parameters and date type need # delimiters.

    You are missing the apostrophes for the StaffFirstName criteria.

    Why are you duplicating staff names into the attendance table?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79
    Thanks for your help

    sorry am confused as am a novice still learning access.

    I got first name and surname into attendance table because their names are in listbox so that whoever marks the attendance can see their names in the attendance table.
    I have put the # on the date and I put the 'after the " in the firstname I still get the error

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    "[StaffFirstName] = '" & Me.lstStaffAttendance & "' AND " & _

    The # characters were already correct.

    Should not duplicate names into attendance table.

    No users should be looking directly at tables or queries, only forms and reports. You control what they see on forms and reports.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79
    thank you

    I followed your suggestions and removed the duplicates and I still get the same error, which occurs for the staffID

    thank you

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    The error is still on the DCount? Is StaffID a number type field?

    Also, need to be careful about mixing AND and OR operators. Should have () around the date OR phrase, otherwise the conditional logic won't be what you expect.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79
    yes the error is still on dcount the staff id is a number field in tblattendance and autonumber in staffdetails table. thank you

    I don't understand about () I tried putting it like this

    If (DCount("[StaffID]", _
    "tblAttendance", _
    "([StaffID] = " & Me.lstStaffAttendance & ") And " & _
    "([AbsentDate] = #" & Me.txtTodaysDate & "#")) > 0 Then
    MsgBox "This Employee Already Exists"
    Cancel = True
    Me!lstStaffAttendance.Undo

    now the error states extra ) in query expression '([StaaffID] = ) AND ([AbsentDate] = #01/02/2015#'.

    just want to say thank you for helping me, I really appreciate it.

  8. #8
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79
    I removed the extra () and the old syntax errors comes again back to staffID.

  9. #9
    princess12 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    79
    thank you for your help
    I figured out the error, you were right I should be careful in using And and OR operators, that was the issue causing the error. I did a working system and thought I lost it found it now and compared it. Thanks a lot!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. DCount Error
    By tareyj8569 in forum Access
    Replies: 2
    Last Post: 10-14-2014, 02:27 AM
  2. DCount error
    By azhar2006 in forum Forms
    Replies: 5
    Last Post: 09-13-2014, 03:58 AM
  3. DCount Error
    By drewetzel in forum Access
    Replies: 5
    Last Post: 02-13-2014, 06:57 AM
  4. Dcount Error
    By waqas in forum Queries
    Replies: 3
    Last Post: 07-28-2012, 08:24 PM
  5. DCount on a Query giving #Name? error
    By Huddle in forum Access
    Replies: 9
    Last Post: 06-20-2012, 11:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums