Results 1 to 5 of 5
  1. #1
    BLFOSTER is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2013
    Posts
    68

    If the field on the form is visible, a value is required, else if it is not visible........

    Hello!



    Can I make a field required only when the field is visible (using VBA)? I don't see a .required property.......

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    In the form text box, you can set
    VALIDATION RULE (and VALIDATION TEXT if they dont follow it)

    or yes, since you want it 'sometimes' you can put in a check when they hit save...

    Code:
    btnSave_click()
    if IsValidForm() then
       SAVErec()
    end if
    end sub
    
    
    public Function IsValidForm() as boolean
    dim vMsg
      select case true
           case txtName = "" and txtName.visible
               vMsg = "Client Name is missing"
           case isnull(cboState )
              vMsg = "State is missing"
       cboState.setfocus
      end select
      if vMsg <>"" then msgbox vmsg,vbCritical,"Required"
      IsValidForm =vMsg =""
    end sub

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You can use code in the Form_BeforeUpdate event to

    1. See if the TextBox is Visible
    2. See if it has been populated, if Visible
    3. Cancel the Update if #1 is True and #2 is False
    4. Warn the user
    5. Return Focus to the Control

    Where SometimesRequired is the name of the Textbox in question:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If (Me.SometimesRequired.Visible = True) And (Nz(Me.SometimesRequired, "") = "") Then
       Cancel = True
       MsgBox "The SometimesRequired Field Must be Populated When Visible!"
       SometimesRequired.SetFocus
     End If
    End Sub

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    BLFOSTER is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jun 2013
    Posts
    68
    Thanks! This worked....how do I mark as solved?

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Have no idea how to mark a thread as 'solved!' I've never actually posted a question, here, but expect that there is a tool, maybe only visible to the original poster, for doing so...maybe under the "Preview Post" button?

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 30
    Last Post: 07-21-2014, 02:46 PM
  2. Visible/Not Visible based on condition VBA
    By BLFOSTER in forum Programming
    Replies: 3
    Last Post: 07-15-2014, 01:29 PM
  3. Replies: 2
    Last Post: 04-04-2011, 02:18 PM
  4. Replies: 2
    Last Post: 01-06-2011, 04:38 AM
  5. Tab only visible when field = x
    By ecpike in forum Forms
    Replies: 7
    Last Post: 06-08-2009, 04:38 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