Results 1 to 4 of 4
  1. #1
    llmdb is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2023
    Posts
    3

    Forbid null if condition


    Hello,

    I am new to access, and I didn't find the answer to my question online, I hope someone can help
    I would like to forbid a null entry in a field, only if the answer to the previous question is "yes". For instance : question 1 : "do you have cats" (yes/no) ; question 2 "how many cats do you have". If answer to question 1 is "yes", then the user must write a number in the field of question 2, but if the answer to question 1 is "no", then the field for question 2 may be null. I am guessing this could be easily done in code but I am not familiar enough with it to invent something
    Thank you for your help!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    You could do this but why even have the Yes/No control? A zero in the "how many cats do you have" control tells you that they do not have cats.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Join Date
    Apr 2017
    Posts
    1,679
    You say, "...user must write a number in the field...". I'm somewhat confused! Is user entering data into table directly (tables have fields)? Or is user entering data into form control, which is linked to table field. Having users working directly with tables is a really bad idea! As best, you simply hide all tables from users, and allow for users only access through forms!

    When you are speaking about entering data into form controls, then:
    To ensure after answering "yes" to 1st question the user must answer 2nd question too, either
    - At start of the entry, have only the control for 1st answer enabled for whole row. When 1st question is answered, the AfterUpdate event for this controls checks the answer. When the answer was "yes", the control for 2nd answer is enabled, and the user can enter the number of cats. When this number is >0, the control for next answer (3rd control, when there was more than 2 questions) is enabled by AfterUpdate event for 2nd control, etc. When 0 was entered, or nothing was entered, either next controls must remain disabled, and BeforeUpdate event of form must prevent any tries to save the record (displaying some error message). When on 1st control "no" was selected/entered, the AfterUpdate event of 1st control will enable 3rd control in case the number of controls was >2;
    - Same as previous, but for the 2nd you use a combo box with list of positive numbers. User can enter only numbers presented in this list;
    - All controls are enabled, and user can fill them with anything, but BeforeUpdate event is used, to check all entries for whole record, and prevents the saving (with proper message), whenever the entry rules for any of controls are wronged.

    When enabling/disabling of controls is used, OnCurrent event of form is needed, to check which controls must be enabled/disabled, whenever another record is activated in form.

    And as last - why is there any need for 1st question? Ask for number of cats - when empty, the user don't have cats, when not empty, the user has cats!

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I agree with Bob, but I assume you want to guarantee that they don't just skip over the question.

    Here's an example to give you some ideas. I didn't fully test the logic.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 4
    Last Post: 03-11-2017, 09:48 PM
  2. Replies: 2
    Last Post: 09-24-2016, 11:29 PM
  3. Forbid an ID
    By sharkantipav in forum Access
    Replies: 3
    Last Post: 03-14-2014, 02:57 PM
  4. Replies: 6
    Last Post: 02-12-2014, 09:46 AM
  5. Replies: 17
    Last Post: 01-26-2014, 06:53 AM

Tags for this Thread

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