Solved

Detect if there is a specific choice selected in a combo box

Posted on 2013-05-23
6
285 Views
Last Modified: 2013-06-26
Hi.

How do I detect through vba that a specific selection has been made in a combobox in a subform? I need to send a Msgbox to the user... "You can't select this without selecting option 1 and 2 also..."

Should I use a query and then  apply it to the value "on dirty"?
0
Comment
Question by:EvertJor
  • 3
  • 2
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39193062
It depends on where the code is located relative to your combo boxes, and also when you want this message to be triggered.

<< You can't select this without selecting option 1 and 2 also..>>

Also are we trying to detect choices in one or two different combo boxes?

(Your question is a little ambiguous)

If you want to run this code when a user makes a selection in a combo box on the main form, use the Before update event of your main form combo:

Private Sub cboYourMainFormCombo_BeforeUpdate(Cancel as integer)
If Me.cboYourMainFormCombo = "some value" then
      if Not(Me.SubformName.Form.Combo1 = "Value1" And Me.SubformName.Form.Combo2 = "Value2" Then
              msgbox "You cannot make this selection without first selecting Value1 and Value2"
              cancel = true
     end if
End if
End Sub

Open in new window

0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 39193229
<(Your question is a little ambiguous)>
Yes, a bit unclear...
Here I will presume that an "Option" is an actual "Option Button" (Boolean value)
...and that the two options needing to be selected together, is always the case.

Then (Pending clarification), ...another approach might be to "Disable" the combobox in the first place.
...Instead of letting them go trough all the trouble of selecting a value, (Then thinking it will work) only to find out that it wont.

Private Sub Form_Current()
    Call ValidateOptions
End Sub

Private Sub opt1_AfterUpdate()
    Call ValidateOptions
End Sub

Private Sub opt2_Click()
    Call ValidateOptions
End Sub

Private Sub ValidateOptions()
    If Me.opt1 = True And Me.opt2 = True Then
        Me.State.Enabled = True
    Else
        Me.State.Enabled = False
    End If
End Sub


Very simple sample attached
Database40.mdb
0
 

Author Comment

by:EvertJor
ID: 39193567
Hi mbizup.

The control is in a subform and refers to a choice in another subform. Both on a main form (of course)...

Thanks so far. Regards.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 61

Accepted Solution

by:
mbizup earned 395 total points
ID: 39193592
For code in one subform, referring to a control in another subform:

If Me.Parent.OtherChildForm.Combo1 = "xyz" Then
      ' Etc...

Open in new window

0
 

Author Comment

by:EvertJor
ID: 39232429
boag2000:

The module you refer to in your code isn't included in your example file.
0
 

Author Closing Comment

by:EvertJor
ID: 39279140
Thanks a lot for your help, I need to work a bit with this to get the functionality I need.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question