Results 1 to 8 of 8
  1. #1
    Alliana Gray is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    22

    Validation rule for a month field


    In the forms for my database, I require the user to input a month and year. Due to how the reports are structured, I need the format of the month and year to be very specific. I tried to set the format as 'mmmm yyyy' however even though it is visibly displayed as this, in the table, the values are still stored as sometimes 6/1/2011 or June 2011 and Jun 2011, which in Access' eyes are two different values.

    In the report, it will display June multiple times because of this format discrepancy. I am trying to create a validation rule to ensure the user will enter the month and year in the correct format, but I have no idea how to go about writing the expression.

  2. #2
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Can you provide them, instead of direct entry, a combobox with month names and one with years and then use a query to enter it into your database?

  3. #3
    Alliana Gray is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    22
    Yeah I can... thanks for the help! However, can you help me out with something else? I want a message to be displayed if there is already a record with the same Location and Month/Year saying "You are entering duplicate data"

  4. #4
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Will you be holding that in one field or two?

  5. #5
    Alliana Gray is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    22
    Two fields. One field is Location and one field is Month/Year.

  6. #6
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    You can use VBA for this. Using the event trigger of whatever you use to enter the data, you can do something like:

    Dim rs as DAO.Recordset

    Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblName WHERE Month Name = " & Me.MonthComboBox & " AND YearName = " & Me.YearComboBox

    rs.MoveLast

    'BOF stands for Beginning of File. EOF Stands for End of File. If it is both the beginning and end then there are no results, which means it is not a duplicate from the query 'above.
    If Not (rs.BOF AND rs.EOF)
    MsgBox "This Month and Year pairing has already been used. Please try again.", vbOkOnly
    Exit Sub
    Else
    'code to continue or just exit
    End If

    End Sub

  7. #7
    Alliana Gray is offline Novice
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    22
    The month and year are held in the same field. I am trying to display the message when the location and month/year pairing are the same as a previous record.

  8. #8
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Oh. Just replace the SQL in the OpenRecordset() function to pull the date and the location instead. The rest should be fine.

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

Similar Threads

  1. Validation Rule
    By ritzzy in forum Access
    Replies: 1
    Last Post: 04-13-2011, 01:33 PM
  2. Validation Rule
    By robomose in forum Access
    Replies: 1
    Last Post: 09-09-2009, 05:59 AM
  3. Validation Rule
    By smitstev in forum Access
    Replies: 5
    Last Post: 06-30-2009, 09:58 AM
  4. Referencing table data in field validation rule
    By toad848 in forum Database Design
    Replies: 3
    Last Post: 03-19-2009, 07:03 AM
  5. Validation rule for a text field
    By wasim_sono in forum Forms
    Replies: 4
    Last Post: 03-14-2006, 11:39 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