0

I have a MS Access 2010 form which has a combo box called cbxClass and another combo box called cbxCourse + another one called cbxInstitute. Right now the form works by selecting the institute and all the values in the cbxCourse change according to the institute. So does the cbxClass the values inside the combo box change according to the institute. Basically the cbxClass values change according to which class belong to the institute, now the problem is that, I want the user to be able to select the class from the cbxClass and once this event happens , the selected value of the cbxCourse change according to the course that the class is enrolled in.... A class can only be enrolled into one course, so there is only one choice, which I would like to automatically select once the user choose a class from the drop down menu.

I'm quite new to MS access, I usually work on something else and I don't really have much experience when configuring macros.

1
  • My answer is very generally, if you specify how the relation between classes and courses is stored, e.g. in a table or in code, etc. I can specify my answer as well so it suits your problem better.
    – Plasher
    Jul 30, 2015 at 9:14

1 Answer 1

0

Create the OnLostFocus event for your Combo Box cbxClass and add
cbxCourse.value = findCourse(cbxClass.value).

findCourse would be a function that returns the corresponding course to a given class, as only you know which course belongs to a given class. And it would be called everytime you'll leave the cbxClass Combo Box. You could use the OnChange event as well, then it would be executed everytime you'll change something in the cbxClass Combo Box.

If you click on the cbxClass control and open the control properties, there should be a tab called "Events" or something like this, I have it only in german. And in that tab there should be the events like OnLostFocus, OnChange, OnAfterUpdate, etc. double click on the one you want to use and you should see something like this:

Option Compare Database
Option Explicit

Private Sub cbxClass_AfterUpdate()

End Sub

Then just add the statement from above and add your findCourse function outside of the AfterUpdate event like this:

Option Compare Database
Option Explicit

Private Function findCourse(selectedClass as String) As String
    '// do your stuff here
    '// and return your course by using the following:
    findCourse = "theFoundCourse"
End function

Private Sub cbxClass_AfterUpdate()
    cbxCourse.value = findCourse(cbxClass.value)
End Sub
6
  • MM this looks good, but where should I implement the function ? and could this be done on after update event instead?
    – Monroy
    Jul 30, 2015 at 9:26
  • Sure you can do this on after update as well. Are you a bit familiar with the VBA editor? If you view the AfterUpdate event in the VBA editor you can just declare a new function outside of the after update event. I'll edit my answer to show it.
    – Plasher
    Jul 30, 2015 at 9:35
  • Yer Im working really new to this but I will get it quite quickly but I'm having some problems with the syntax :/ ... I'm used to Java and C#....
    – Monroy
    Jul 30, 2015 at 9:40
  • yeah vba can be a bit confusing at the beginning. ^^ so I'll edited my answer, just let me know if you need some more information.
    – Plasher
    Jul 30, 2015 at 9:42
  • Thanks you so much dude : ) ... And I reckon that there should be a sql statement in the "do your stuff comment " and do you don't use the return keyword in VBA?
    – Monroy
    Jul 30, 2015 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.