how to pass parameters from forms to stored procedures

G

Guest

Currently i am working in a project of report generation in MS ACCESS.

The tables are in sql server 2000.
I have to write stored proc in ms access.

Illustration:
I am having a stored proc as follows

name: myproc
-------------------
Create procedure my_proc
@f1 char(1),
@f2 char(5)
As
select * from table1 where field1=@f1 and field2=@f2
________________________________________________
and calling proc
name: call_myproc

execute my_proc 'A','2004'

If I wrote the calling proc as above. It works fine.

If i am getting the vales of 'a' and '2004' from forms in ms access.(combo
box and text box) How I have to write the stored proc as follows.

I wrote the calling stored proc as follows.

my_proc [forms]![form_a].[Combo4],[forms]![form_a].[text12]

But ms access throws syntax error.

How is it possible to pass values from ms access FORMS to a calling stored
procedure.

could you please help me to fix this problem ?
___________________________________________________________________

Krishnakanth VM
Anjana Software Solutions
Chennai
Tamil Nadu
INDIA
 
D

Douglas J. Steele

You have to dynamically change the SQL in your pass-through query.

Dim qdfCurr As DAO.QueryDef

qdfCurr = CurrentDb().QueryDefs("MyQuery")
qdfCurr.SQL = "my_proc '" & _
[forms]![form_a].[Combo4] & "', '" & _
[forms]![form_a].[text12] & "'"
 
G

Guest

Douglas,

Thank you very much.

I copied and paste the following from your text in SQL Pass Through query

Dim qdfCurr As DAO.QueryDef

qdfCurr = CurrentDb().QueryDefs("MyQuery")
qdfCurr.SQL = "my_proc '" & _
[forms]![form_a].[Combo4] & "', '" & _
[forms]![form_a].[text12] & "'"

When I try executing I got the following error message.
---------------------
ODBC-call failed.
[Microsoft][ODBC SQL Server Driver][SQL Server] Incorrect syntax near the
keyword 'As' (#156)
---------------------


Douglas J. Steele said:
You have to dynamically change the SQL in your pass-through query.

Dim qdfCurr As DAO.QueryDef

qdfCurr = CurrentDb().QueryDefs("MyQuery")
qdfCurr.SQL = "my_proc '" & _
[forms]![form_a].[Combo4] & "', '" & _
[forms]![form_a].[text12] & "'"


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Currently i am working in a project of report generation in MS ACCESS.

The tables are in sql server 2000.
I have to write stored proc in ms access.

Illustration:
I am having a stored proc as follows

name: myproc
-------------------
Create procedure my_proc
@f1 char(1),
@f2 char(5)
As
select * from table1 where field1=@f1 and field2=@f2
________________________________________________
and calling proc
name: call_myproc

execute my_proc 'A','2004'

If I wrote the calling proc as above. It works fine.

If i am getting the vales of 'a' and '2004' from forms in ms access.(combo
box and text box) How I have to write the stored proc as follows.

I wrote the calling stored proc as follows.

my_proc [forms]![form_a].[Combo4],[forms]![form_a].[text12]

But ms access throws syntax error.

How is it possible to pass values from ms access FORMS to a calling stored
procedure.

could you please help me to fix this problem ?
___________________________________________________________________

Krishnakanth VM
Anjana Software Solutions
Chennai
Tamil Nadu
INDIA
 
D

Douglas J. Steele

Go into your query after you've run the code, and look at the SQL that's
there. Does it look correct?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Douglas,

Thank you very much.

I copied and paste the following from your text in SQL Pass Through query

Dim qdfCurr As DAO.QueryDef

qdfCurr = CurrentDb().QueryDefs("MyQuery")
qdfCurr.SQL = "my_proc '" & _
[forms]![form_a].[Combo4] & "', '" & _
[forms]![form_a].[text12] & "'"

When I try executing I got the following error message.
---------------------
ODBC-call failed.
[Microsoft][ODBC SQL Server Driver][SQL Server] Incorrect syntax near the
keyword 'As' (#156)
---------------------


Douglas J. Steele said:
You have to dynamically change the SQL in your pass-through query.

Dim qdfCurr As DAO.QueryDef

qdfCurr = CurrentDb().QueryDefs("MyQuery")
qdfCurr.SQL = "my_proc '" & _
[forms]![form_a].[Combo4] & "', '" & _
[forms]![form_a].[text12] & "'"


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Currently i am working in a project of report generation in MS ACCESS.

The tables are in sql server 2000.
I have to write stored proc in ms access.

Illustration:
I am having a stored proc as follows

name: myproc
-------------------
Create procedure my_proc
@f1 char(1),
@f2 char(5)
As
select * from table1 where field1=@f1 and field2=@f2
________________________________________________
and calling proc
name: call_myproc

execute my_proc 'A','2004'

If I wrote the calling proc as above. It works fine.

If i am getting the vales of 'a' and '2004' from forms in ms
access.(combo
box and text box) How I have to write the stored proc as follows.

I wrote the calling stored proc as follows.

my_proc [forms]![form_a].[Combo4],[forms]![form_a].[text12]

But ms access throws syntax error.

How is it possible to pass values from ms access FORMS to a calling
stored
procedure.

could you please help me to fix this problem ?
___________________________________________________________________

Krishnakanth VM
Anjana Software Solutions
Chennai
Tamil Nadu
INDIA
 
G

Guest

It works fine. (I did not get any errors so far).
Let me explain in detail.

In MS ACCESS

I am having a form.
It consists of 1 combobox. “Department†consists of values (ECE,EEE,CSE).
The user have to choose any one of the department and he has to click “Run
Report†Button.

There is a table called register consists of the fields reg_no, stud_name,
mark1, mark2 and mark3

Under the query I am created a stored procedure.

I have given the calling stored procedure in the VB coding (as per your
suggestion)

So far no errors.

Now I have to create a new report (report1). When I try to create a new
report it asks me to select the query/table. Now what should I have to do?

Note:
stored procedure.
--------------------
query name: query1
++++++++++++++++++++
create procedure str_reg
@dept char(5)
as
select * from dbo.register where Department=@dept
++++++++++++++++++++
expected output.

when the user choose the department (ECE/EEE/CSE) and when he click run
report.
I need a report in printpreview layout consists of reg_no, stud_name, mark1,
mark2, mark3 (all values of chosen field "Department")

please tell me how to proceed furthur ?

Krishnakanth VM
INDIA

=====================================================
 
G

Guest

I am generating reports in ms access by means stored procedure. (tables and
stored procedures are in sql server 2000). The problem what i am facing is
sorting. when i execute the calling stored procedure in sql server 2000 query
analyser, i am getting the results in the preferred order. where as i am not
getting the same order in ms access report. i don't know why?.

while generating a report, the report will call the "calling stored
procedure" (eg. exec str_proc_a ....list of values). That calling stored
procedure will get the output from sql server 2000 and send it to reports.

I have also tried "sorting and grouping" in ms access reports. that will not
work.

when i execute the same procedure in query analyser, i am getting the
expected result, but i am getting in report was entirely different.

can anyone please help me to fix this problem???
 
R

Rick Brandt

Krishnakanth said:
I am generating reports in ms access by means stored procedure. (tables and
stored procedures are in sql server 2000). The problem what i am facing is
sorting. when i execute the calling stored procedure in sql server 2000 query
analyser, i am getting the results in the preferred order. where as i am not
getting the same order in ms access report. i don't know why?.

Report in Access (for the most part) do not care about the sorting of the
underlying query. Use the Sorting and Grouping dialog in the report's design to
specify the sorting you want.
while generating a report, the report will call the "calling stored
procedure" (eg. exec str_proc_a ....list of values). That calling stored
procedure will get the output from sql server 2000 and send it to reports.

I have also tried "sorting and grouping" in ms access reports. that will not
work.

What does "not work" mean? Access is incapable of ignoring those settings.
when i execute the same procedure in query analyser, i am getting the
expected result, but i am getting in report was entirely different.

can anyone please help me to fix this problem???

As stated, the report does not care how the query is sorted. You MUST use the
report's sorting dialog.
 
G

Guest

Thank you very much for your reply.
I need to sort a field as follows (not in ascending or descending)
There are around 17000 rows in the table. All of the row consists of any one
of these values.


field1: budgetcategory consists of values (ECP,BASE,CAP)
field 2: LOB consists of values (A,B,F)
field 3: ProjectType consists of values
("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
....

The preferred order of sorting would be as follows
(1) ECP
(a) A - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(b) B - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(c) C - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(2) CAP
(3) BASE
similarly for CAP and BASE

could you plz. help me how to achieve this ms access reports
 
G

Guest

could you plz. give me your contact number. so that i will explain in detail.
also tell me which is the best time to call you
 
R

Rick Brandt

Krishnakanth said:
Thank you very much for your reply.
I need to sort a field as follows (not in ascending or descending)
There are around 17000 rows in the table. All of the row consists of any one
of these values.


field1: budgetcategory consists of values (ECP,BASE,CAP)
field 2: LOB consists of values (A,B,F)
field 3: ProjectType consists of values
("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
...

The preferred order of sorting would be as follows
(1) ECP
(a) A - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(b) B - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(c) C - ("PROJ","IDF","CLNUP","REBL","R&R","RTDP","CAP","PST")
(2) CAP
(3) BASE
similarly for CAP and BASE

could you plz. help me how to achieve this ms access reports

You need some kind of formula or a join to another table such that you end up
with a field in your query that your report can sort on in either ascending or
descending order. For example you could have in your stored procedure two Case
statements...

CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

Then you can sort on SortVal1 and SortVal2 in your report.
 
G

Guest

CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while writing
procedure. Because I have already mentioned that tables are in sql server. I
have also executed the stored procedure in sql server. For your convinience I
have given procedure as follows. Its a quite big procedure.
------
Create Procedure both_str_proc_v12
@relyr char(4),
@relno char(2),
@dotno char(1),
@budget57 char(6),
@budget58 char(6),
@budget59 char(6),
@region78 char(15),
@region80 char(15),
@region82 char(15),
@region84 char(15),
@status98 char(1),
@status100 char(1)
as
BEGIN
SELECT dbo.RSM_Project.LOB, dbo.RSM_Project.ProjScope,
dbo.RSM_Project.RelYear, dbo.RSM_Project.ProjectType, dbo.RSM_Project.RelNum,
dbo.RSM_Project.DotNum, dbo.RSM_Project.BudgetCategory,
dbo.RSM_Project.ProjSeqNum, dbo.RSM_Project.ProjectId,
dbo.RSM_Project.ProjectTitle,
PCF=(dbo.RSM_Project.BizPCFImpact+'/'+dbo.RSM_Project.ITPCFImpact),
APPS = SUM(APPSSME+APPSDev+APPSSMEICF+APPSDevICF),
AutoBatch=SUM(AutobatchSME+AutobatchDev+AutobatchSMEICF+AutobatchDevICF),
AutoEdge=SUM(AutoEDGESME+AutoEDGEDev+AutoEDGESMEICF+AutoEDGEDevICF),
FireBatch =SUM(FirebatchSME+FirebatchDev+FirebatchSMEICF+FirebatchDevICF),
eAuto=SUM(eAutoSME+eAutoDev+eAutoSMEICF+eAutoDevICF),
eHome =SUM(eHomeSME+eHomeDev+eHomeSMEICF+eHomeDevICF),
FPPS=SUM(FPPSSME+FPPSDev+FPPSSMEICF+FPPSDevICF+CDJSME+CDJDev+CDJSMEICF+CDJDevICF+FireNetworkSME+FireNetworkDev+FireNetworkSMEICF+FireNetworkDevICF),
FireEdge=SUM(FireEDGESME+FireEDGEDev+FireEDGESMEICF+FireEDGEDevICF),
Tables =SUM(TablesSME+TablesDev+TablesSMEICF+TablesDevICF),
Total= SUM(APPSSME+APPSDev+APPSSMEICF+APPSDevICF+
AutobatchSME+AutobatchDev+AutobatchSMEICF+AutobatchDevICF+
AutoEDGESME+AutoEDGEDev+AutoEDGESMEICF+AutoEDGEDevICF+
FirebatchSME+FirebatchDev+FirebatchSMEICF+FirebatchDevICF+
eAutoSME+eAutoDev+eAutoSMEICF+eAutoDevICF+
FPPSSME+FPPSDev+FPPSSMEICF+FPPSDevICF+CDJSME+CDJDev+CDJSMEICF+CDJDevICF+FireNetworkSME+FireNetworkDev+FireNetworkSMEICF+FireNetworkDevICF+
eHomeSME+eHomeDev+eHomeSMEICF+eHomeDevICF+
FireEDGESME+FireEDGEDev+FireEDGESMEICF+FireEDGEDevICF+
TablesSME+TablesDev+TablesSMEICF+TablesDevICF),
dbo.RSM_Project.Emergency, dbo.RSM_Project.ProjCategory,
dbo.RSM_Project.BRISubmittedBy, dbo.RSM_Project.ITSME,
CEA =SUM(CEASME+CEADEV+CEASMEICF+CEADEVICF),
Region=dbo.RSM_ProjectState.Region+space(10),
dbo.RSM_Project.ProjStatus
INTO #RptResult
FROM dbo.RSM_Project, dbo.RSM_ProjectROM, dbo.RSM_ProjectState
WHERE dbo.RSM_Project.ProjSeqNum = dbo.RSM_ProjectROM.ProjSeqNum AND
dbo.RSM_Project.ProjSeqNum *= dbo.RSM_ProjectState.ProjSeqNum AND
(((dbo.RSM_Project.RelYear)= @relyr) AND
((dbo.RSM_Project.RelNum)= @relno) AND
((dbo.RSM_Project.DotNum)= @dotno) AND
((dbo.RSM_Project.BudgetCategory)= @budget57 Or
(dbo.RSM_Project.BudgetCategory)= @budget58 Or
(dbo.RSM_Project.BudgetCategory)= @budget59) AND
((dbo.RSM_ProjectState.Region)= @region78 Or
(dbo.RSM_ProjectState.Region)= @region80 Or
(dbo.RSM_ProjectState.Region)= @region82 Or
(dbo.RSM_ProjectState.Region)= @region84) AND
((dbo.RSM_Project.ProjStatus)= @status98 Or
(dbo.RSM_Project.ProjStatus)= @status100) AND
((dbo.RSM_Project.BudgetCategory) in ('BASE','ECP','CAP')) AND
((dbo.RSM_Project.Projecttype) in
('RTDP','R&R','CLNUP','REBL','PROJ','IDF','CAP','PST')))
GROUP BY dbo.RSM_Project.LOB,
dbo.RSM_Project.ProjScope,
dbo.RSM_Project.RelYear,
dbo.RSM_Project.ProjectType,
dbo.RSM_Project.RelNum,
dbo.RSM_Project.DotNum,
dbo.RSM_Project.BudgetCategory,
dbo.RSM_Project.ProjSeqNum,
dbo.RSM_Project.ProjectId,
dbo.RSM_Project.ProjectTitle,
dbo.RSM_Project.BizPCFImpact,
dbo.RSM_Project.ITPCFImpact,
dbo.RSM_Project.Emergency,
dbo.RSM_Project.ProjCategory,
dbo.RSM_Project.BRISubmittedBy,
dbo.RSM_Project.ITSME,
dbo.RSM_ProjectState.Region,
dbo.RSM_Project.ProjStatus
ORDER BY (dbo.RSM_Project.BudgetCategory) DESC,
case
when LOB = 'A' then 1
when LOB = 'B' then 2
when LOB = 'F' then 3
end,
case
when Projecttype = 'PROJ' then 1
when Projecttype = 'IDF' then 2
when Projecttype = 'CLNUP' then 3
when Projecttype = 'REBL' then 4
when Projecttype = 'R&R' then 5
when Projecttype = 'RTDP' then 6
when Projecttype = 'CAP' then 7
when Projecttype = 'PST' then 8
end

DECLARE @ResultCnt int, @ResCounter int, @ProjSeqNum int, @RegionCnt int,
@RegionCounter int, @AllRegions varchar(30), @Region varchar(10)

Select * into #temp from #RptResult
Select @ResultCnt = count(*) from #temp
Set Rowcount 1
Select @ResCounter = 0
WHILE @ResCounter <= @ResultCnt
BEGIN
Select @ProjSeqNum = ProjSeqNum FROM #temp
set rowcount 0
IF (Select Count(*) FROM RSM_ProjectState WHERE ProjSeqNum = @ProjSeqNum
and Region is Not Null) > 0
BEGIN
Select Region into #region from RSM_ProjectState WHERE ProjSeqNum =
@ProjSeqNum and Region is Not Null
Select @RegionCnt = Count(*) FROM #region
set rowcount 1
Select @RegionCounter = 1, @AllREgions = ''
IF @RegionCnt = 1 ---- Handles ONE region ----------------
BEGIN
Select @AllRegions = Region from #region
END
ELSE
BEGIN ------ Handles Multiple Regions ------------------
While @RegionCounter <= @RegionCnt
BEGIN
Select @Region = ltrim(rtrim(Region)) FROM #region
IF IsNull(charindex(@Region,@AllRegions),'0') = 0
BEGIN
IF @RegionCounter = 1
BEGIN SELECT @AllRegions = @Region END
ELSE
BEGIN Select @AllRegions = IsNull(@AllRegions,'') +
',' + @Region END
END
SELECT @RegionCounter = @RegionCOunter + 1
Delete from #region
END
UPDATE #RptResult
Set Region = @AllRegions
Where ProjseqNum = @ProjSeqNum
END
Drop Table #region
END
Set Rowcount 1
SELECT @ResCounter = @ResCounter + 1
Delete from #temp
END

Set rowcount 0
SELECT * from #RptResult

--Drop table #temp, #RptResult
END
GO
--------
when I execute the above query in sql server, I have got the expected
output. But I am not getting in reports. You have mentioned that sorting can
be done using case when. Where should I have to give the case when in ms
access reports.

Thanks for your help in advance.



===================================================
 
R

Rick Brandt

Krishnakanth said:
CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while
writing procedure. [snip]

You need those calculated fields in your output so that you can THEN sort on
them in the Report using the Grouping and Sorting dialog. Sorting in the
Stored Procedure is useless and probably just makes the report slower.
 
G

Guest

Yes Rick. We do have a field in the report called "ProjectType". But the
option called "Sorting and Grouping" in Reports have Ascending or Descending.
Let me make clear. You are telling like "case when" option in sql query is
useless.
How will i sort the field "ProjectType" in reports.

Could you please explain elaborately. I am new to MS Access Reports.
Thanks for your help in advance.


Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
========================================

Rick Brandt said:
Krishnakanth said:
CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while
writing procedure. [snip]

You need those calculated fields in your output so that you can THEN sort on
them in the Report using the Grouping and Sorting dialog. Sorting in the
Stored Procedure is useless and probably just makes the report slower.
 
D

Douglas J. Steele

I don't think Rick was telling you that "case when" is useless. The issue is
that you cannot sort on the resultant values in your query, and expect that
sort order to carry over to the report. You must do the sorting using the
report's Grouping and Sorting dialog.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Yes Rick. We do have a field in the report called "ProjectType". But the
option called "Sorting and Grouping" in Reports have Ascending or
Descending.
Let me make clear. You are telling like "case when" option in sql query is
useless.
How will i sort the field "ProjectType" in reports.

Could you please explain elaborately. I am new to MS Access Reports.
Thanks for your help in advance.


Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
========================================

Rick Brandt said:
Krishnakanth said:
CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while
writing procedure. [snip]

You need those calculated fields in your output so that you can THEN sort
on
them in the Report using the Grouping and Sorting dialog. Sorting in the
Stored Procedure is useless and probably just makes the report slower.
 
G

Guest

Thanks for your reply. Using grouping and sorting we can sort either in
Ascending or in Descending order. But i want to sort the field "ProjectType"
in the order as follows.

1 'PROJ'
2 'IDF'
3 'CLNUP'
4 'REBL'
5 'R&R'
6 'RTDP'
7 'CAP'
8 'PST'

Could you please help me how this can be done ?

Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
===================================================

Douglas J. Steele said:
I don't think Rick was telling you that "case when" is useless. The issue is
that you cannot sort on the resultant values in your query, and expect that
sort order to carry over to the report. You must do the sorting using the
report's Grouping and Sorting dialog.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Yes Rick. We do have a field in the report called "ProjectType". But the
option called "Sorting and Grouping" in Reports have Ascending or
Descending.
Let me make clear. You are telling like "case when" option in sql query is
useless.
How will i sort the field "ProjectType" in reports.

Could you please explain elaborately. I am new to MS Access Reports.
Thanks for your help in advance.


Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
========================================

Rick Brandt said:
Krishnakanth wrote:
CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while
writing procedure. [snip]

You need those calculated fields in your output so that you can THEN sort
on
them in the Report using the Grouping and Sorting dialog. Sorting in the
Stored Procedure is useless and probably just makes the report slower.
 
D

Douglas J. Steele

Your Case statements are creating computed fields SortVal1 and SortVal2 in
your query. Use those fields in the Sorting and Grouping dialog. Remember
that you can sort (and group) on those fields, but still display the actual
text for BudgetCategory or ProjectType in your report.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Thanks for your reply. Using grouping and sorting we can sort either in
Ascending or in Descending order. But i want to sort the field
"ProjectType"
in the order as follows.

1 'PROJ'
2 'IDF'
3 'CLNUP'
4 'REBL'
5 'R&R'
6 'RTDP'
7 'CAP'
8 'PST'

Could you please help me how this can be done ?

Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
===================================================

Douglas J. Steele said:
I don't think Rick was telling you that "case when" is useless. The issue
is
that you cannot sort on the resultant values in your query, and expect
that
sort order to carry over to the report. You must do the sorting using the
report's Grouping and Sorting dialog.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Krishnakanth said:
Yes Rick. We do have a field in the report called "ProjectType". But
the
option called "Sorting and Grouping" in Reports have Ascending or
Descending.
Let me make clear. You are telling like "case when" option in sql query
is
useless.
How will i sort the field "ProjectType" in reports.

Could you please explain elaborately. I am new to MS Access Reports.
Thanks for your help in advance.


Krishnakanth VM
(e-mail address removed)
(e-mail address removed)
========================================

:

Krishnakanth wrote:
CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

The sorting that you have mentioned is given the sql query while
writing procedure. [snip]

You need those calculated fields in your output so that you can THEN
sort
on
them in the Report using the Grouping and Sorting dialog. Sorting in
the
Stored Procedure is useless and probably just makes the report slower.
 
G

Guest

when I modify the query as follows.

CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

I am getting the error message as follows.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 88
Incorrect syntax near the keyword 'as'.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 150
Incorrect syntax near the keyword 'END'.

What can be done to fix this problem ?
Please help us.

Krishnakanth VM
 
J

John W. Vinson

when I modify the query as follows.

CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

I am getting the error message as follows.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 88
Incorrect syntax near the keyword 'as'.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 150
Incorrect syntax near the keyword 'END'.

Is this in Microsoft Access, or in SQL/Server? Access does not support
the CASE syntax, which is probably why you're getting the error.

Try using the Switch() function instead:
SELECT Switch([budgetcategory] = "ECP", 1, [budgetcategory] = "CAP",
2, True, 3) AS SortVal1,
Switch([ProjectType] = "PROJ", 1,
[ProjectType] = "IDF", 2,
[ProjectType] = "CLNUP", 3,
[ProjectType] = "REBL", 4,
[ProjectType] = "R&R", 5,
[ProjectType] = "RTDP", 6,
[ProjectType] = "CAP", 7,
True, 8) AS SortVal2


John W. Vinson [MVP]
 
G

Guest

I don't how to modify it in the below query.

Could you plz. modify it in the below query.
----------------------------------------------
Create Procedure both_str_proc_v12
@relyr char(4),
@relno char(2),
@dotno char(1),
@budget57 char(6),
@budget58 char(6),
@budget59 char(6),
@region78 char(15),
@region80 char(15),
@region82 char(15),
@region84 char(15),
@status98 char(1),
@status100 char(1)
as
BEGIN
SELECT dbo.RSM_Project.LOB, dbo.RSM_Project.ProjScope,
dbo.RSM_Project.RelYear, dbo.RSM_Project.ProjectType, dbo.RSM_Project.RelNum,
dbo.RSM_Project.DotNum, dbo.RSM_Project.BudgetCategory,
dbo.RSM_Project.ProjSeqNum, dbo.RSM_Project.ProjectId,
dbo.RSM_Project.ProjectTitle,
PCF=(dbo.RSM_Project.BizPCFImpact+'/'+dbo.RSM_Project.ITPCFImpact),
APPS = SUM(APPSSME+APPSDev+APPSSMEICF+APPSDevICF),
AutoBatch=SUM(AutobatchSME+AutobatchDev+AutobatchSMEICF+AutobatchDevICF),
AutoEdge=SUM(AutoEDGESME+AutoEDGEDev+AutoEDGESMEICF+AutoEDGEDevICF),
FireBatch =SUM(FirebatchSME+FirebatchDev+FirebatchSMEICF+FirebatchDevICF),
eAuto=SUM(eAutoSME+eAutoDev+eAutoSMEICF+eAutoDevICF),
eHome =SUM(eHomeSME+eHomeDev+eHomeSMEICF+eHomeDevICF),
FPPS=SUM(FPPSSME+FPPSDev+FPPSSMEICF+FPPSDevICF+CDJSME+CDJDev+CDJSMEICF+CDJDevICF+FireNetworkSME+FireNetworkDev+FireNetworkSMEICF+FireNetworkDevICF),
FireEdge=SUM(FireEDGESME+FireEDGEDev+FireEDGESMEICF+FireEDGEDevICF),
Tables =SUM(TablesSME+TablesDev+TablesSMEICF+TablesDevICF),
Total= SUM(APPSSME+APPSDev+APPSSMEICF+APPSDevICF+
AutobatchSME+AutobatchDev+AutobatchSMEICF+AutobatchDevICF+
AutoEDGESME+AutoEDGEDev+AutoEDGESMEICF+AutoEDGEDevICF+
FirebatchSME+FirebatchDev+FirebatchSMEICF+FirebatchDevICF+
eAutoSME+eAutoDev+eAutoSMEICF+eAutoDevICF+
FPPSSME+FPPSDev+FPPSSMEICF+FPPSDevICF+CDJSME+CDJDev+CDJSMEICF+CDJDevICF+FireNetworkSME+FireNetworkDev+FireNetworkSMEICF+FireNetworkDevICF+
eHomeSME+eHomeDev+eHomeSMEICF+eHomeDevICF+
FireEDGESME+FireEDGEDev+FireEDGESMEICF+FireEDGEDevICF+
TablesSME+TablesDev+TablesSMEICF+TablesDevICF),
dbo.RSM_Project.Emergency, dbo.RSM_Project.ProjCategory,
dbo.RSM_Project.BRISubmittedBy, dbo.RSM_Project.ITSME,
CEA =SUM(CEASME+CEADEV+CEASMEICF+CEADEVICF),
Region=dbo.RSM_ProjectState.Region+space(10),
dbo.RSM_Project.ProjStatus
INTO #RptResult
FROM dbo.RSM_Project, dbo.RSM_ProjectROM, dbo.RSM_ProjectState
WHERE dbo.RSM_Project.ProjSeqNum = dbo.RSM_ProjectROM.ProjSeqNum AND
dbo.RSM_Project.ProjSeqNum *= dbo.RSM_ProjectState.ProjSeqNum AND
(((dbo.RSM_Project.RelYear)= @relyr) AND
((dbo.RSM_Project.RelNum)= @relno) AND
((dbo.RSM_Project.DotNum)= @dotno) AND
((dbo.RSM_Project.BudgetCategory)= @budget57 Or
(dbo.RSM_Project.BudgetCategory)= @budget58 Or
(dbo.RSM_Project.BudgetCategory)= @budget59) AND
((dbo.RSM_ProjectState.Region)= @region78 Or
(dbo.RSM_ProjectState.Region)= @region80 Or
(dbo.RSM_ProjectState.Region)= @region82 Or
(dbo.RSM_ProjectState.Region)= @region84) AND
((dbo.RSM_Project.ProjStatus)= @status98 Or
(dbo.RSM_Project.ProjStatus)= @status100) AND
((dbo.RSM_Project.BudgetCategory) in ('BASE','ECP','CAP')) AND
((dbo.RSM_Project.Projecttype) in
('RTDP','R&R','CLNUP','REBL','PROJ','IDF','CAP','PST')))
GROUP BY dbo.RSM_Project.LOB,
dbo.RSM_Project.ProjScope,
dbo.RSM_Project.RelYear,
dbo.RSM_Project.ProjectType,
dbo.RSM_Project.RelNum,
dbo.RSM_Project.DotNum,
dbo.RSM_Project.BudgetCategory,
dbo.RSM_Project.ProjSeqNum,
dbo.RSM_Project.ProjectId,
dbo.RSM_Project.ProjectTitle,
dbo.RSM_Project.BizPCFImpact,
dbo.RSM_Project.ITPCFImpact,
dbo.RSM_Project.Emergency,
dbo.RSM_Project.ProjCategory,
dbo.RSM_Project.BRISubmittedBy,
dbo.RSM_Project.ITSME,
dbo.RSM_ProjectState.Region,
dbo.RSM_Project.ProjStatus
ORDER BY (dbo.RSM_Project.BudgetCategory) DESC,
case
when LOB = 'A' then 1
when LOB = 'B' then 2
when LOB = 'F' then 3
end,
case
when Projecttype = 'PROJ' then 1
when Projecttype = 'IDF' then 2
when Projecttype = 'CLNUP' then 3
when Projecttype = 'REBL' then 4
when Projecttype = 'R&R' then 5
when Projecttype = 'RTDP' then 6
when Projecttype = 'CAP' then 7
when Projecttype = 'PST' then 8
end

DECLARE @ResultCnt int, @ResCounter int, @ProjSeqNum int, @RegionCnt int,
@RegionCounter int, @AllRegions varchar(30), @Region varchar(10)

Select * into #temp from #RptResult
Select @ResultCnt = count(*) from #temp
Set Rowcount 1
Select @ResCounter = 0
WHILE @ResCounter <= @ResultCnt
BEGIN
Select @ProjSeqNum = ProjSeqNum FROM #temp
set rowcount 0
IF (Select Count(*) FROM RSM_ProjectState WHERE ProjSeqNum = @ProjSeqNum
and Region is Not Null) > 0
BEGIN
Select Region into #region from RSM_ProjectState WHERE ProjSeqNum =
@ProjSeqNum and Region is Not Null
Select @RegionCnt = Count(*) FROM #region
set rowcount 1
Select @RegionCounter = 1, @AllREgions = ''
IF @RegionCnt = 1 ---- Handles ONE region ----------------
BEGIN
Select @AllRegions = Region from #region
END
ELSE
BEGIN ------ Handles Multiple Regions ------------------
While @RegionCounter <= @RegionCnt
BEGIN
Select @Region = ltrim(rtrim(Region)) FROM #region
IF IsNull(charindex(@Region,@AllRegions),'0') = 0
BEGIN
IF @RegionCounter = 1
BEGIN SELECT @AllRegions = @Region END
ELSE
BEGIN Select @AllRegions = IsNull(@AllRegions,'') +
',' + @Region END
END
SELECT @RegionCounter = @RegionCOunter + 1
Delete from #region
END
UPDATE #RptResult
Set Region = @AllRegions
Where ProjseqNum = @ProjSeqNum
END
Drop Table #region
END
Set Rowcount 1
SELECT @ResCounter = @ResCounter + 1
Delete from #temp
END

Set rowcount 0
SELECT * from #RptResult

--Drop table #temp, #RptResult
END
GO
-------------------------------------------------
thanks for your hel in advance.

Krishnakanth. VM

John W. Vinson said:
when I modify the query as follows.

CASE budgetcategory
WHEN 'ECP' THEN 1
WHEN 'CAP' THEN 2
ELSE 3
END as SortVal1

CASE ProjectType
WHEN 'PROJ' THEN 1
WHEN 'IDF' THEN 2
WHEN 'CLNUP' THEN 3
WHEN 'REBL' THEN 4
WHEN 'R&R' THEN 5
WHEN 'RTDP' THEN 6
WHEN 'CAP' THEN 7
ELSE 8
END as SortVal2

I am getting the error message as follows.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 88
Incorrect syntax near the keyword 'as'.
Server: Msg 156, Level 15, State 1, Procedure auto_or_fire_str_proc_vx1,
Line 150
Incorrect syntax near the keyword 'END'.

Is this in Microsoft Access, or in SQL/Server? Access does not support
the CASE syntax, which is probably why you're getting the error.

Try using the Switch() function instead:
SELECT Switch([budgetcategory] = "ECP", 1, [budgetcategory] = "CAP",
2, True, 3) AS SortVal1,
Switch([ProjectType] = "PROJ", 1,
[ProjectType] = "IDF", 2,
[ProjectType] = "CLNUP", 3,
[ProjectType] = "REBL", 4,
[ProjectType] = "R&R", 5,
[ProjectType] = "RTDP", 6,
[ProjectType] = "CAP", 7,
True, 8) AS SortVal2


John W. Vinson [MVP]
 
J

John W. Vinson

I don't how to modify it in the below query.

Could you plz. modify it in the below query.

Are you using Microsoft Access, or SQL/Server? The query syntax
suggests you're used to SQL; Access syntax is quite different.

The query is too long and complex for me to feel comfortable editing
it on an unpaid volunteer basis. Note that in Access you CANNOT string
multiple queries into one object; instead you need to store separate
queries and run them from VBA code, in sequence.

If you are constructing a pass-through query to be executed on a
SQL/Server database, I'd suggest you get help in a SQL forum.

John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top