I'm having problems with a query I'm building from a SQL statement in VBA.
First I build the query using:
Code:
Dim dbs As Database
Dim strSQL
Dim strQueryName As String
Dim qryDef As QueryDef
Set dbs = CurrentDb
strQueryName = "qryBacklog"
For Each qryDef In dbs.QueryDefs
If qryDef.Name = strQueryName Then
dbs.QueryDefs.Delete strQueryName
Exit For
End If
Next
Then use a string to build the (numerous) fields:
Code:
strSQL = "SELECT dbo_tblJobInfo.FabLoc, dbo_tblJobInfo.JobNum, dbo_tblJobInfo.JobName, dbo_tblJobInfo.Cstmr1, " & _
"dbo_tblEmployees.FirstName & " " & dbo_tblEmployees.LastName AS Detlr, dbo_tblJobInfo.Sales, " & _
"dbo_tblMrktCat.MrktCatDesc AS MrktCat, dbo_tblJobInfo.BillType, dbo_tblJobInfo.LastInvoice, " & _
"dbo_tblJobInfo.EstWgt, dbo_tblJobInfo.AdjWgt, dbo_tblJobInfo.DelWgt, dbo_tblJobInfo.BalToDet, dbo_tblJobInfo.EstLastDel, " & _
"dbo_tblJobInfo.Comments, dbo_tblJobInfo.DetStatus, dbo_tblJobInfo.JobStatus, Sum(dbo_tblSubmittals.DetWgt) AS DetWgt, " & _
"([DelWgt]/([DetWgt]+[BalToDet])) AS PercDel, ([DetWgt]/([DetWgt]+[BalToDet])) AS PercDet, " & _
"([AdjWgt]-([DetWgt]+[BalToDet])) AS OverUnder, ([AdjWgt]-[DelWgt]) AS BalToDel " & _
"FROM dbo_tblEmployees INNER JOIN (dbo_tblMrktCat INNER JOIN (dbo_tblJobInfo LEFT JOIN dbo_tblSubmittals " & _
"ON dbo_tblJobInfo.JobNum = dbo_tblSubmittals.JobNum) ON dbo_tblMrktCat.MrktCatCode = dbo_tblJobInfo.MrktCat) ON dbo_tblEmployees.Username = dbo_tblJobInfo.Detlr1 " & _
"GROUP BY dbo_tblJobInfo.FabLoc, dbo_tblJobInfo.JobNum, dbo_tblJobInfo.JobName, dbo_tblJobInfo.Cstmr1, Detlr, " & _
"dbo_tblJobInfo.Sales, dbo_tblMrktCat.MrktCatDesc, dbo_tblJobInfo.BillType, dbo_tblJobInfo.LastInvoice, dbo_tblJobInfo.EstWgt, " & _
"dbo_tblJobInfo.AdjWgt, dbo_tblJobInfo.DelWgt, dbo_tblJobInfo.BalToDet, dbo_tblJobInfo.EstLastDel, dbo_tblJobInfo.Comments, " & _
"dbo_tblJobInfo.DetStatus, dbo_tblJobInfo.JobStatus " & _
"HAVING (((dbo_tblJobInfo.FabLoc) =" & lngFabDBID1 & ") And ((dbo_tblJobInfo.JobStatus) ='" & "Open" & "' Or (dbo_tblJobInfo.JobStatus) ='" & "On Hold" & "')) Or (((dbo_tblJobInfo.FabLoc) =" & lngFabDBID2 & ") And ((dbo_tblJobInfo.JobStatus) ='" & "Open" & "' Or (dbo_tblJobInfo.JobStatus) ='" & "On Hold" & "')) Or (((dbo_tblJobInfo.FabLoc) =" & lngFabDBID3 & ") And ((dbo_tblJobInfo.JobStatus) ='" & "Open" & "' Or (dbo_tblJobInfo.JobStatus) ='" & "On Hold" & "')) " & _
"ORDER BY dbo_tblJobInfo.JobNum;"
Then finish the code off:
Code:
Set qryDef = dbs.CreateQueryDef(strQueryName, strSQL)
Now the code is failing when trying to concatenate the Name fields highlighted above. I've tried to recreate the query through the query design, view the SQL, all of that and this is how it looks, however once I pair it with the string and quotations it just gets a little crazy.