Results 1 to 7 of 7
  1. #1
    Nadine67 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    55

    Validation condition where at least one of four fields meets the critera


    Hello and thank you for any attention this post may receive

    Windows XP
    MS Access 2007
    VBA experience is very limited

    So in my forms 'BeforeUpdate' property I want to validate whether a condition is met. The condition is whether 'any one of 4 fields contains something (text or number)'. I have written this code for validating a single condition and it alerts if the field is left blank. This code returns a warning message when the field is blank.

    If Me.[txtObjective] & "" = "" Then
    MsgBox "A 'Problem Objective' is required, please complete this field.", vbOKOnly
    Me.[txtObjective].SetFocus
    Cancel = True
    Exit Sub
    End If
    If Me.[txtApproxCost] & "" = "" Then
    MsgBox "'Approx Cost' is a required field, please complete this field.", vbOKOnly
    Me.[txtApproxCost].SetFocus
    Cancel = True
    Exit Sub
    End If
    If Me.[txtApproxDuration] & "" = "" Then
    MsgBox "'Approx Duration' is a required field, please complete this field.", vbOKOnly
    Me.[txtApproxDuration].SetFocus
    Cancel = True
    Exit Sub

    I am unsure how to write it in my code above. I thought I could write it like this

    If Me.[txtProduct1] And Me.[txtProductType] And Me.[txt549] And Me.[txtCurrentRisk] & "" = "" Then
    MsgBox "'This' is a required field, please complete this field.", vbOKOnly
    Me.[?????].SetFocus
    Cancel = True
    Exit Sub

    But I dont know how to write the SetFocus part. And I dont know if this is correct anyway.

    Thank you for any suggestions for my issue.



  2. #2
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,429
    try

    If nz([txtProduct1] ,"") & nz([txtProductType],"") & nz([txt549],"") & nz([txtCurrentRisk],"") = "" Then

  3. #3
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,429
    meant to say you can't set focus on multiple controls, only one

  4. #4
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    So you want the update to cancel if at least one of the txt boxes is blank or if all of them are blank?

  5. #5
    Nadine67 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    55
    Thank you Ajax. You have made my day and I am happy.


    So you want the update to cancel if at least one of the txt boxes is blank or if all of them are blank?
    Nick

    Yes i want to cancel if at least one of the boxes is blank is blank.
    More to the point, I would like to specify that only one of[txtTargetIncrease], [txtMthForecast], [Text549] can be filled at a time. If one is filled then the other two must be blank. but [txtCurrentRisk] can be filled at the same time as any one of the first three, or on its own. But I do not have a clue how to write that, so thats why I went with the simple option. it does not satisfy my database criteria but I don't know how to get around it.......unless someone is willing to help me.....wink wink.

    Nadine.

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,429
    OK change to

    If nz([txtProduct1] ,"") & nz([txtProductType],"") & nz([txt549],"") = "" OR nz([txtCurrentRisk],"") ="" Then

  7. #7
    Nadine67 is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Jul 2015
    Posts
    55
    Thank you very much Ajax.

    Cheers

    Nadine

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

Similar Threads

  1. Replies: 1
    Last Post: 07-18-2015, 08:35 AM
  2. Replies: 1
    Last Post: 05-13-2015, 02:17 AM
  3. Replies: 4
    Last Post: 01-12-2015, 12:07 AM
  4. Replies: 7
    Last Post: 12-12-2014, 11:58 AM
  5. Update 2 fields based on where condition.
    By Confused in forum Access
    Replies: 2
    Last Post: 11-19-2009, 05:21 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