Can't get query criteria to read a form text field (1 Viewer)

nortonm

Registered User.
Local time
Today, 08:56
Joined
Feb 11, 2016
Messages
49
Hi - I have a list on a form that you can select multiple values, and when btn pressed it copies them to a text field. Then the other button should open the query with the contents of that text field in the criteria. But when you run the query it won't read directly from the form, and it brings up no values. However, If you copy the string into your clipboard and paste that exact same string manually into the criteria field and run the report, it finds all the values no problem! I've attached the screenshots. I've been at this 5 hours now, and I can't figure out what i'm doing wrong! Has anybody come across this inability to get the value from the text box?

On the 'Form' screenshot btn 'Copy List' code puts the values into the correct string (see 'Cohort List' in Form screenshot) to run the query (adding IN() and the "s.):

Code:
Dim oItem As Variant
    Dim sTemp As String
    Dim iCount As Integer

iCount = 0

If Me!lstCohort.ItemsSelected.Count <> 0 Then
        For Each oItem In Me!lstCohort.ItemsSelected
            If iCount = 0 Then
                sTemp = sTemp & Me!lstCohort.ItemData(oItem)
                iCount = iCount + 1
            Else
                sTemp = "In (" & """" & sTemp & """" & "," & """" & Me!lstCohort.ItemData(oItem) & """" & ")"
                iCount = iCount + 1
            End If
        Next oItem
    Else
        MsgBox "Nothing was selected from the list", vbInformation
        Exit Sub  'Nothing was selected
    End If

Me!txtCohortList.Value = sTemp
 

Attachments

  • Form.JPG
    Form.JPG
    19.5 KB · Views: 26
  • Query.JPG
    Query.JPG
    14.4 KB · Views: 27

ebs17

Well-known member
Local time
Today, 09:56
Joined
Feb 7, 2020
Messages
1,946
A listing is created in sTemp.
The criterion construct with IN requires a listing.
So: the beginning and end of the chain are consistent.

However, the content of a text field is exactly one value, here in the form of a string. When passed to this text field, the listing ceases to be a listing.

So something needs to change. There are various options for passing a multiple selection as a filter criterion.

 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:56
Joined
May 7, 2009
Messages
19,243
Code:
    Dim sTemp As String
    Dim oItem As Variant

    For Each oItem In Me.lstChort.ItemsSelected
        sTemp = sTemp & ",""" & Me.lstChort.ItemData(oItem) & """"
    Next
    If Len(sTemp) Then
        sTemp = "In (" & Mid$(sTemp, 2) & ")"
    Else
        MsgBox "Nothing was selected from the list", vbInformation
    End If
    Me.txtCohortList = sTemp
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:56
Joined
Sep 21, 2011
Messages
14,301
Could use Instr() perhaps?
Code:
SELECT tblDaily.DailyID, tblDaily.*, tblDaily.DailyID
FROM tblDaily
WHERE ((InStr(1,"3,5,6,8,9",[DailyID])>0));
 

ebs17

Well-known member
Local time
Today, 09:56
Joined
Feb 7, 2020
Messages
1,946
Could use Instr() perhaps?
Instr finds 1 not only in 1, but also in 21, 315, ... because it simply searches for a character or a substring.
Therefore, some additional precautions are necessary.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 08:56
Joined
Sep 21, 2011
Messages
14,301
Instr finds 1 not only in 1, but also in 21, 315, ... because it simply searches for a character or a substring.
Therefore, some additional precautions are necessary.
1 is the starting position?
However yes, extra characters like the , would be needed.

It was an example after all :(
 

nortonm

Registered User.
Local time
Today, 08:56
Joined
Feb 11, 2016
Messages
49
Apologies - I've had a bad week - (been dumped!) - i'll feed back as soon as I can.
 

nortonm

Registered User.
Local time
Today, 08:56
Joined
Feb 11, 2016
Messages
49
Many thanks for your replies. I didn't manage to get the array to be added to the query design grid successfully, so for now I've had to resort to combo box and single values in the query. I'll try and pick up the thread when I have more time, i'll update if I sort it. Many Thanks.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:56
Joined
Feb 19, 2002
Messages
43,275
Here's some examples that show how to use the In() clause.
 

Users who are viewing this thread

Top Bottom