Solved

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

Posted on 2013-05-23
6
260 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now