Build the 'where' sql statement following search form selection (1 Viewer)

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
Still not working, should I change the list box to multiple select?
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:53
Joined
Jan 23, 2006
Messages
15,394
Can you post a copy of the database (zip format) with enough records to highlight the issue?
 

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
What happens (or doesn't happen)?

Remember we can't see your screen! Please describe the steps you take and what you observe.


Probably not.
I have attached the db with some sample data.
Contact list will autoload, just manually open frmSearch, and give it a try to see.
 

Attachments

  • Contacts V3.zip
    247 KB · Views: 93

cheekybuddha

AWF VIP
Local time
Today, 23:53
Joined
Jul 21, 2014
Messages
2,321
My version of Access is too old to open your forms, so I'll leave it to @jdraw and others to see if they can spot what's wrong
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:53
Joined
May 21, 2018
Messages
8,605
There is a lot of code here you may want to leverage to greatly simplify what you are trying to do.
 

cheekybuddha

AWF VIP
Local time
Today, 23:53
Joined
Jul 21, 2014
Messages
2,321
Building the SQL is no longer the problem!

Just removing the filter from the calling form doesn't seem to work.
 

KitaYama

Well-known member
Local time
Tomorrow, 07:53
Joined
Jan 6, 2022
Messages
1,575
test this.

Code:
    Dim SelectedID As Integer
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
  
    With lbSearchResults
        If .ItemsSelected.Count > 0 Then
            For index = 0 To .ListCount - 1
                If .Selected(index) Then
                    SelectedID = .Column(0, index)
                End If
            Next
            Forms![Contact List].Filter = "ID = " & SelectedID
            Forms![Contact List].FilterOn = True
        Else
            Forms![Contact List].FilterOn = False
        End If
    End With
 

Attachments

  • Contacts V3.accdb
    2.1 MB · Views: 63

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
test this.

Code:
    Dim SelectedID As Integer
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub

    With lbSearchResults
        If .ItemsSelected.Count > 0 Then
            For index = 0 To .ListCount - 1
                If .Selected(index) Then
                    SelectedID = .Column(0, index)
                End If
            Next
            Forms![Contact List].Filter = "ID = " & SelectedID
            Forms![Contact List].FilterOn = True
        Else
            Forms![Contact List].FilterOn = False
        End If
    End With
Still not filtering.
Am guessing because no selection is being made.
 

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
Still not filtering.
Am guessing because no selection is being made.
I made the list to allow multiple selection, and when selecting more than one record, it is returning only one. (The last one to be specific)
 

KitaYama

Well-known member
Local time
Tomorrow, 07:53
Joined
Jan 6, 2022
Messages
1,575
I didn't see you mentioning multi selecting.
Check this

Code:
Dim SelectedID As String
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
    
    With lbSearchResults
        If .ItemsSelected.Count > 0 Then
            For index = 0 To .ListCount - 1
                If .Selected(index) Then
                    SelectedID = SelectedID & .Column(0, index) & ","
                End If
            Next
            SelectedID = Left(SelectedID, Len(SelectedID) - 1)
            Forms![Contact List].Filter = "ID In (" & SelectedID & ")"
            Forms![Contact List].FilterOn = True
        Else
            Forms![Contact List].FilterOn = False
        End If
    End With
 

Attachments

  • Contacts V3.accdb
    2.2 MB · Views: 81

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
I didn't see you mentioning multi selecting.
Check this

Code:
Dim SelectedID As String
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
   
    With lbSearchResults
        If .ItemsSelected.Count > 0 Then
            For index = 0 To .ListCount - 1
                If .Selected(index) Then
                    SelectedID = SelectedID & .Column(0, index) & ","
                End If
            Next
            SelectedID = Left(SelectedID, Len(SelectedID) - 1)
            Forms![Contact List].Filter = "ID In (" & SelectedID & ")"
            Forms![Contact List].FilterOn = True
        Else
            Forms![Contact List].FilterOn = False
        End If
    End With
This is working perfectly as long as items are selected.
but... is it possible to have the filter based on the current list result regardless of selected values?
That listbox is not intended to be clicked, just display the search result, and I want to use that same result as a filter for the other form. (it's gonna always be the whole list whether selected or not)
Did I make sense? :p
 

cheekybuddha

AWF VIP
Local time
Today, 23:53
Joined
Jul 21, 2014
Messages
2,321
Did I make sense? :p
Ah OK, that's a bit different!

Just to be sure: you want the original form to be filtered with the results from the filter form (more than one if more than one is returned).

If no results returned in the filter form, then don't filter the calling form

?
 

KitaYama

Well-known member
Local time
Tomorrow, 07:53
Joined
Jan 6, 2022
Messages
1,575
Check this
Code:
    Dim SelectedID As String
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
    
    With lbSearchResults
        For index = 0 To .ListCount - 1
            If .ItemData(index) Then
                SelectedID = SelectedID & .Column(0, index) & ","
            End If
        Next
        If SelectedID = "" Then
            Forms![Contact List].FilterOn = False
        Else
            SelectedID = Left(SelectedID, Len(SelectedID) - 1)
            Forms![Contact List].Filter = "ID In (" & SelectedID & ")"
            Forms![Contact List].FilterOn = True
        End If
    End With
 

Attachments

  • Contacts V3.accdb
    2.2 MB · Views: 100

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
Ah OK, that's a bit different!

Just to be sure: you want the original form to be filtered with the results from the filter form (more than one if more than one is returned).

If no results returned in the filter form, then don't filter the calling form

?
My form "Contact List" opens up on db launch and has all contacts in continuous form.
I will add a button that will open the search form (we worked on), and after doing the search, take the result from the listbox, and use it as filter for "contact list" to display the same records as per the search result.
Contact list has much more info that the listbox can display, so users will select any record they want to edit from it (instead of the listbox)
 

Chaga

Member
Local time
Tomorrow, 01:53
Joined
Aug 19, 2022
Messages
34
Check this
Code:
    Dim SelectedID As String
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
  
    With lbSearchResults
        For index = 0 To .ListCount - 1
            If .ItemData(index) Then
                SelectedID = SelectedID & .Column(0, index) & ","
            End If
        Next
        If SelectedID = "" Then
            Forms![Contact List].FilterOn = False
        Else
            SelectedID = Left(SelectedID, Len(SelectedID) - 1)
            Forms![Contact List].Filter = "ID In (" & SelectedID & ")"
            Forms![Contact List].FilterOn = True
        End If
    End With
YES YES YES!
Thank a million mate!

Forms![Contact List].Filter = "ID In (" & SelectedID & ")"

That was the line I needed.
 

KitaYama

Well-known member
Local time
Tomorrow, 07:53
Joined
Jan 6, 2022
Messages
1,575
Use this.
This one is shorter and easier to understand.

Code:
    Dim SelectedID As String
    Dim index As Integer
    If Not CurrentProject.AllForms("Contact List").IsLoaded Then Exit Sub
    
    With lbSearchResults
        For index = 0 To .ListCount - 1
            SelectedID = SelectedID & .Column(0, index) & ","
        Next
        If SelectedID = "" Then
            Forms![Contact List].FilterOn = False
        Else
            SelectedID = Left(SelectedID, Len(SelectedID) - 1)
            Forms![Contact List].Filter = "ID In (" & SelectedID & ")"
            Forms![Contact List].FilterOn = True
        End If
    End With
 

KitaYama

Well-known member
Local time
Tomorrow, 07:53
Joined
Jan 6, 2022
Messages
1,575
I just wanted to add this.

I personally never filter a form. I change the record source
In your case instead of

Forms![Contact List].Filter = "ID In (" & SelectedID & ")"

I would use

Forms![Contact List].RecordSource = " SELECT * FROM [Contacts Extended] WHERE ID In (" & SelectedID & ")"

And never use space in object names.
 

Users who are viewing this thread

Top Bottom