Page 1 of 4 1234 LastLast
Results 1 to 15 of 50
  1. #1
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664

    SQL code error

    Consider the following SQL code

    Code:
    SELECT [tblPersonnel].[PersonnelID], [FirstName] & " " & [LastName] AS FullName, emailaddress FROM tblPersonnel ORDER BY [FirstName] & " " & [LastName] WHERE FullName IS NOT NULL;
    The last part WHERE FullName IS NOT NULL



    was added to the SQL statement to prevent the combo box from selecting or allowing blank spaces. The semi-colon was moved to the end of the line when the new SQL code as shown above was added.

    Now I get a error whenever I click on the combo box to access its choices of employee names.

    The error is shown in the attached snagit screen files. It claims there is a syntax error, but all I did was insert the SQL code fraction (above) and then place the semi-colon at the end of the now longer line of code. I do not see this as causing an error.

    What is going on?

    Any help appreciated. tanks in advance.

    Respectfully,

    Lou Reed
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    FullName is not a field in tblPersonnel, it is calculated in the query. Cannot reference field generated by calculation in the query in the WHERE clause. Reference LastName instead.
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    you would need to use

    WHERE [FirstName] & " " & [LastName] is not null

    but it never will be null because as a minimum, you've added a space. So try

    WHERE [FirstName] is not null and [LastName] is not null


    Also, the order for query sections is

    SELECT
    FROM
    WHERE
    ORDER BY

    and you have the last two the wrong way round

  4. #4
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Okay, looks like you are right. I am use a calculated filed in he WHERE clause ad it fails.

    But isn't

    WHERE [FirstName] & " " & [LastName] is not null also a calculated field?

    I use FullName and you use [FirstName] & " " & [LastName]. So how do you get way with it and I do not?

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  5. #5
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    I'm not getting way with anything. I'm just following the rules of how sql is interpreted

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Lou, Ajax said the same thing I did - cannot reference calculated field and to eliminate the error would have to repeat the concatenation expression. Also, Ajax said that your concatenation expression would not filter as you want because of the space. Then provides alternative. Not getting away with anything.

    I failed to notice the incorrect order of clauses.
    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.

  7. #7
    Join Date
    Apr 2017
    Posts
    1,776
    Hi

    In addition to anwers from others:

    Quote Originally Posted by Lou_Reed View Post
    Consider the following SQL code
    Code:
    SELECT [tblPersonnel].[PersonnelID], [FirstName] & " " & [LastName] AS FullName, emailaddress FROM tblPersonnel ORDER BY [FirstName] & " " & [LastName] WHERE FullName IS NOT NULL;
    Lou Reed
    Access queries can't operate very well with empty fields, so use Nz() where you suspect them - otherwise some records may be lost in result table.

    SELECT [tblPersonnel].[PersonnelID], Trim(Nz([FirstName],"") & " " & Nz([LastName],"")) AS FullName, emailaddress FROM tblPersonnel ORDER BY [FirstName] & " " & [LastName] WHERE Trim(Nz([FirstName],"") & " " & Nz([LastName],""))<>"";
    or check FirstName and Lastname separately, as it was advised before.

  8. #8
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,511
    Lou you should use the query designer more, it helps keep the code error free.

    For the RowSource of your combo box, click on the ... to go to the query builder, it should put things as it needs to avoid errors. One of the fields is FullName: [FirstName] & " " & [LastName], sort on this field and in the criteria box of this field put Is Not Null, then run it to see if it gives what you need. you can also put it in SQL code mode to see what that looks like.

  9. #9
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I apologize for the use of the of the term "getting away with it". I should have said yours works and mine does not. I was just curious as to why I could not use FullName because it was a calculated field and
    it seemed the you did use a calculated field.

    I just thought that maybe my syntax was more explicit than yours on using a calculated field, but either way it should be wrong. We are using as calculated field! So I will use your syntax and I am very thankful for it. Please do not misunderstand. I just do not see any difference in the two - at first glance; that is all.

    I must really apologize if you mistook the tone of the post. It was not intended. The minute you said you are using a calculated field, I knew what the error was.

    Thanks so much for your help.

    Respectfully,

    Lou Reed

  10. #10
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    It now works in that it does not crash with a runtime error of 2465 on that line. It crashes on the next line with a runtime error of 2162.

    The reason was my brain lock when you said control. It was just a question of terminology. Once I new what you said it was simple.

    However, as I said it now crashes on the next line of the VBA (see attached screenshot).

    I am also including the db in one of the attached fields.

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed


    Respectfully,


    Lou Reed

  11. #11
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    Here are the files that should have been put in the previous post.

    Respectfully,


    Lou Reed
    Attached Files Attached Files

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    no idea what runtime error 2162 is. Provide a description and perhaps it will tell me what the problem is

  13. #13
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    The error read is pop-up dialogue box

    "A Macro set to one of the current field's properties failed because of an error in a FindRecord action argument"

    I do not know what that means. I hope someone on the forum does, because it would seem that the VBA code should work.

    Is there a work-around for this? Some code modification to avoid the runtime error 2162.

    Any help appreciated. Thanks in advance.

    Respectfully,

    Lou Reed

  14. #14
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Regret I don't know what it means either. But if you google the error message in it's entirety you will find several links to other forums where other developers have had the same message - perhaps one of those will give you a clue as to what is wrong with your application.

  15. #15
    Lou_Reed is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    1,664
    I have tried that there is no definitive answer. They suggest you might try this or that.........? Nothing clear.I will keep trying.

    Respectfully,

    Lou Reed

Page 1 of 4 1234 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error in VB code
    By Lou_Reed in forum Access
    Replies: 19
    Last Post: 09-21-2016, 08:50 AM
  2. Error in VB code
    By Lou_Reed in forum Access
    Replies: 1
    Last Post: 09-19-2016, 02:47 PM
  3. code error??
    By Jen0dorf in forum Access
    Replies: 1
    Last Post: 11-14-2015, 12:16 PM
  4. Error in code
    By cbende2 in forum Programming
    Replies: 5
    Last Post: 06-04-2015, 10:39 AM
  5. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM

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