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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Query with Sum formula from multiple tables

Status
Not open for further replies.

jabenj

Technical User
Joined
Dec 18, 2008
Messages
20
Location
US
Can someone please let me know why this isn't working? I'm trying to take the difference of two fields from two separate spreadsheets. I believe it's only reading from the first spreadsheet (tlbLatexBatch QC) and not the other. Any help would be greatly appreciated.



Dim con As Object
Dim rs As Object
Dim stSql As String
Dim currentweight As Long


Set con = Application.CurrentProject.Connection
stSql = "SELECT Sum(tblLatexBatchQC.[TransferredWeight]) - Sum([Blendsheet Ouptput].Weight) AS currentweight"
stSql = stSql & " WHERE tblLatexBatchQC.[TankNum] = [Blendsheet Output].TankN" & Me.cmbTankNum & ""
Set rs = CreateObject("ADODB.Recordset")
rs.Open stSql, con, 1

TxtCurrentWeight.SetFocus
currentweight = TxtCurrentWeight.Text
 
There is not any "FROM ..." clause in your stSql.

There is no reason to set focus to TxtCurrentWieght. You can use:
Code:
  currentweight = Me.TxtCurrentWeight


Duane
Hook'D on Access
MS Access MVP
 
Tried this way also and didn't work

stSql = "SELECT Sum([TransferredWeight]) - Sum(Weight) From tblLatexBatchQC, [Blendsheet Output] AS currentweight"
stSql = stSql & " WHERE tblLatexBatchQC.[TankNum] = [Blendsheet Output].TankN" & Me.cmbTankNum & ""
Set rs = CreateObject("ADODB.Recordset")
rs.Open stSql, con, 1
 
What about this ?
Code:
stSql = "SELECT Sum([TransferredWeight]) - Sum(Weight) AS currentweight" _
 & " FROM tblLatexBatchQC INNER JOIN [Blendsheet Output]" _
 & " ON tblLatexBatchQC.TankNum = [Blendsheet Output].TankN" & Me!cmbTankNum
Set rs = CreateObject("ADODB.Recordset")
rs.Open stSql, con, 1
Me!TxtCurrentWeight = rs!currentweight

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
No. Keep getting same error messages as before. Depending on how I arrange code keep getting:

No value given for one or more required parameters

Or

Run time Error - The select statement includes a reserved word or an argument name that is mispelled or missing, or the punctuation is incorrect.

Leads me to believe I may have some stupid mistake in there somewhere
 
Debug.Print stSql and then copy/paste in a SQL view pane.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks. I'll keep plugging away. SQL pane says:

Compile error, Variable not yet created in this context

I defined the currentweight variable so I'm not sure what exactly it's talking about
 
Got it to work:

stSql = "SELECT Sum([TransferredWeight]) - Sum(Weight) AS currentweight" _
& " FROM tblLatexBatchQC INNER JOIN [Blendsheet Output]" _&"ON tblLatexBatchQC.TankNum = [Blendsheet Output].TankN Where [Blendsheet Output].TankN = '" & Me!cmbTankNum & "'"

Needed the where clause to get it to work

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top