+ Reply to Thread
Results 1 to 22 of 22

Incorrect syntax?

  1. #1
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Incorrect syntax?

    Hello,
    There's this Module in Excel file (.xlsm)

    For some reason it is erroring.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I cannot figure out what it is. When I click on Debug it takes me to the Module and to this line
    Please Login or Register  to view this content.
    In the Properties under Connection, the SQL works. Verified it on SQL Server Management Studio 2014.

    Help would be appreciated.

    Here's the SQL that works as is in the Connection Properties for the Excel Macro:
    Please Login or Register  to view this content.
    Last edited by supportservice; 08-25-2015 at 12:31 PM.

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Incorrect syntax?

    just a little confused....the only thing I see "weird" is, in the SQL on the line where "pm", you have a few +'s and I'm wondering if they should be &'s....

    but you say the SQL is working so I'm not sure exactly what's going on..

    I could be totally off base...
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Since I'm not that familiar with working with either, what I did try is the SQL and it produces the results.

    But do not see what could be wrong on the Excel side.

    All I know is that it's erroring on 'pm' somewhere.

    hope someone can see what and where the 'pm' is a problem

  4. #4
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    This is the SQL:
    Please Login or Register  to view this content.
    How about this ...
    How do I change this for user input for the date range portion?
    Please Login or Register  to view this content.

  5. #5
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Incorrect syntax?

    a few questions...

    1) are you still getting the error?
    2) are you running this SQL (post #4) from an XL Macro?

    I use the code below for Oracle SQL when running from a PL/SQL window (NOT XL)
    Please Login or Register  to view this content.
    this technique prompts the user for input

    Please Login or Register  to view this content.
    in XL I would create a variable and use INPUTBOX to ask the user for the date

    then change your code to the following:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    I will try that ...

    Anyone know why I am getting the error though?
    Please Login or Register  to view this content.
    If that can be found and fixed, trying to change and do the input parameters will be moot.
    I was going for the input parameters if can't find the resolution to the original error.

    However if using the same code and modifying for the date prompts, the error will still exist.

  7. #7
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Incorrect syntax?

    have you tried changing the +'s to &'s


    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 08-25-2015 at 05:43 PM. Reason: Added missing CODE tags.

  8. #8
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Quote Originally Posted by judgeh59 View Post
    have you tried changing the +'s to &'s


    Split3 = " PersonMembership.CreatedDate > DATEADD( day, -120, GETDATE() ) AND " & _
    " ( ( SUBSTRING( invWork.BACHNUMB, 1, 8 ) >= '" + StartingBatchDate + "' AND SUBSTRING( invWork.BACHNUMB, 1, 8 ) <= '" + EndingBatchDate + "' ) " & _
    "OR ( SUBSTRING( invHist.BACHNUMB, 1, 8 ) >= '" + StartingBatchDate + "' AND SUBSTRING( invHist.BACHNUMB, 1, 8 ) <= '" + EndingBatchDate + "' ) ) ) pm "
    Replaced with that and still the same error

  9. #9
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Incorrect syntax?

    the best I can come up right now is to have you upload the file so I can see what is going on....right now I'm guessing on how this is all interconnected....

    sorry about that...

  10. #10
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Quote Originally Posted by judgeh59 View Post
    the best I can come up right now is to have you upload the file so I can see what is going on....right now I'm guessing on how this is all interconnected....

    sorry about that...
    I'm attaching. the codes shared at the beginning is a copy & paste from the .xlsm file, so not sure having the Excel file with the codes in it makes much of a difference?

    Membership Card Export.zip
    Last edited by supportservice; 08-25-2015 at 04:43 PM.

  11. #11
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Incorrect syntax?

    I'm sending this to the Guru's.....sorry I couldn't help....

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Incorrect syntax?

    You're missing a comma and a space after REGION in the second line. I don't think it's causing your error but it's not helping

  13. #13
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Incorrect syntax?

    Looks like you have an extra closing parenthesis on your where clause (in the excel macro) right before the alias name pm.

    EXCEL:
    Please Login or Register  to view this content.
    SQL FROM OTHER POST:
    Please Login or Register  to view this content.
    I'm just comparing those 2 lines.

    If that doesn't work - I would interrupt the code and somehow get excel to kick the full SQL statement to a workbook - then I would copy out that text to the mgt studio and see what happens when I run it.

    It should be easier to see any bad chracters (missing spaces, extra parenthesis, unclosed single quotes, etc.) from within the management studio.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  14. #14
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    I removed the "extra" ")" and still the same error.
    NOTE: It appears "AS" is allowed or rather used in the Module. It errors when I added that there as well.

    In Split 2 is where the Alias is assigned from what i can tell. However when the 'pm' is removed at the end of that substring line the error is on 'JOIN' at the end of Split 3.

    The references used in these to sections only has PersonMembership from attribute.PersonMembership or the Alias 'pm'

    Yet in the first line in Split 4, it uses the Alias 'pm'.

    I've tried replacing the PersonMembership to pm as well as attribute.PersonMembership in Split 2 and Split 3, no go. same error.

    And attempting to Step Into and Run, it errors at

    ActiveWorkbook.Connections("RegularMemberships").Refresh

  15. #15
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    I also copied the SQL that works in SSMS and used in the Properties Definition for the Connection and split it in the Excel Module:
    Please Login or Register  to view this content.
    The error is now on 'attribute'

  16. #16
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Any Excel experts out there see why still would be happening and what needs to be modified?

  17. #17
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Incorrect syntax?

    Why aren't you outputting the result and running that? If you do your current issue becomes clear:
    Please Login or Register  to view this content.
    You've also still not added the required space after REGION

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Incorrect syntax?

    You're also missing a comma after 'CityStateZip' (as well as REGION)

  19. #19
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Kyle123,
    those did not work.

    same error

    Incorrect syntax near 'pm'

  20. #20
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Finally figured it out.

    Changed from this
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    Oddly the other placements didn't error that wasn't using 'pm'

    I also removed the CreatedDate line. They said it's incorrect anyway to use that field.
    Last edited by supportservice; 08-27-2015 at 11:52 AM.

  21. #21
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Incorrect syntax?

    EDIT: I realize this is solved for you - but this general idea below (work it out in SMSS, go to notepad to add the S = S & " , then copy to VBA and alter as needed with variables in place of hard coded values...) is good to know in general. You would have found your error immediately in the management studio (it's hard to see it once you are letting excel run it under the hood).

    Well - I'm having trouble reading through all of that - here's my suggestion.

    IN GENERAL - when you have a query that is working well in the SMSS just copy it over directly to notepad and add S = S & " To ever line. THERE IS A SPACE THERE!

    THEN just copy over your notepad code to the code module and viola - usable code is born!


    NOTE: I got this from your earlier post - you indicated that the query did in fact work.

    When you paste your Notepad text into your VBA module, excel adds the final " on each line. The nice thing about this is you can be certain you have all the spaces needed...

    All you need to do is place this in the code, then pass it to your command text :D you'll just be passing "S" instead of Split1 + Split2 + ..... etc.




    Please Login or Register  to view this content.
    Last edited by GeneralDisarray; 08-27-2015 at 01:46 PM.

  22. #22
    Registered User
    Join Date
    08-13-2015
    Location
    Colorado Springs, CO
    MS-Off Ver
    2013
    Posts
    12

    Re: Incorrect syntax?

    Good to know! i'll try that.
    Though when i copied the SSMS working SQL, it still error'd

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Nested IF AND not working as I expect - Returns 'False' so probably is incorrect syntax
    By pongmeister in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 07-23-2015, 05:38 AM
  2. SQL code in VBA - incorrect syntax near
    By michiel soede in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-02-2015, 10:00 AM
  3. Incorrect Vlookup syntax
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-04-2014, 05:56 PM
  4. [SOLVED] Incorrect Syntax
    By ImStevenB in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-14-2014, 05:03 PM
  5. IF(COUNTIF statement seems to have incorrect syntax
    By dwiseman in forum Excel General
    Replies: 2
    Last Post: 04-27-2010, 10:10 AM
  6. Incorrect syntax near '#' problem when returning to MS Query fromExcel
    By Steen Persson (DK) in forum Excel General
    Replies: 0
    Last Post: 03-07-2006, 11:30 AM
  7. [SOLVED] Incorrect syntax near '#' problem when returning to MS Query fromExcel
    By Steen Persson (DK) in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-07-2006, 11:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1