Results 1 to 6 of 6

Thread: How to translate SQL language in Access to SQL in VBA, VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    72

    How to translate SQL language in Access to SQL in VBA, VB

    Dear All,

    I have faced the problem that, I have test SQL in database MS.Access. It run correctly but when I use this SQL in VB (or VBA) the result was wrong.

    Code:
    (SELECT Data.Item, Data.Description, SUM(Data.ProQty) AS [ProQty], SUM(Data.SaleQty) AS [SaleQty], 0 AS [StockQty] 
                   FROM (SELECT TB_Movements_Docs_Keep.sMaNo AS [Item], TB_Movements_Docs_Keep.sMaDes AS [Description], 
                   TB_Movements_Docs_Keep.nQty AS [ProQty], 0 AS [SaleQty] 
                   FROM TB_Movements_Docs_Keep 
                   WHERE MONTH(TB_Movements_Docs_Keep.dPosDate)=9 And YEAR(TB_Movements_Docs_Keep.dPosDate)=2010 
                  AND (TB_Movements_Docs_Keep.nMovType=131  OR TB_Movements_Docs_Keep.nMovType=132) 
                   AND TB_Movements_Docs_Keep.sMaNo LIKE '22*' AND TB_Movements_Docs_Keep.sLoc='P001' 
                   ORDER BY TB_Movements_Docs_Keep.sMaNo 
                   UNION SELECT DISTINCT TB_Movements_Docs_Keep.sMaNo AS [Item], TB_Movements_Docs_Keep.sMaDes AS [Description], 
                   0 AS [ProQty], TB_Movements_Docs_Keep.nQty AS [SaleQty] 
                   FROM TB_Movements_Docs_Keep 
                   WHERE MONTH(TB_Movements_Docs_Keep.dPosDate)=9 And YEAR(TB_Movements_Docs_Keep.dPosDate)=2010
                  AND (TB_Movements_Docs_Keep.nMovType=601 OR TB_Movements_Docs_Keep.nMovType=602 
                   OR TB_Movements_Docs_Keep.nMovType=992 OR TB_Movements_Docs_Keep.nMovType=993) 
                   AND TB_Movements_Docs_Keep.sMaNo LIKE '22*' AND TB_Movements_Docs_Keep.sLoc='W001')  AS [Data] 
                   GROUP BY Data.Item, Data.Description) 
                   UNION (SELECT TB_Stock.sMaNo AS [Item], TB_Stock.sMaDes AS [Description], 0 AS [ProQty], 
                   0 AS [SaleQty], TB_Stock.nMaQty AS [StockQty] 
                   FROM TB_Stock 
                   WHERE TB_Stock.sLoc='W001' AND GoodsCondition=1);
    Anyone has experience about this problem.

    Thanks,

    LVD

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How to translate SQL language in Access to SQL in VBA, VB

    Your question is better suited in the Database Development section. I'll notify Mods to consider moving it there.

    Also, what do you mean by "the result was wrong" in VB? What results do you expect and what are the results you get?

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

    Re: How to translate SQL language in Access to SQL in VBA, VB

    you need to quote your strings, but not your variables or objects
    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    72

    Re: How to translate SQL language in Access to SQL in VBA, VB

    Quote Originally Posted by baja_yu View Post
    Your question is better suited in the Database Development section. I'll notify Mods to consider moving it there.
    Tks.

    Quote Originally Posted by baja_yu View Post
    Also, what do you mean by "the result was wrong" in VB? What results do you expect and what are the results you get?
    I mean that I took the SQL string in Access, then use it in the VBA to extract the data from Access. But the the result is difference as the pictures:
    1. Run SQL from Access


    2. Run SQL from VB


    Le Van Duyet
    Last edited by levanduyet; Oct 2nd, 2010 at 01:07 AM.

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

    Re: How to translate SQL language in Access to SQL in VBA, VB

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    The most likely thing to need to change is the wildcard for Like, rather than * try using % , eg: LIKE '22%'

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2009
    Posts
    72

    Re: How to translate SQL language in Access to SQL in VBA, VB

    Hi Si_the_geek,

    Thanks,

    LVD

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