MS Access Forum / Forms / May 2008
VB query code
|
|
Thread rating:  |
GuiGuy - 19 May 2008 21:36 GMT Can anyone tell me why the code listed does not run from a comand button when clicked? I have copied the query code and modified it so as to replace the name of a field when extracting the query.
Private Sub ExtractForecastPurchases_Click() Dim dbsCurrent As Database Dim qryTest As QueryDef Dim CreateSQL As String
CreateSQL = "SELECT tblHFIMR357PlannPurchasesExtract.[VENDOR], " _ & "tblHFIMR357PlannPurchasesExtract.[NAME], " _ & "tblHFIMR357PlannPurchasesExtract.[PART NUMBER], " _ & "tblHFIMR357PlannPurchasesExtract. tblHFIMR357Purchases_Description, " _ & "tblHFIMR357PlannPurchasesExtract.AMPR, tblHFIMR357PlannPurchasesExtract.[ORD-QTY], " _ & "tblHFIMR357PlannPurchasesExtract.ABC, tblHFIMR357PlannPurchasesExtract.SteelClass, " _ & "tblHFIMR357PlannPurchasesExtract.DFClass, tblHFIMR357PlannPurchasesExtract.Currency, " _ & "tblHFIMR357PlannPurchasesExtract.BYR, [ComMgrFName] & "" "" & [commgrLName] AS ComName, tblHFIMR357PlannPurchasesExtract.[M-GP], " _ & "tblHFIMR357PlannPurchasesExtract. tblMaterialGroup_Description, tblHFIMR357PlannPurchasesExtract.SMAT, IIf( [MultipleQte]>1,'YES' , '""') AS AVGQte, " _ & "tblHFIMR357PlannPurchasesExtract.Period01 AS " & [MO01] & "Qty, [Period01]*[SMAT] AS " & [MO01] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period02 AS " & [Mo02] & "Qty, [Period02]*[SMAT] AS " & [Mo02] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period03 AS " & [Mo03] & "Qty, [Period03]*[SMAT] AS " & [Mo03] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period04 AS " & [Mo04] & "Qty, [Period04]*[SMAT] AS " & [Mo04] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period05 AS " & [Mo05] & "Qty, [Period05]*[SMAT] AS " & [Mo05] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period06 AS " & [Mo06] & "Qty, [Period06]*[SMAT] AS " & [Mo06] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period07 AS " & [Mo07] & "Qty, [Period07]*[SMAT] AS " & [Mo07] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period08 AS " & [Mo08] & "Qty, [Period08]*[SMAT] AS " & [Mo08] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period09 AS " & [Mo09] & "Qty, [Period09]*[SMAT] AS " & [Mo09] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period10 AS " & [Mo10] & "Qty, [Period10]*[SMAT] AS " & [Mo10] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period11 AS " & [Mo11] & "Qty, [Period11]*[SMAT] AS " & [Mo11] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period12 AS " & [Mo12] & "Qty, [Period12]*[SMAT] AS " & [Mo12] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period13 AS " & [Mo13] & "Qty, [Period13]*[SMAT] AS " & [Mo13] & "Spend " _ & "FROM tblComMgr RIGHT JOIN tblHFIMR357PlannPurchasesExtract ON tblComMgr.ComMgrID = tblHFIMR357PlannPurchasesExtract.BYR;" Set dbsCurrent = CurrentDb Set qryTest = dbsCurrent.QueryDefs("qryHFIMR357Excel") qryTest.SQL = CreateSQL
End Sub
Thanks
Pete D. - 20 May 2008 04:07 GMT When you say doesn't run do you mean nothing happens or that it craps out in the middle. Have you run it in debug and seen which line it stops on?
> Can anyone tell me why the code listed does not run from a comand button > when [quoted text clipped - 74 lines] > > Thanks GuiGuy - 20 May 2008 14:09 GMT When the error comes up it asks if I want to debug or end. When I debug, the code states is highlighted in yellow and there is a yellow arrow to the left on the last line of this code.
tHANKS
Pete D. - 20 May 2008 16:33 GMT Hi, I found some errors and corrected them which was causing it to fall through to the last line. I have realigned it for easier reading and need you to compare to your tables now for incorrect field/table names. Watch for line wrapping after pasting back into the VBA window.
Option Compare Database Option Explicit
Private Sub ExtractForecastPurchases_Click() Dim dbsCurrent As Database Dim qryTest As QueryDef Dim CreateSQL01 As String Dim CreateSQL02 As String Dim CreateSQL03 As String Dim CreateSQL04 As String Dim CreateSQL_All As String
CreateSQL01 = "SELECT tblHFIMR357PlannPurchasesExtract.[VENDOR], " _ & "tblHFIMR357PlannPurchasesExtract.[NAME], " _ & "tblHFIMR357PlannPurchasesExtract.[PART NUMBER], " _ & "tblHFIMR357PlannPurchasesExtract." _ & "tblHFIMR357Purchases_Description , """ _ & "tblHFIMR357PlannPurchasesExtract.AMPR," _ & "tblHFIMR357PlannPurchasesExtract.[ORD-QTY] , """ _ & "tblHFIMR357PlannPurchasesExtract.ABC," CreateSQL02 = "tblHFIMR357PlannPurchasesExtract.SteelClass , """ _ & "tblHFIMR357PlannPurchasesExtract.DFClass," _ & "tblHFIMR357PlannPurchasesExtract.Currency , """ _ & "tblHFIMR357PlannPurchasesExtract.BYR, [ComMgrFName] & """" & [commgrLName] AS ComName," _ & "tblHFIMR357PlannPurchasesExtract.[M-GP], " _ & "tblHFIMR357PlannPurchasesExtract. tblMaterialGroup_Description," _ & "tblHFIMR357PlannPurchasesExtract.SMAT, IIf( [MultipleQte]1,'YES' , '""') AS AVGQte, " CreateSQL03 = "tblHFIMR357PlannPurchasesExtract.Period01 AS " & [MO01] & "Qty, [Period01]*[SMAT] AS " & [MO01] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period02 AS " & [Mo02] & "Qty, [Period02]*[SMAT] AS " & [Mo02] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period03 AS " & [Mo03] & "Qty, [Period03]*[SMAT] AS " & [Mo03] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period04 AS " & [Mo04] & "Qty, [Period04]*[SMAT] AS " & [Mo04] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period05 AS " & [Mo05] & "Qty, [Period05]*[SMAT] AS " & [Mo05] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period06 AS " & [Mo06] & "Qty, [Period06]*[SMAT] AS " & [Mo06] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period07 AS " & [Mo07] & "Qty, [Period07]*[SMAT] AS " & [Mo07] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period08 AS " & [Mo08] & "Qty, [Period08]*[SMAT] AS " & [Mo08] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period09 AS " & [Mo09] & "Qty, [Period09]*[SMAT] AS " & [Mo09] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period10 AS " & [Mo10] & "Qty, [Period10]*[SMAT] AS " & [Mo10] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period11 AS " & [Mo11] & "Qty, [Period11]*[SMAT] AS " & [Mo11] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period12 AS " & [Mo12] & "Qty, [Period12]*[SMAT] AS " & [Mo12] & "Spend, " _ & "tblHFIMR357PlannPurchasesExtract.Period13 AS " & [Mo13] & "Qty, [Period13]*[SMAT] AS " & [Mo13] & "Spend " CreateSQL04 = "FROM tblComMgr RIGHT JOIN tblHFIMR357PlannPurchasesExtract ON tblComMgr.ComMgrID = tblHFIMR357PlannPurchasesExtract.BYR;" CreateSQL_All = CreateSQL01 & CreateSQL02 & CreateSQL03 & CreateSQL04 Set dbsCurrent = CurrentDb Set qryTest = dbsCurrent.QueryDefs("qryHFIMR357Excel") qryTest.SQL = CreateSQL_All
End Sub
> When the error comes up it asks if I want to debug or end. When I debug, > the [quoted text clipped - 3 lines] > > tHANKS GuiGuy - 20 May 2008 14:09 GMT When the error comes up it asks if I want to debug or end. When I debug, the code states is highlighted in yellow and there is a yellow arrow to the left on the last line of this code.
Thanks
GuiGuy - 20 May 2008 14:31 GMT Let me clarify. The yellow arrow is on the line with the FROM statement.
>When the error comes up it asks if I want to debug or end. When I debug, the >code states is highlighted in yellow and there is a yellow arrow to the left >on the last line of this code. > >Thanks
|
|
|