Results 1 to 11 of 11
  1. #1
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36

    Syntax in access query using square brackets bested in another set

    Hello, again...
    er... thtle s/b "nested" not "bested" (fat fingers)
    I want to select a substring of a field's value in the Expression Builder in an Access query.

    Here's what I have:
    Code:
    FN:= [SocialAttendiesTbl].[Left([FirstName],2)]
    I *think* I've done this in the past, but I really am getting older.



    Thanks in advance.

    Bud

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Show what does it show?
    I would use Left on the WHOLE reference and not just the fieldname, plus you could omit the table name. That is just the access way, as the From signifies where the data comes from.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    Square brackets don't go around expressions. Field names, sure, but not expressions. Then again, you can't use a function as part of the field reference. Perhaps you don't need the table reference at all.

    FN:Left(FirstName,2)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by Micron View Post
    Square brackets don't go around expressions. Field names, sure, but not expressions. Then again, you can't use a function as part of the field reference. Perhaps you don't need the table reference at all.

    FN:Left(FirstName,2)
    Guess I poorly worded my problem.. perhaps this will be clearer:
    Here's what I'm looking for...
    Attendie's name, consisting of the first two characters of their Firstname and a space and their Lastname

    Code:
    AbbreviatedName:= [SocialAttendiesTbl]![Left([FirstName],2)] & " " & [SocialAttendiesTbl]![Lastname]
    When running the nested [] throw an error.

    I need to reference the table name, as there is another table with Firstname and Lastname in the query.

  5. #5
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    If multiple tables have that field name, the reference to the table is necessary.

    OP:
    As a general advice, when creating queries, first make sure you get the values using the simplest reference to them. In this case, you should start with just:
    Code:
    FirstName
    Run it and see if you got the value. If you have multiple tables/queries with FirstName as a field, then you must specify the table/query name before the field and then the dot, like this:
    Code:
    SocialAttendiesTbl.FirstName
    Run it and see if you got the value. If your table/query or your field had spaces or symbols, then you must add square brackets (Access adds them anyway to make sure), for example:
    Code:
    [Social Attendies Table].FirstName
    Run it and see if you got the value.
    Once you have the reference right, use that reference as a whole to create expressions with it. For example, if you just add a Left function, access adds an alias automatically, like:
    Input:
    Code:
    Left(FirstName,2)
    Becomes:
    Code:
    Expr1: Left(FirstName,2)
    "
    Speaking of aliases, you can also specify an alias for your SocialAttendiesTbl in its properties within the query builder. If you assign "s" for that table, then you can write it like this: s.FirstName within the columns, making things easier for you.
    Finally, your expression can be any of the following:
    Code:
    FN:Left(FirstName,2)
    FN:Left(SocialAttendiesTbl.FirstName,2)
    FN:Left([SocialAttendiesTbl].[FirstName],2)
    or if aliased
    Code:
    FN:Left(s.FirstName,2)
    which Access modifies to
    Code:
    FN:Left([s].[FirstName],2)
    EDIT:
    Instead of
    Code:
    AbbreviatedName:= [SocialAttendiesTbl]![Left([FirstName],2)] & " " & [SocialAttendiesTbl]![Lastname]
    Try
    Code:
    AbbreviatedName: Left([SocialAttendiesTbl].[FirstName],2) & " " & [SocialAttendiesTbl].[Lastname]
    Last edited by Edgar; 03-25-2023 at 07:48 PM. Reason: OP replied while I was writing my post, typos

  6. #6
    Budatlitho is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Mar 2016
    Posts
    36
    EUREKA!
    FN: Left([SocialAttendiesTbl].[FirstName],2) & " " & [SocialAttendiesTbl].[LastName]

    It woikes! Thanks.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    So logic says if you have same fields in two tables in the query, then you need to use the Left() function on one of them?
    Last edited by Welshgasman; 03-26-2023 at 03:31 AM.

  8. #8
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    If your first name was Welsh and your last name was Gasman, OP was trying to get "We Gasman", which is done using the Left() function on FirstName and concatenating the result with LastName. OP needed to include the reference to the table SocialAttendiesTbl because there were other tables or queries that had FirstName as a field being picked up in the final query. Maybe there is another table called OperationStaffTbl where there would be a field for FirstName too, so these are the tables:

    SocialAttendiesTbl: Id, FirstName, LastName, EventID
    OperationStaffTbl: Id, FirstName, LastName, Salary, EventID

    And you need to return return the FirstName of the person from the OperationStaffTbl associated to each person in SocialAttendiesTbl:

    You shouldn't do this:
    Code:
    SELECT FirstName, FirstName, LastName
    FROM SocialAttendiesTbl
    INNER JOIN OperationStaffTbl
    ON EventID = EventID;
    You should do this:
    Code:
    SELECT os.FirstName AS oFirstName, sa.FirstName AS sFirsName, sa.LastName AS sLastName
    FROM SocialAttendiesTbl sa 
    INNER JOIN OperationStaffTbl os
    ON sa.EventID = os.EventID;
    But OP wanted to return "We Gasman", so:
    Code:
    SELECT os.FirstName AS oFirstName, LEFT(sa.FirstName, 2) & " " & sa.LastName AS SocialAttendeeName
    FROM SocialAttendiesTbl sa
    INNER JOIN OperationStaffTbl os
    ON sa.EventID = os.EventID;
    Last edited by Edgar; 03-25-2023 at 05:14 PM. Reason: typos

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    OP needed to include the reference to the table SocialAttendiesTbl because there were other tables or queries that had FirstName as a field
    Where is this stated, because I can't see it. In post 4 brackets were still around the expression even though I said they cannot be.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Edgar is offline Competent Performer
    Windows 8 Access 2016
    Join Date
    Dec 2022
    Posts
    274
    Micron,
    #2 and #3 are only partially correct, reference is necessary when there are repeated fields, explained in detail in posts #5 and #8
    #4 OP confirms repetition along with the problematic syntax
    #5 is how to fix
    #6 is confirmation of fix
    #7 is wrong
    #8 is why #2, #3 and #7 are wrong
    #9 ??? what do you mean?

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,791
    OK, I missed the part where it was declared that more than one table involved had the same field name.
    What do I mean?
    [Left([FirstName],2)]

    I say, you can't wrap an expression/function in brackets.
    Maybe I'm full of it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 11
    Last Post: 12-07-2021, 10:07 AM
  2. Replies: 5
    Last Post: 08-08-2020, 01:59 AM
  3. Brackets (] or } in Access Report
    By ijaz8883 in forum Reports
    Replies: 13
    Last Post: 10-25-2019, 07:25 AM
  4. Convert SQL Server Syntax To Access Query Syntax
    By chalupabatman in forum Queries
    Replies: 1
    Last Post: 10-18-2017, 08:53 PM
  5. Replies: 1
    Last Post: 07-12-2013, 01:15 PM

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