+ Reply to Thread
Results 1 to 19 of 19

SQL in VBA - Concatenating 2 fields

  1. #1
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    SQL in VBA - Concatenating 2 fields

    Hi,
    I have the following but it keeps throwing up an error, all works fine in other queries where i'm not trying to concatenate 2 fields.
    What is wrong?

    strSQL = "SELECT Format([org_param_value].[param_effective_date],0) & [org_parameter].[param_id] AS [LookupValue], org_param_value.param_effective_date, org_parameter.param_id, org_parameter.param_daily_name, org_param_value.param_value, org_param_value.param_rag, daily_report_items.cmi_parent, daily_report_name.cmn_id " & _

    Thanks in advance.

    Kerry

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: SQL in VBA - Concatenating 2 fields

    Shouldn't that first & be a + instead?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    That hasn't worked, I get the message:
    vba_sql_error.png

    My full query is:
    Please Login or Register  to view this content.
    Last edited by KML1976; 02-03-2016 at 06:24 PM. Reason: inserting image

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: SQL in VBA - Concatenating 2 fields

    Independently what is an example of those two values and the expected concatenation result?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL in VBA - Concatenating 2 fields

    Kerry

    Have you tried executing the SQL statement directly in the database?
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: SQL in VBA - Concatenating 2 fields

    Shouldnt the formatting part be enclosed in quotation marks

    Format([org_param_value].[param_effective_date],'0') (try double quotes if this doesnt work, just be sure you double them up when creating the string)

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL in VBA - Concatenating 2 fields

    gbeats101

    FORMAT is a MySQL function and as far as I can recall it doesn't need the arguments to be enclosed in quotes.

  8. #8
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: SQL in VBA - Concatenating 2 fields

    This is incorrect, it is a required syntax to use quotation...

    EDIT: just tested it on msql server management studio and it returns an error in the second parameter if you dont use quotation

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL in VBA - Concatenating 2 fields

    The OP is working with a MySQL database not a MSSQL database - look at the error message in post #3.

    http://dev.mysql.com/doc/refman/5.7/...unction_format

  10. #10
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: SQL in VBA - Concatenating 2 fields

    Ah ok, im sorry, this function for mysql seems to format numbers with x decimal digits, in this case (i cannot test this but) it seems that the OP is trying to format a date with 0 decimal figures........is this going to be the problem?

    Format([org_param_value].[param_effective_date],0)

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL in VBA - Concatenating 2 fields

    No problem.

    When I first saw the code myself I thought the OP was trying to use the VBA Format function.

    Just looking at the error message I think the problem lies/starts somewhere here.
    Please Login or Register  to view this content.
    I'd test it but I don't really have time to set up something to test it with.

  12. #12
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    Quote Originally Posted by Norie View Post
    Kerry

    Have you tried executing the SQL statement directly in the database?
    I am unable to do this, we can only link and query in Access or via VBA.

  13. #13
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    This is what i'm trying to achieve.
    thanks

    query output.png

  14. #14
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    Quote Originally Posted by Norie View Post
    No problem.

    When I first saw the code myself I thought the OP was trying to use the VBA Format function.

    Just looking at the error message I think the problem lies/starts somewhere here.
    Please Login or Register  to view this content.
    I'd test it but I don't really have time to set up something to test it with.
    I've tried using + instead of & and that doesn't work.

  15. #15
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    Quote Originally Posted by gbeats101 View Post
    Shouldnt the formatting part be enclosed in quotation marks

    Format([org_param_value].[param_effective_date],'0') (try double quotes if this doesnt work, just be sure you double them up when creating the string)
    I've tried '0' and double quotes too (doubling them up) and i'm getting the same error

  16. #16
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    OK, i've been allowed to install HeidiSQL so i'm going to give it a go there.

  17. #17
    Registered User
    Join Date
    01-17-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    23

    Re: SQL in VBA - Concatenating 2 fields

    I've run this directly in the db:

    Please Login or Register  to view this content.
    and it is returning this:
    db query output.png
    The lookup value is incorrect, I think it's actually adding the 2 values together instead of just concatenating them.
    Attached Images Attached Images
    Last edited by KML1976; 02-04-2016 at 06:16 AM.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: SQL in VBA - Concatenating 2 fields

    What happens if you use & instead of + to concatenate?

    If that doesn't work try using the CONCAT function.

  19. #19
    Forum Contributor
    Join Date
    01-08-2016
    Location
    Cagayan De Oro City, Philippines
    MS-Off Ver
    2013
    Posts
    152

    Re: SQL in VBA - Concatenating 2 fields

    have a look at this, it may (or may not) be the problem

    Link

    like i mentioned earlier i noticed your using FORMAT() on what appears to be a date field......

+ 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. Replies: 5
    Last Post: 11-21-2014, 10:00 PM
  2. Concatenating 2 Fields in VBA SQL
    By stonesfan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2014, 01:17 PM
  3. [SOLVED] Concatenating two columns and reversing after concatenating the result
    By irfanparbatani in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-22-2014, 09:35 PM
  4. Build a URL by Concatenating Two Fields of a Spreadsheet.
    By dustywoodworker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2013, 09:30 AM
  5. Need help concatenating fields from different workbooks
    By excelhelp14 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-16-2012, 12:57 PM
  6. How to Disable automatic fields in pivot table like total and count on fields
    By anushka in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-24-2009, 07:53 AM
  7. [SOLVED] Excel SHOULD NOT AUTO-CHANGE formated text fields to DATE FIELDS!
    By PSSSD in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-08-2006, 04:35 PM

Tags for this Thread

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