Link to home
Start Free TrialLog in
Avatar of gigifarrow
gigifarrow

asked on

Using List boxes to query a form will only show info for criteria not the rest of the fields. Error Name!? for other fields

This code allows a user to select criteria as many as they need In 3  List Boxes. Then that information is suppose to populate a Sub form.

The data that is selected shows the criteria that is selected but doesnt show the information for the rest of the fields in the subform. Where and how do I put the code to join the forms to work together and show all the data for the subform?

Below is the code the tables were linked so I had to change them.

 Dim sf As SubForm
    Dim MODKitStr As String, DIVStr As String, BdeStr As String
    Dim SQLStr As String
    Dim ListCounter As Integer
    Dim AnItem As Variant
     
    For Each AnItem In Me.lstMODKit.ItemsSelected
        If Not IsNull(AnItem) Then
            MODKitStr = MODKitStr & "'" & Me.lstMODKit.ItemData(AnItem) & "',"
        End If
    Next AnItem
    If Len(MODKitStr) = 0 Then
        Call noselection("A")
        Exit Sub
    Else
        MODKitStr = Left(MODKitStr, Len(MODKitStr) - 1)
    End If
     
    For Each AnItem In Me.lstDIV.ItemsSelected
        If Not IsNull(AnItem) Then
            DIVStr = DIVStr & "'" & Me.lstDIV.ItemData(AnItem) & "',"
        End If
    Next AnItem
    If Len(DIVStr) = 0 Then
        Call noselection("B")
        Exit Sub
    Else
        DIVStr = Left(DIVStr, Len(DIVStr) - 1)
    End If
       
    For Each AnItem In Me.lstBde.ItemsSelected
        If Not IsNull(AnItem) Then
            BdeStr = BdeStr & "'" & Me.lstBde.ItemData(AnItem) & "',"
        End If
    Next AnItem
    If Len(BdeStr) = 0 Then
        Call noselection("C")
        Exit Sub
    Else
        BdeStr = Left(BdeStr, Len(BdeStr) - 1)
    End If
    SQLStr = "SELECT tblMODHistory.Bde,tblMODHistory.DIV,tblMODHistory.[MOD Kit]   " & _
    "FROM qryMODHistory " & _
    "WHERE qryMODHistory.Bde IN(" & BdeStr & ") and qryMODHistory.DIV IN(" & DIVStr & ") and qryMODHistory.[MOD Kit] IN(" & MODKitStr & ")"
    Set sf = Me.[qryMODHistory_Crosstab subform]
    Debug.Print SQLStr
    sf.Controls.Parent.RecordSource = SQLStr
    sf.Requery

   End Sub
Private Sub noselection(LType As String)
    Dim msg As String
    Select Case LType
    Case Is = "A"
        msg = "No MOD Kits "
    Case Is = "B"
        msg = "No DIvision "
    Case Is = "C"
        msg = "No Brigade "
    Case Else
        msg = "Error : Do not know list box type :"
        End Select

    Me.[qryMODHistory_Crosstab subform].Controls.Parent.RecordSource = "SELECT qryMODHistory.Bde  ,qryMODHistory.DIV, qryMODHistory.[MOD Kit] FROM qryMODHistory WHERE qryMODHistory.[MOD Kit]  = '£$^'"
    Me.[qryMODHistory_Crosstab subform].Requery
    msg = msg & "have been selected" & vbCrLf & "Record was not updated"
    MsgBox msg
End Sub
 


Private Sub lstMODKit_AfterUpdate()
    Call DoubleMultiSelect_Click
End Sub
 
 
Private Sub lstDIV_AfterUpdate()
    Call DoubleMultiSelect_Click
End Sub

Private Sub lstBde_AfterUpdate()
    Call DoubleMultiSelect_Click
End Sub


User generated imageHelpwithListSearch.zip
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

test this,

note that --some of the columns in your query are not valid...
HelpwithListSearchRev.accdb
Avatar of gigifarrow
gigifarrow

ASKER

Thank you for your time and help. I tried that rev database and Im still getting #Name?.

Can you tell what do you mean by some of my columns are not valid? What is making them not valid?
Can you tell me what you changed on the example database please? Thanks in advance.
open the one that you uploaded and you will see the error when you open the form.
<Can you tell me what you changed on the example database please? Thanks in advance. >

open the form vba module,

look for the lines i commented,
the change is the line below/above the commented lines
I saw the changes you made and I Incorporated them in my database. However, I am still getting the error #NAME?

You see the form is a crosstab query. The columns are in a field called Model. So instead of showing these fields under model, in the query it is shown as a individual column.


How do I make the Crosstabform work with the selections?

User generated image
I dont understand why you changed it from Sub Form to form. It stills is showing the same error messages.
ASKER CERTIFIED SOLUTION
Avatar of gigifarrow
gigifarrow

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I figured out the answer to my own question.