Results 1 to 6 of 6
  1. #1
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145

    VBA code for Required Field If Combo Box is "Yes"

    Hi Forum,



    Does anyone have a simple VBA code for if the Combo Box Above was selected "Yes"; the form user must enter Details on Field Box (Make this Box Required)?

    Thanks,

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,811
    Very sketchy information.
    Your combo box name is "Above"?? And you have a text box named "Field Box"??
    And it's bound column contains the values Yes, or whatever (i.e. you're not showing Yes but the control is bound to some other column in its list)?
    It is what type of form - main or sub form? When do you want to check this combo choice - when user clicks a button? Moves off of the record? Selects from the combo?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by Micron View Post
    Very sketchy information.
    Your combo box name is "Above"?? And you have a text box named "Field Box"??
    And it's bound column contains the values Yes, or whatever (i.e. you're not showing Yes but the control is bound to some other column in its list)?
    It is what type of form - main or sub form? When do you want to check this combo choice - when user clicks a button? Moves off of the record? Selects from the combo?
    Sorry let me clarify.

    The is for mainform. The combo box (cbobox) is a "Yes","No" Value List. If "Yes" is selected then the field box (txtbox)below it is required to be entered.

  4. #4
    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
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.cbobox = "Yes" and Nz(Me.txtbox,"") = "" Then
       Msgbox "txtbox Must Be Filled In!"
       Cancel = True
       txtbox.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

  5. #5
    gwboolean is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    65
    Not exactly sure what you are trying to do, but I will take a guess. It is my impression that you want to have a message that the user must use the combobox before something else can occur. There are a couple of ways that I do this in VB (I code in Visual Basic, but from past experience VBA is the same, just smaller).
    I usually use a label with the property to .Visible = False. So here is the code I might use for the message:

    With LabelMessage
    .Visible = True
    .Text = "You must make a selection from the Combobox above."
    End With

    The only question becomes where and how do you want the message executed and where and how do you want the label placed. In this case the content of the message would suggest the location of the message label.
    I also might have the combobox enabled property set to False and then have some other event or routine that would enable the combobox and run the message at the same time. Then I would have another event routine disable the combobox and set the message label Visible property to False.
    By the way, you do not need to use the With, End With. Something like this:

    SomeEvent_()
    'triggers Combobox and message
    ComboBox.Enabled = True
    MessageLabel.Visible = True
    MessageLabel.Text = "Seriously, You really want to use this ComboBox?"
    'Whatever else goes into the event
    End Sub

    SomeOtherEvent()
    'Disable Combobox and lose message
    ComboBox.Enabled = False
    MessageLabel.Visible = False
    'Whatever else goes into the event
    End Sub

    So all you would need to do is determine what event, subroutine or function you would want to trigger the message to be visible and what event, subroutine or function you want to trigger the message to disappear. Enable/Disable the combobox only if you think it would be beneficial to do so. Hope that addresses what you are wanting to do.

  6. #6
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by Missinglinq View Post
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     If Me.cbobox = "Yes" and Nz(Me.txtbox,"") = "" Then
       Msgbox "txtbox Must Be Filled In!"
       Cancel = True
       txtbox.SetFocus
     End If 
    End Sub

    Linq ;0)>
    Thanks Missinglinq!

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

Similar Threads

  1. Replies: 19
    Last Post: 03-09-2017, 09:48 AM
  2. Can't test code - "Debug" - "Run to Cursor"
    By GraeagleBill in forum Programming
    Replies: 4
    Last Post: 05-15-2016, 05:16 PM
  3. Replies: 2
    Last Post: 12-23-2015, 09:32 PM
  4. Replies: 3
    Last Post: 02-06-2015, 03:22 PM
  5. Replies: 4
    Last Post: 07-12-2014, 02:02 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