Results 1 to 7 of 7
  1. #1
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58

    Replacing commas in Multi-Value Lists into Or Conditions in queries

    I have a multi-value list in a table which allows for users to select multiple departments.


    Click image for larger version. 

Name:	MultiValue List.JPG 
Views:	31 
Size:	25.2 KB 
ID:	43085

    I want to be able to use the multiple values in a query as "Or" Operators.
    So far I have created a Replace function that replaces the commas with the word "or"
    Click image for larger version. 

Name:	Commas.JPG 
Views:	30 
Size:	10.4 KB 
ID:	43086
    Click image for larger version. 

Name:	Replace FX.JPG 
Views:	30 
Size:	19.4 KB 
ID:	43087
    Replace function...
    Dim LResult1 As String
    LResult1 = Replace(DLookup("strUserDept", "tblUserRoles", "[strUserID]='" & Me.txtLoginName.Value & "'"), ",", " or")
    Me.txtUserDept = LResult1




    I have created a query that is supposed to pull the department names from the field above but i suspect that the entire entry is enclosed in quotes as it is a text field
    So instead of "Medicine" or "Neurology" i believe it is returning "Medicine or Neurology" as one piece of criteria. Cant tell for sure as query returns no records.
    I expect the department values to range from one to several.

    Is there a way to substitute the word "or" with the Or operator?
    Any suggestions?
    Thanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    You are using a multi-value field. Nature of MVF is the field actually only stores pointers to records in a hidden table. Access formats the field to display values as if they are comma-separated but in reality there is no comma-separated string and no comma to replace. https://support.microsoft.com/en-us/...3-b6624e1e323a

    I NEVER use MVF.

    Even if you could substitute comma with OR, criteria would have to repeat the field like: Department = "Medicine" OR Department = "Neurology".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is an article by Colin Riddington (isladogs) as to why Multivalued Fields - BAD

  4. #4
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,572
    I agree with Steve & June I never use Multi Value Fields
    It is always best to store the required Departments as Records in a separate table which makes it very easy to query for the required values.
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,743
    Perhaps you could step back and tell us about your application and current table designs in simple English.
    With some emphasis on your need for, and use of, multiple values. There may be design options to simplify (ease of maintenance, potential to move to different DBMS...), but readers need more info about the application and any plans that may exist.
    As others have advised, most avoid multi-valued fields, and use traditional lookup tables when required.
    Good luck with your project.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,446
    want to be able to use the multiple values in a query as "Or" Operators.
    aside from the issues of using multi value fields you might want to consider using the IN operator. So rather than

    myVal="A" OR myVal="B" OR myVal="C"....

    you would use

    myVal IN ("A","B","C".....)

    i believe it is returning "Medicine or Neurology"
    I doubt it if you have PK's in the table where these are listed. More likely to be returning

    1 or 3 or 2

    add the code
    debug.print LResult1

    after you have run your dlookup code

  7. #7
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58

    Multi-Value list items as query criteria

    Quote Originally Posted by orange View Post
    Perhaps you could step back and tell us about your application and current table designs in simple English.
    With some emphasis on your need for, and use of, multiple values. There may be design options to simplify (ease of maintenance, potential to move to different DBMS...), but readers need more info about the application and any plans that may exist.
    As others have advised, most avoid multi-valued fields, and use traditional lookup tables when required.
    Good luck with your project.
    Thanks for your reply.
    I have found a solution that seems to be working.
    Posting this for anyone else who might come across this issue.
    I found some code that replaced text with an In operator.
    Here is an example...

    Me.RecordSource = "SELECT * FROM qryFieldsForAgreementsForm_SOM " _
    & " WHERE Department IN (" & Chr(34) & Replace([Forms]![_frmLoginVerify]![txtUserDept], " or ", Chr(34) & "," & Chr(34)) & Chr(34) & ") " _
    & " AND Placeholder = False"

    This runs when form is opened and replaced criteria that I originally placed in the query. Instead uses the code.

    Thanks again for the consideration and reply.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 02-17-2019, 08:46 AM
  2. Replacing tables name within queries
    By jaryszek in forum Access
    Replies: 5
    Last Post: 04-30-2018, 11:36 PM
  3. multi table queries
    By omid116200@gmail.com in forum Queries
    Replies: 2
    Last Post: 07-19-2016, 09:10 AM
  4. Queries with multiple conditions
    By dccjr in forum Queries
    Replies: 3
    Last Post: 11-08-2013, 12:57 AM
  5. Using Crosstab Queries for Data Lists?
    By JoeM in forum Queries
    Replies: 10
    Last Post: 10-02-2013, 06:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums