Results 1 to 15 of 15
  1. #1
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183

    Field rules/validation rules


    I have a table which consists of field names like ID, Unit, DATE, Time, Site

    Time means either AM, PM, LUNCH.

    Each date (eg. 2nd March, 2012) could have 3 different TIME.
    Each date and time is stored in different rows/as separate record

    How do I set the rules to my query or my table that 2nd march of TIME - PM cannot be a repeat and so on? I know how to set rules for each field in the table itself but my problem is 2nd March could be repeated 3 times in that column and Time could be repeated too and if 2nd March, 2012 of TIME - PM already exits, Access should warn the user...

    Is there a way to do that?

    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Options:

    1. set Date, Time, Site, Unit as compound primary key.

    2. use VBA code in some event of data entry form to search table for the inputs
    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
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    Options:

    1. set Date, Time, Site, Unit as compound primary key.

    2. use VBA code in some event of data entry form to search table for the inputs
    Thanks, June!

    How do I set to compound primary key? :S

    and for if I use VBA code.. what should it be?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    1. Table in design view, select the fields simultaneously, set as primary key

    2. Simplest code might be a DLookup function. The real trick is figuring out what even to put code in. Maybe a 'Save Record' button click. How restrictive is the data? Can there be more than one unit per date per Site?

    If Not IsNull(DLookup("ID", "tablename", "Unit='" & Me.Unit & "', AND [Date]=#" & Me.Date & "# AND [Time]='" & Me.Time & "' AND Site='" & Me.Site & "'")) Then
    MsgBox "Record for this date, time, unit, site already exists."
    Else
    'code to save record, quit form
    End If

    Review this http://office.microsoft.com/en-us/ac...010341717.aspx
    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
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    1. Table in design view, select the fields simultaneously, set as primary key

    2. Simplest code might be a DLookup function. The real trick is figuring out what even to put code in. Maybe a 'Save Record' button click. How restrictive is the data? Can there be more than one unit per date per Site?

    If Not IsNull(DLookup("ID", "tablename", "Unit='" & Me.Unit & "', AND [Date]=#" & Me.Date & "# AND [Time]='" & Me.Time & "' AND Site='" & Me.Site & "'")) Then
    MsgBox "Record for this date, time, unit, site already exists."
    Else
    'code to save record, quit form
    End If

    Review this http://office.microsoft.com/en-us/ac...010341717.aspx
    Thanks June.. . The composite primary key did not help. I was still able to add 2 of the same date and time ..

    I am going to try the second option now. .thanks

  6. #6
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    1. Table in design view, select the fields simultaneously, set as primary key

    2. Simplest code might be a DLookup function. The real trick is figuring out what even to put code in. Maybe a 'Save Record' button click. How restrictive is the data? Can there be more than one unit per date per Site?

    If Not IsNull(DLookup("ID", "tablename", "Unit='" & Me.Unit & "', AND [Date]=#" & Me.Date & "# AND [Time]='" & Me.Time & "' AND Site='" & Me.Site & "'")) Then
    MsgBox "Record for this date, time, unit, site already exists."
    Else
    'code to save record, quit form
    End If

    Review this http://office.microsoft.com/en-us/ac...010341717.aspx

    June,
    I just tried using that code and I am getting error. Please see the attachment.

    Also, I am wondering if I can use the DLookup for only Date and Time only? and I can put the code in "AFTER UPDATE" Event? do you think that will work? Click image for larger version. 

Name:	1.png 
Views:	18 
Size:	3.9 KB 
ID:	6468

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Yeah, sorry for typo. Remove the comma in front of AND.

    Use whatever criteria is appropriate. If you use AfterUpdate, must be in events for both Date and Time fields.
    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.

  8. #8
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    Yeah, sorry for typo. Remove the comma in front of AND.

    Use whatever criteria is appropriate. If you use AfterUpdate, must be in events for both Date and Time fields.

    This is what I put in afterupdate.... somewhere is wrong I think.. I thought of removing SITE and Unit .. . but its not working ... Can you please tell me where I went wrong?

    If Not IsNull(DLookup("ID", "table", "Dateaudit"=#" & Me.Dateaudit & "' AND [time]=#" & Me.time & "# )) Then

    MsgBox "Record for this date, time, unit, site already exists."

    Else
    'code to save record, quit form
    End If

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Errors shown red:

    If Not IsNull(DLookup("ID", "table", "Dateaudit"=#" & Me.Dateaudit & "' AND [time]=#" & Me.time & "#)) Then

    Correct:
    If Not IsNull(DLookup("ID", "table", "Dateaudit=#" & Me.Dateaudit & "# AND [time]='" & Me.time & "'")) Then


    Your table is named 'table'? And 'time' is actual name of field?
    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.

  10. #10
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    Errors shown red:

    If Not IsNull(DLookup("ID", "table", "Dateaudit"=#" & Me.Dateaudit & "' AND [time]=#" & Me.time & "#)) Then

    Correct:
    If Not IsNull(DLookup("ID", "table", "Dateaudit=#" & Me.Dateaudit & "# AND [time]='" & Me.time & "'")) Then


    Your table is named 'table'? And 'time' is actual name of field?
    Yes.. the table name is table and the actual name of the field is time

    I just tried using
    If Not IsNull(DLookup("ID", "table", "Dateaudit=#" & Me.Dateaudit & "# AND [time]='" & Me.time & "'")) Then
    it is still giving me error and says Runtime Error 3464 - Data type mismatch in criteria expression.


  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Dateaudit is a date/time datatype and time is a text datatype? Text uses apostrophe delimiter, date uses # and number uses nothing.

    Could there be other Unit and Site entries for this date and time?
    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.

  12. #12
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    Quote Originally Posted by June7 View Post
    Dateaudit is a date/time datatype and time is a text datatype? Text uses apostrophe delimiter, date uses # and number uses nothing.

    Could there be other Unit and Site entries for this date and time?
    Oh! My time is stored as Number. Like AM = 0, PM means 1, LUNCH means 2

    When you say - number uses nothing. Does that mean the code will be --
    If Not IsNull(DLookup("ID", "table", "Dateaudit=#" & Me.Dateaudit & "# AND [time]=" & Me.time & ")) Then

  13. #13
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183
    and the just tried the above code and it did not work either.
    I also tried [time] = & Me.time & ))
    No luck on that one too...

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Remove the last & " marks.

    Quotes must be in pairs.
    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.

  15. #15
    sk88 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    183

    Post

    Quote Originally Posted by June7 View Post
    Remove the last & " marks.

    Quotes must be in pairs.
    YAY! it works!! Thanks, June!!

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

Similar Threads

  1. Multiple Validation Rules
    By Theremin_Ohio in forum Access
    Replies: 1
    Last Post: 12-16-2011, 01:21 PM
  2. troubles with validation rules
    By focosi in forum Access
    Replies: 4
    Last Post: 08-02-2011, 10:46 AM
  3. Question about Validation Rules
    By WilsonsW in forum Access
    Replies: 1
    Last Post: 04-18-2011, 05:06 PM
  4. Validation Rules
    By esglover in forum Database Design
    Replies: 1
    Last Post: 07-23-2010, 08:02 PM
  5. Validation rules question
    By cps6568 in forum Access
    Replies: 3
    Last Post: 01-12-2010, 02:01 PM

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