+ Reply to Thread
Results 1 to 8 of 8

Getting Inner Join to work in a VBA-based query to MS SQL

  1. #1
    Registered User
    Join Date
    01-16-2013
    Location
    Bodo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    5

    Question Getting Inner Join to work in a VBA-based query to MS SQL

    Hi, I am having trouble getting SQL-data by using VBA.

    Everything Works fine until I use the "inner Join"-sql command.

    Basicly I want to exctract the newest updates from a employee table. The table would look something like this:


    EmployeeNo ChangeNo EmploymentStatus FromDate
    1 10 Working 01.01.2014
    2 11 Working 01.02.2014
    2 12 Sick 01.03.2014
    1 13 Quit 01.03.2014

    The ChangeNo is a PK, and will increase for each New row in the table.

    I want the Query to understand that the ChangeNo 12 and 13 is the largest, and then display EmployeeNo, ChangeNo, EmploymentStatus and FromDate for the row With ChangeNo 12 and 13.
    To do this, I have tried to tweek an excelent macro by Carl.

    Unfortunately, I am struggeling with the "inner join"-bit of my macro.


    Please Login or Register  to view this content.

    Where did I go wrong?

    Best regards
    Habang

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Hello and welcome to Excel Forum

    I have a similar problem and from my experience (trial-and-error), it doesn't look like you can use JOIN in SQL queries through ADO?!

    My (temporary) solution is to rewrite my queries so they don't use the word 'JOIN'. For more background detail, see: http://www.excelforum.com/excel-prog...n-msdasql.html
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

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

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    There is no issue with Joins in ADO, it would be kind of pointless without them. What is the actual sql you are trying to use (exclude the VBA code since that looks ok), you seem to have commented out a lot of the sql (on the assumption that you are using MSSQL)

    I'm, struggling to understand what you're actually after - is it just the top 2 by ChangeNo? If so you don't need a join:
    PHP Code: 
    SELECT TOP 2 *
    FROM GG
    ORDER BY GG
    .ChangeNo Desc 
    Last edited by Kyle123; 11-23-2014 at 05:52 PM.

  4. #4
    Registered User
    Join Date
    01-16-2013
    Location
    Bodo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Hi, Kyle123- My older brother is a Leeds United fan :-)

    - I want the query to find the highest ChangeNo for each employee. In my example, I only included 2 employees, but in the real table, it would be about 400.

    mc84excel
    l - Thank you for your "foul" code ;-) It might be the solution, but I can't see how I can get the Max ChangeNo for every employe without importing it to Excel first.


    I might of course use som sort of loop to go through all ChangeNo's, but the best solution would be if I could get the Innerjoin to work...

    Still scratching my head, and I am woundering if OPENROWSET could be used to make this work.

    Best regards
    Habang.

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

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Ok got what you're after what error message are you getting with your code? Does it work in SSMS?

    Wrong shaped ball for me I'm afraid!
    Last edited by Kyle123; 11-23-2014 at 08:13 PM.

  6. #6
    Registered User
    Join Date
    01-16-2013
    Location
    Bodo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Found it!

    Somtimes it is so simple...

    Problem was a forgotten Space after the SS at the end of this line.

    wrong:' & "Inner join(select EmployeeNo, MAX(ChangeNo) ChangeNo from [--MyDatabase--].[--MySchema--].[--Table--] group by EmployeeNo) SS" _
    right: ' & "Inner join(select EmployeeNo, MAX(ChangeNo) ChangeNo from [--MyDatabase--].[--MySchema--].[--Table--] group by EmployeeNo) SS " _

    mc84excel - I'll look on the Join-issue on your post.
    Kyle123 - I once ended up in a pub with som Irish Rugby-fans before an International between England and Ireland. It wasn't a happy sunday morning...

    Best regards
    Habang

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

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Ah, always the little things! I use http://www.dpriver.com/pp/sqlformat.htm to convert to code. You can copy the sql from SSMS into the top box and select VB as the output and voila:
    PHP Code: 
    SELECT fld1fld2,fld3
    FROM G
    Inner join j tbl2 on tbl2
    .key g.key
    where
    tbl2
    .fld3 
    Becomes:
    PHP Code: 
    varname1 ""
    varname1 varname1 "SELECT fld1, fld2,fld3 " vbCrLf
    varname1 
    varname1 "FROM G " vbCrLf
    varname1 
    varname1 "Inner join j tbl2 on tbl2.key = g.key " vbCrLf
    varname1 
    varname1 "where " vbCrLf
    varname1 
    varname1 "tbl2.fld3 = 5" 
    Nifty eh?

    Haha I'll bet, lived with a couple of Irish lads ar Uni and they will support anyone against England!!

  8. #8
    Registered User
    Join Date
    01-16-2013
    Location
    Bodo, Norway
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Getting Inner Join to work in a VBA-based query to MS SQL

    Kyle123 - Very nifty :-) I tested the website, and it worked perfectly

+ 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. Query Help - ambiguous outer join
    By jammy1812 in forum Access Tables & Databases
    Replies: 3
    Last Post: 10-18-2012, 09:43 AM
  2. inner join query
    By masond3 in forum Excel General
    Replies: 0
    Last Post: 09-05-2012, 06:46 AM
  3. Table join query
    By moseleya in forum Access Tables & Databases
    Replies: 11
    Last Post: 06-05-2012, 03:46 PM
  4. Query Join / Union
    By ciprian in forum Access Tables & Databases
    Replies: 26
    Last Post: 09-14-2011, 01:36 PM
  5. [SOLVED] LEFT JOIN sql query
    By Michael Malinsky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2005, 04:05 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