Results 1 to 6 of 6

Thread: [RESOLVED] SQL IN WHERE condition using textbox on form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Resolved [RESOLVED] SQL IN WHERE condition using textbox on form

    I'm trying to get a report to limit to the regions/departments that are specific to the ones in a textbox in a form.

    I can get them to display in a report by using the .value, however I now matter what I do, I can't get it to display anything if I put it in the IN condition.

    Code:
    SELECT Rgn_dept, forms!frmReports!txtAllRegions.value
    FROM MyTable
    WHERE  Rgn_dept IN (forms!frmReports!txtAllRegions.value)
    I have even tried

    Code:
    SELECT Rgn_dept, forms!frmReports!txtAllRegions.value
    FROM MyTable
    WHERE  Rgn_dept IN (forms!frmReports!txtAllRegions)
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: SQL IN WHERE condition using textbox on form

    Try this

    Code:
    SELECT Rgn_dept, Trim(forms!frmReports!txtAllRegions.value)
    FROM MyTable
    WHERE  Rgn_dept IN (Trim(forms!frmReports!txtAllRegions.value))
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: SQL IN WHERE condition using textbox on form

    Koolsid,

    Nope that didn't work either.

    Its like the IN command is having issues with the fact the items to look at, the comma seperated list of values, is in a textbox on a form, instead of hard coded values.

    If I replace the forms!frmReports!txtAllRegions.value with the comma seperated values 14,19,5,18,4,3,17,23,28,24,16,15,6,7,2,8,9,1,11,22,21,26,12,25,20,10,27 everything works fine.

    This list above I copied directly from the textbox.

    I guess I'm going to have to go with an exists query. I'm trying to limit the regions/departments that the managers can see, to the ones they manage.

    I don't want to just throw it in as another joined table, because admins have only 1 region they belong to, but there level is Admin, which automatically gives them access to all regions.

    I'm thinking about just doing a where
    Code:
    EXISTS (
    SELECT tblManagers.Rgn_Dept 
    FROM tblManagers 
    WHERE MyTable.Rgn_Dept = tblManagers.Rgn_Dept 
    AND tblManagers.EmployeeID = forms!frmReports!txtEmployeeID)
    I wanted to avoid this method, as I'm sure it requires more processing/data to be sent accross the network.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL IN WHERE condition using textbox on form

    Quote Originally Posted by rack View Post
    Its like the IN command is having issues with the fact the items to look at, the comma seperated list of values, is in a textbox on a form, instead of hard coded values.
    That is entirely expected... because the textbox contains a single value (a string containing multiple numbers and commas), whereas what you type in to the query is multiple number values separated by commas.

    I'm not aware of a way to write a query that would treat the single value as multiple for an IN list, and suspect the best way for a query would be a table to store records for each of the values.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: SQL IN WHERE condition using textbox on form

    [QUOTE=si_the_geek;3774030]That is entirely expected... because the textbox contains a single value (a string containing multiple numbers and commas), whereas what you type in to the query is multiple number values separated by commas.
    QUOTE]

    Ah so its looking to match "14,19,5,18,4,3,17,23,28,24,16,15,6,7,2,8,9,1,11,22,21,26,12,25,20,10,27" with each Rgn_Dept field value. That would make sense for it not showing any records then.

    The Exists query in the WHERE condition works. I don't create tables on the fly in any of my code/database(s), or that would be a good idea Si_the_geek. I"ll use the Exists where condition instead for now.

    Thank you both for the input, and Si_the_Geek for the explanation, that helps a lot knowing what it is trying to do.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL IN WHERE condition using textbox on form

    Quote Originally Posted by rack View Post
    I don't create tables on the fly in any of my code/database(s), or that would be a good idea Si_the_geek.
    There are times when it can be useful, but in general within Access it should be avoided.

    As you've got something that works, I'd recommend sticking with it.

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
  •  



Click Here to Expand Forum to Full Width