Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 SQL statements into 1

Status
Not open for further replies.

Greaser

Technical User
Aug 1, 2001
84
0
0
CA
Hi,
Is there a way to combine 2 SQL statements into 1?

Query1:
SELECT itsTbl.itsNo, itsTbl.workplanNo, itsTbl.workplanSectionNo
FROM itsTbl
WHERE (((itsTbl.workplanNo)=[Forms]![itsNewFrm]![workplanNoCombo]) AND ((itsTbl.workplanSectionNo)=[Forms]![itsNewFrm]![workplanSectionNoCombo]));


Query2:
SELECT Max(Right([itsNo],3))+1 AS itsNo_3
FROM Query1;

This SQL query will be attached to [Forms]![itsNewFrm]![workplanSectionNoCombo] AfterUpdate statement

Then, I would like to return the value of the result of Query2 to unbound field itsNo_3 in form itsNewFrm

Thanks,
John
 
Code:
SELECT Max(Right(Q1.[itsNo],3))+1 AS itsNo_3
FROM 

(
SELECT itsTbl.itsNo
     , itsTbl.workplanNo
     , itsTbl.workplanSectionNo

FROM itsTbl

WHERE itsTbl.workplanNo=[Forms]![itsNewFrm]![workplanNoCombo] 
  AND itsTbl.workplanSectionNo=[Forms]![itsNewFrm]![workplanSectionNoCombo]
) As Q1
 
Thanks Golom.
I have one more question:
In what context do I use the query?
If I use DoCmd.RunSQL, the debugger will only let me use the UPDATE statement, not the SELECT statement.
Also, Do I declare itsNo_3 as a string?

Thanks again,
John
 
I would like to return the value of the result of Query2 to unbound field itsNo_3 in form itsNewFrm
Why not simply something like this ?
Code:
Me!itsNo_3 = Nz(DMax("Right(itsNo,3)", "itsTbl", "workplanNo='" _
  & Me!workplanNoCombo & "' AND workplanSectionNo='" _
  & Me!workplanSectionNoCombo & "'"), 0) + 1
If either workplanNo or workplanSectionNo are defined as numeric in itsTbl then get rid of the corresponding single quotes.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your help.
workplanNo and workplanSectionNo are declared as Text (2 chars) in itsTbl.

I get a "type mismatch" error message.

When I remove the single quotes, I get a "Type mismatch in criteria expression" error message.
Thanks again,
Cheers,
John
 
And this ?
Me!itsNo_3 = Val(DMax("Right(itsNo,3)", "itsTbl", "workplanNo='" _
& Me!workplanNoCombo & "' AND workplanSectionNo='" _
& Me!workplanSectionNoCombo & "'") & "") + 1

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Worked like a charm.
Thanks.
Cheers,
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top