|
-
Oct 1st, 2010, 09:24 PM
#1
Thread Starter
Lively Member
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
-
Oct 1st, 2010, 09:57 PM
#2
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?
-
Oct 1st, 2010, 09:58 PM
#3
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
-
Oct 2nd, 2010, 12:08 AM
#4
Thread Starter
Lively Member
Re: How to translate SQL language in Access to SQL in VBA, VB
 Originally Posted by baja_yu
Your question is better suited in the Database Development section. I'll notify Mods to consider moving it there.
Tks.
 Originally Posted by baja_yu
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.
-
Oct 2nd, 2010, 02:47 AM
#5
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%'
-
Oct 2nd, 2010, 04:20 AM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|