Results 1 to 6 of 6

Thread: vba sql query

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    23

    vba sql query

    Hello I posted this in the db section of the forums with no help so I figured I'd try here... I'm writing some vba code that accesses a db. I keep receiving the error posted below. The query works fine with two tables and a where but when I try a join (eventually I will need to add a third table). The query is posted below thanks for the help in advance.

    Name:  Capture.PNG
Views: 313
Size:  5.0 KB

    Code:
    str = "SELECT CWIPH.CUST_NO, CCSMS.CUST_NO " _
        & "FROM CWIPH " _
        & "JOIN CCSMS ON CWIPH.CUST_NO=CCSMS.CUST_NO "
    JO

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: vba sql query

    Not sure where you're writing the code, but when I use Excel VBA, here's what I get:

    Code:
    strSQL = "SELECT * FROM tblNames INNER JOIN tblSals ON tblNames.name = tblSals.name"    'works
        strSQL = "SELECT * FROM tblNames JOIN tblSals ON tblNames.name = tblSals.name"          'doesn't work
    Have you tried using "INNER JOIN" instead?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    23

    Re: vba sql query

    Thanks the reply. I updated my query and am now receiving the error below

    Name:  Capture.PNG
Views: 217
Size:  8.4 KB

    Code:
    str = "SELECT CWIPH.CUST_NO, CCSMS.CUST_NO " _
        & "FROM (CWIPH INNER JOIN CCSMS ON CWIPH.CUST_NO = CCSMS.CUST_NO) "

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: vba sql query

    Haven't seen that before, but check this out:

    http://support.microsoft.com/kb/964644

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vba sql query

    does the database you are accessing support joins?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: vba sql query

    Have you tried the suggestion I made in your other thread?
    Quote Originally Posted by si_the_geek View Post
    OK, well try the really simplified version:
    Code:
      str = "SELECT CCSMS.CUST_NO " _
        & "FROM CSPCL, CCSMS " _
        & "WHERE CCSMS.CUST_NO = CSPCL.CUST_NO "

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width