Access VBA SQL Statement too long

Gregorys05

Board Regular
Joined
Sep 24, 2008
Messages
217
Hi,

I am trying to run an SQL statement in VBA but it keep saying there are too few parameters. Below is the VBA SQL

Code:
SQL1 = "PARAMETERS [Forms]![NBV date]![Chosen Date] Text ( 255 );"
SQL1 = SQL1 & " SELECT [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier],"
SQL1 = SQL1 & " tblSuppliercode.Group_Supplier, Count([all meters - operating leases].[Meter type 2]) AS [CountOfMeter type 21],"
SQL1 = SQL1 & " Sum([all meters - operating leases].[Current Rental]) AS [SumOfCurrent Rental1], Sum([all meters - operating leases].[Asset cost]) AS [SumOfAsset cost1],"
SQL1 = SQL1 & " Sum(IIf([all meters - operating leases]![Asset cost]-(((DateDiff(""m"",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *[all meters - operating leases]![Depn amount]))<=0,0,[all meters - operating leases]![Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount])))) AS NBV,"
SQL1 = SQL1 & " Sum([all meters - operating leases].[Asset cost])-(Sum(IIf([all meters - operating leases]![Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount]))<=0,0,"
SQL1 = SQL1 & " [all meters - operating leases]![Asset cost]-(((DateDiff(""m"",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *[all meters - operating leases]![Depn amount]))))) AS Depn"
SQL1 = SQL1 & " FROM [all meters - operating leases] INNER JOIN tblSuppliercode ON [all meters - operating leases].[Current Supplier] = tblSuppliercode.Supplier"
SQL1 = SQL1 & " GROUP BY [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier], tblSuppliercode.Group_Supplier;"

When i look at it in the immediate window using debug.print i get

Code:
PARAMETERS [Forms]![NBV date]![Chosen Date] Text ( 255 ); SELECT [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier], tblSuppliercode.Group_Supplier, Count([all meters - operating leases].[Meter type 2]) AS [CountOfMeter type 21], Sum([all meters - operating leases].[Current Rental]) AS [SumOfCurrent Rental1], Sum([all meters - operating leases].[Asset cost]) AS [SumOfAsset cost1], Sum(IIf([all meters - operating leases]![Asset cost]-(((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1) *[all meters - operating leases]![Depn amount]))<=0,0,[all meters - operating leases]![Asset cost]- (((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount])))) AS NBV, Sum([all meters - operating leases].[Asset cost])-(Sum(IIf([all meters - operating leases]![Asset cost]- (((DateDiff("m",[all meters - operating leases]![Depn start date],[Form
s]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount]))<=0,0, [all meters - operating leases]![Asset cost]-(((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1) *[all meters - operating leases]![Depn amount]))))) AS Depn FROM [all meters - operating leases] INNER JOIN tblSuppliercode ON [all meters - operating leases].[Current Supplier] = tblSuppliercode.Supplier GROUP BY [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier], tblSuppliercode.Group_Supplier;

there is a break in the line around

Code:
s]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount]))<=0,0,


if i remove the space and paste into an access query it runs but when i try and run the VBA i get the error.#


Any ideas on how to fix this


This is how it looks in access query

Code:
PARAMETERS [Forms]![NBV date]![Chosen Date] Text ( 255 ); SELECT [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier], tblSuppliercode.Group_Supplier, Count([all meters - operating leases].[Meter type 2]) AS [CountOfMeter type 21], Sum([all meters - operating leases].[Current Rental]) AS [SumOfCurrent Rental1], Sum([all meters - operating leases].[Asset cost]) AS [SumOfAsset cost1], Sum(IIf([all meters - operating leases]![Asset cost]-(((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1) *[all meters - operating leases]![Depn amount]))<=0,0,[all meters - operating leases]![Asset cost]- (((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount])))) AS NBV, Sum([all meters - operating leases].[Asset cost])-(Sum(IIf([all meters - operating leases]![Asset cost]- (((DateDiff("m",[all meters - operating leases]![Depn start date],[Form
s]![NBV date]![Chosen Date])+1)*[all meters - operating leases]![Depn amount]))<=0,0, [all meters - operating leases]![Asset cost]-(((DateDiff("m",[all meters - operating leases]![Depn start date],[Forms]![NBV date]![Chosen Date])+1) *[all meters - operating leases]![Depn amount]))))) AS Depn FROM [all meters - operating leases] INNER JOIN tblSuppliercode ON [all meters - operating leases].[Current Supplier] = tblSuppliercode.Supplier GROUP BY [all meters - operating leases].[Lease type], [all meters - operating leases].[Current Supplier], tblSuppliercode.Group_Supplier;


Thank you
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can reduce the length of the SQL by aliasing the table names, and at some points you used table!field instead of table.field notation.

This SQL statement reduces the original code from 1833 to 1205 characters:
Code:
SQL1 = "PARAMETERS [Forms]![NBV date]![Chosen Date] Text ( 255 );"
SQL1 = SQL1 & " SELECT al.[Lease type], al.[Current Supplier],"
SQL1 = SQL1 & " sc.Group_Supplier, Count(al.[Meter type 2]) AS [CountOfMeter type 21],"
SQL1 = SQL1 & " Sum(al.[Current Rental]) AS [SumOfCurrent Rental1], Sum(al.[Asset cost]) AS [SumOfAsset cost1],"
SQL1 = SQL1 & " Sum(IIf(al.[Asset cost]-(((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *al.[Depn amount]))<=0,0,al.[Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)*al.[Depn amount])))) AS NBV,"
SQL1 = SQL1 & " Sum(al.[Asset cost])-(Sum(IIf(al.[Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)*al.[Depn amount]))<=0,0,"
SQL1 = SQL1 & " al.[Asset cost]-(((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *al.[Depn amount]))))) AS Depn"
SQL1 = SQL1 & " FROM [all meters - operating leases] AS al INNER JOIN tblSuppliercode AS sc ON al.[Current Supplier] = sc.Supplier"
SQL1 = SQL1 & " GROUP BY al.[Lease type], al.[Current Supplier], sc.Group_Supplier;"

EDIT just noticed... Why is the parameter Text when it contains a Date?

Denis
 
Last edited:
Upvote 0
thank you for this, i have now changed the code to the below:

But i now get
runtime error 3061
Too few parameters. Expected 1

But when i run the query in access it works fine. :confused:


Code:
SQL1 = " SELECT al.[Lease type], al.[Current Supplier],"
SQL1 = SQL1 & " sc.Group_Supplier, Count(al.[Meter type 2]) AS [CountOfMeter type 21],"
SQL1 = SQL1 & " Sum(al.[Current Rental]) AS [SumOfCurrent Rental1], Sum(al.[Asset cost]) AS [SumOfAsset cost1],"
SQL1 = SQL1 & " Sum(IIf(al.[Asset cost]-(((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *al.[Depn amount]))<=0,0,al.[Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)*al.[Depn amount])))) AS NBV,"
SQL1 = SQL1 & " Sum(al.[Asset cost])-(Sum(IIf(al.[Asset cost]-"
SQL1 = SQL1 & " (((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)*al.[Depn amount]))<=0,0,"
SQL1 = SQL1 & " al.[Asset cost]-(((DateDiff(""m"",al.[Depn start date],[Forms]![NBV date]![Chosen Date])+1)"
SQL1 = SQL1 & " *al.[Depn amount]))))) AS Depn"
SQL1 = SQL1 & " FROM [all meters - operating leases] AS al INNER JOIN tblSuppliercode AS sc ON al.[Current Supplier] = sc.Supplier"
SQL1 = SQL1 & " GROUP BY al.[Lease type], al.[Current Supplier], sc.Group_Supplier;"
 
Upvote 0
> "Too few parameters. Expected 1"

this happens when you reference a control that is not filled out. Wrap ALL control references with NZ, ie:

nz([Forms]![NBV date]![Chosen Date])
 
Upvote 0
instead of this:

sSQL = "blah blah"
sSQL = sSQL & "blah blah"

do this:

Code:
sSQL = "blah blah" _
      & "blah blah"

space underscore at the end of the line means the statement is continued

your names are waaay too long! Your fieldnames seem like they should be the field DESCRIPTIONS. Fieldnames should be short -- at least not any longer than the data they contain

also, you are making a LOT of references to controls on forms ... what is the query being used for?
 
Upvote 0
I also just noticed this:

PARAMETERS [Forms]![NBV date]![Chosen Date] Text ( 255 )

shouldn't this be a Date/Time? Later in the query you add 1 ... cannot do that with text, it will just put a "1" on the end ...
 
Upvote 0
ps, I would like to comment on your structure ... am a petroleum engineer ... assume this is O&G data from your names
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top