Nested loop to Iterate Values from/of 2 combo boxes

帅比萌擦擦* 提交于 2019-12-24 06:29:40

问题


I am making a College Admission Management System; there are 2 tables named Categories and Groups.

The category contains a field named Category which contains values like 'Army Serving' , Army Retired, Civilian , etc.. while Groups Table has a field named Group that contains values such as Pre-Engineering, Pre-Medical etc..

I am successfully able to preview all lists separately by selecting the category and group from combo boxes in a form, and I am also able to create a Merit list of students that have the required marks to get admitted.

Now, I want to automate it, like a 'Generate All' and 'Create All' Button that would generate all Merit Lists according to their category and Group.

Here: GroupVal is the name of Group ComboBox and QuotaVal is the name of Category ComboBox.

I tried this code but it returns different errors every single time I fix one error.

Private Sub CreateAllKey_Click()

Dim QuotaList As String
Dim GroupList As String
Set QuotaList = Tables!Categories!Category
Set GroupList = Tables!Groups!Group
For Each QuotaList In QuotaVal
    For Each GroupList In GroupVal
        DoCmd.OpenQuery "Merit List Generator", acViewNormal, acEdit
    Next
Next

MsgBox "All Lists Successfully Cleated", vbOKOnly, "Merit List Created!"

End Sub

And this following Code is returning only 1 value.. means, it runs on one value only and give out only 1 value;


    Dim QuotaList As Integer
    Dim GroupList As Integer

    For QuotaList = 0 To Me.QuotaVal.ListCount - 1
        If QuotaList = 0 Then
            Me.QuotaVal.Value = "AR"
        End If
        If QuotaList = 1 Then
            Me.QuotaVal.Value = "AS"
        End If
        If QuotaList = 2 Then
            Me.QuotaVal.Value = Null
        End If
        If QuotaList = 3 Then
            Me.QuotaVal.Value = "DP"
        End If
        If QuotaList = 4 Then
            Me.QuotaVal.Value = "FGEI"
        End If
        If QuotaList = 5 Then
            Me.QuotaVal.Value = "RFGEI"
        End If
        For GroupList = 0 To Me.GroupVal.ListCount - 1
            If GroupList = 0 Then
                Me.GroupVal.Value = "Gen-Sci-I"
            End If
            If GroupList = 1 Then
                Me.GroupVal.Value = "Gen-Sci-II"
            End If
            If GroupList = 2 Then
                Me.GroupVal.Value = "Gen-Sci-III"
            End If
            If GroupList = 3 Then
                Me.GroupVal.Value = "Humanities"
            End If
            If GroupList = 4 Then
                Me.GroupVal.Value = "Pre-Engg"
            End If
            If GroupList = 5 Then
                Me.GroupVal.Value = "Pre-Med"
            End If
            DoCmd.OpenQuery ("Merit List Creator")
        Next
    Next

End Sub

For example; I want something like this:

'Create lists of numbers and letters
Dim numbers() As Integer = {1, 4, 7}
Dim letters() As String = {"a", "b", "c"}

'Iterate through the list by using nested loops.
For Each number As Integer In numbers
    For Each letter As String In letters
        Debug.Write(number.ToString & letter & " ")
    Next
Next
Debug.WriteLine("")
'Output: 1a 1b 1c 4a 4b 4c 7a 7b 7c

Consider the values of Numbers as Categories Table here and the value of Alphabets as Groups Table and run the 'Merit List Generator" Query each time it selects values instead of printing '1a, 1b, 1c, 4a, 4b,•••'

Keep in mind, all query formulas are correct and working.. they are working when I generate a single list when I input my own category and group values.

The errors I get after fixing one or the other error are: 'Can't Assign Value' 'Object Required' 'Data Mismatch' Nothing worked! If anyone has written such code, please send me... This is more complex than a tangled earphone. XD


回答1:


What you describe sounds like a Cartesian query. When a query lacks a JOIN clause, every record of each table will associate with every record of other table. The result is every possible combination of pairs.

SELECT Category, Group FROM Categories, Groups;



来源:https://stackoverflow.com/questions/58237979/nested-loop-to-iterate-values-from-of-2-combo-boxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!