Results 1 to 10 of 10
  1. #1
    to47122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    5

    iif statement in a query

    Help please



    I am querying my primary table off the primary key. From that query I get [CustomerPartNumber]. Then [CustomerPartNumber] is linked to another secondary table [PartNumber]. The problem is sometimes [CustomerPartNumber] has extra characters in it that I don't want. Example: Primary table has (001)123456 and the secondary table has 123456, these are the same and I need them to be linked. I want to check if [CustomerPartNumber] start with "(" and if it does I want to skip the first 5 characters and finish the query. How do I do this in the Design view of the Query. I tried the following in the Criteria field on both fields.

    iif(left([CustomerPartNumber],1)="(",mid([CustomerPartNumber],6),[CustomerPartNumber])

    Help Please

  2. #2
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94

    iif statement in a query

    Try changing the mid to a right.

  3. #3
    to47122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    5
    right will not work because i do not know the length of the feild

  4. #4
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    OK - I see what you are doing. If you are not including the expression, then you will need to:

    Code:
    nochris: IIf(Left([first_name],5)="Chris",Mid([first_name],6),[first_name])
    Attached Thumbnails Attached Thumbnails Screen Shot 2012-09-15 at 12.39.45 PM.png  

  5. #5
    to47122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    5
    Im not sure I understand. How does that help to continue the query. Sorry I am a beginner over my head. Please see attached

    Click image for larger version. 

Name:	Capture.JPG 
Views:	7 
Size:	57.5 KB 
ID:	9172

  6. #6
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    Create a new field in the query with the expression. Make sure you give it a new name such as "checkedpartnumber". Choose a blank column in the query and under the field name enter this:

    Code:
    checkedpartnumber:iif(left([CustomerPartNumber],1)="(",mid([CustomerPartNumber],6),[CustomerPartNumber])
    Then in your forms that use the query, refer to the field [checkedparnumber] instead of [CustomerPartNumber]

  7. #7
    to47122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    5
    I need to change the customer part number mid query because if it does the query will not return any information to the form. the two customer part numbers are linked and I need information out of the secondardy table

  8. #8
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    Then you can insert the following code in the field name and either eliminate the unmodified field of the same name or rename it.

    Code:
    CustomerPartNumber:iif(left([dbo_PSC17OrderDetail].[CustomerPartNumber],1)="(",mid([dbo_PSC17OrderDetail].[CustomerPartNumber],6),[dbo_PSC17OrderDetail].[CustomerPartNumber])
    To rename the original field insert this into the field name:

    Code:
    OriginalCustomerPartNumber:[dbo_PSC17OrderDetail].[CustomerPartNumber]

  9. #9
    to47122 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2012
    Posts
    5
    Thank you that will work but how do I get the new [CustomerPartNumber] linked to [CustPart]. I do not have access to the source table to rename the feilds?

  10. #10
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    If I understand you correctly, you want to use the (sometimes) modified part number to link two tables. To do this, you are going to need two queries. The first will use the IIF expression to generate a calculated field. Let's call that field CustomerPartNumber as you suggested, and call the query Query1. Now create a second query, with Query1 and Partnumber, and link the two using CustomerPartNumber in Query1 and the corresponding field in the PartNumber table. Use the new Query2 in your form.

    John

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

Similar Threads

  1. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  2. Replies: 3
    Last Post: 07-10-2012, 05:23 AM
  3. If statement in Query
    By Jojojo in forum Queries
    Replies: 5
    Last Post: 11-18-2011, 03:02 PM
  4. Iif Statement in query
    By evanhughes in forum Queries
    Replies: 1
    Last Post: 11-03-2011, 08:55 AM
  5. Help fix SQL Statement for query
    By Budro49 in forum Queries
    Replies: 12
    Last Post: 06-21-2011, 02:26 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