Question : Long query in code does not run

Hi,
I have the following query in Access 2003 copied from the Code window that runs in the query designer window if I press the delete key to bring the broken "words" together.  For example when I copy the query from the Debug window and past into the query designer SQL window the "word' tbl_WBS03 is split on two lines: "tbl_WBS" on one line and on the next line "03".  I place the cursor after the S and press Delete to bring the tbl_WBS03 together and the query works (actually there is another instance similar to this in the query.

I have create lots of quereies using the strQuery=strQuery & " " format and it always works.

The query is:

SELECT DISTINCT tbl_WBS01.code_L1, tbl_WBS01.desc_L1, tbl_WBS02.code_L1, tbl_WBS02.desc_L1 , tbl_WBS03.code_L1, tbl_WBS03.desc_L1, tbl_WBS04.code_L1, tbl_WBS04.desc_L1 , tbl_WBS05.code_L1, tbl_WBS05.desc_L1, tbl_WBS11.code_L1, tbl_WBS11.desc_L1 , tbl_WBS11.Level1_Code, tbl_WBS12.code_L1, tbl_WBS12.desc_L1, tbl_WBS12.Level1_Code, tbl_WBS21.code_L1 , tbl_WBS21.desc_L1, tbl_WBS22.code_L1, tbl_WBS22.desc_L1, tbl_WBS23.ProcurementCode , tbl_WBS24.procurementTypeID, tbl_ProcurementType.desc_L1 , tbl_ProcurementCode.desc_L1 FROM (tbl_ProcurementCode RIGHT JOIN (tbl_ProcurementType RIGHT JOIN (((tbl_WBS22 RIGHT JOIN tbl_WBS21 ON tbl_WBS22.w21code_L1 = tbl_WBS21.w21_ID) LEFT JOIN tbl_WBS23 ON tbl_WBS22.w22_ID = tbl_WBS23.w22_ID) LEFT JOIN tbl_WBS24 ON tbl_WBS23.w23_ID = tbl_WBS24.WBS23ID) ON tbl_ProcurementType.Code_L1 = tbl_WBS24.procurementTypeID) ON tbl_ProcurementCode.Code_L1 = tbl_WBS23.procurementCode) INNER JOIN ((((((tbl_WBS01 INNER JOIN tbl_WBS02 ON tbl_WBS01.code_L1 = tbl_WBS02.w01code_L1) INNER JOIN tbl_WB
S03 ON tbl_WBS02.code_L1 = tbl_WBS03.w02code_L1) INNER JOIN tbl_WBS04 ON tbl_WBS03.code_L1 = tbl_WBS04.w03code_L1) INNER JOIN tbl_WBS05 ON tbl_WBS04.code_L1 = tbl_WBS05.w04code_L1) INNER JOIN tbl_WBS11 ON tbl_WBS05.code_L1 = tbl_WBS11.w05_Projcode) INNER JOIN tbl_WBS12 ON tbl_WBS11.w11_ID = tbl_WBS12.w11code_L1) ON tbl_WBS21.w1code_L1=tbl_WBS12.w12_ID WHERE (((tbl_WBS05.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![ProjCode]) AND ((tbl_WBS11.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w11_Code]) AND ((tbl_WBS12.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code]) AND ((tbl_WBS12.Level1_Code)=2)AND ((tbl_WBS21.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w21_Code]) AND ((tbl_WBS22.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w22_Code]) AND ((tbl_WBS23.procurementCode)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![ProcurementCode]) AND ((tbl_WBS24.procurementTypeID)=       [Forms]![frmEnterHoursMain]![frm
EnterHoursSub].[Form]![ProcurementType]));

and the code to create it is:

Dim strSQL As String


strSQL = "SELECT DISTINCT tbl_WBS01.code_L1, tbl_WBS01.desc_L1, tbl_WBS02.code_L1, "
strSQL = strSQL & "tbl_WBS02.desc_L1 , tbl_WBS03.code_L1, tbl_WBS03.desc_L1, tbl_WBS04.code_L1, "
strSQL = strSQL & "tbl_WBS04.desc_L1 , tbl_WBS05.code_L1, tbl_WBS05.desc_L1, "
strSQL = strSQL & "tbl_WBS11.code_L1, tbl_WBS11.desc_L1 , tbl_WBS11.Level1_Code, "

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] <> "" Then
strSQL = strSQL & "tbl_WBS14.Level1_Code, tbl_WBS14.code_L1, tbl_WBS14.desc_L1, "
ElseIf [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] = "" Then
strSQL = strSQL & "tbl_WBS13.Level1_Code, tbl_WBS13.code_L1 , tbl_WBS13.desc_L1, "
ElseIf [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] = "" Then
strSQL = strSQL & "tbl_WBS12.code_L1, tbl_WBS12.desc_L1, tbl_WBS12.Level1_Code, "
End If

strSQL = strSQL & "tbl_WBS21.code_L1 , tbl_WBS21.desc_L1, tbl_WBS22.code_L1, tbl_WBS22.desc_L1, "
strSQL = strSQL & "tbl_WBS23.ProcurementCode , tbl_WBS24.procurementTypeID, "
strSQL = strSQL & "tbl_ProcurementType.desc_L1 , tbl_ProcurementCode.desc_L1 "
strSQL = strSQL & "FROM (tbl_ProcurementCode RIGHT JOIN (tbl_ProcurementType "
strSQL = strSQL & "RIGHT JOIN (((tbl_WBS22 RIGHT JOIN tbl_WBS21 ON tbl_WBS22.w21code_L1 = tbl_WBS21.w21_ID) "
strSQL = strSQL & "LEFT JOIN tbl_WBS23 ON tbl_WBS22.w22_ID = tbl_WBS23.w22_ID) "
strSQL = strSQL & "LEFT JOIN tbl_WBS24 ON tbl_WBS23.w23_ID = tbl_WBS24.WBS23ID) "
strSQL = strSQL & "ON tbl_ProcurementType.Code_L1 = tbl_WBS24.procurementTypeID) "
strSQL = strSQL & "ON tbl_ProcurementCode.Code_L1 = tbl_WBS23.procurementCode) "
strSQL = strSQL & "INNER JOIN "

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] <> "" Then
strSQL = strSQL & "((("
ElseIf [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] = "" Then
strSQL = strSQL & "(("
ElseIf [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] = "" Then
strSQL = strSQL & "("
End If

strSQL = strSQL & "(((((tbl_WBS01 INNER JOIN tbl_WBS02 "
strSQL = strSQL & "ON tbl_WBS01.code_L1 = tbl_WBS02.w01code_L1) "
strSQL = strSQL & "INNER JOIN tbl_WBS03 ON tbl_WBS02.code_L1 = tbl_WBS03.w02code_L1)"
strSQL = strSQL & " INNER JOIN tbl_WBS04 ON tbl_WBS03.code_L1 = tbl_WBS04.w03code_L1) "
strSQL = strSQL & "INNER JOIN tbl_WBS05 ON tbl_WBS04.code_L1 = tbl_WBS05.w04code_L1) "

strSQL = strSQL & "INNER JOIN tbl_WBS11 ON tbl_WBS05.code_L1 = tbl_WBS11.w05_Projcode) "

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" Then
strSQL = strSQL & "INNER JOIN tbl_WBS12 ON tbl_WBS11.w11_ID = tbl_WBS12.w11code_L1) "
strSQL = strSQL & "ON tbl_WBS21.w1code_L1=tbl_WBS12.w12_ID "
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] <> "" Then
strSQL = strSQL & " INNER JOIN tbl_WBS13 ON tbl_WBS12.w12_ID = tbl_WBS13.w12code_L1) "
strSQL = strSQL & "ON tbl_WBS21.w1code_L1 = tbl_WBS13.w13_ID "
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] <> "" Then
strSQL = strSQL & " INNER JOIN tbl_WBS14 ON tbl_WBS13.w13_ID = tbl_WBS14.w13code_L1) "
End If


strSQL = strSQL & "WHERE (((tbl_WBS05.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![ProjCode]) "
strSQL = strSQL & "AND ((tbl_WBS11.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w11_Code]) "

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" Then
strSQL = strSQL & "AND ((tbl_WBS12.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code]) "
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] <> "" Then
strSQL = strSQL & "AND ((tbl_WBS13.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code]) "
End If
If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] <> "" Then
strSQL = strSQL & "AND ((tbl_WBS14.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code]) "
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] = "" Then
strSQL = strSQL & "AND ((tbl_WBS12.Level1_Code)=2)"
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w12_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w13_Code] <> "" And _
[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] = "" Then
strSQL = strSQL & "AND ((tbl_WBS13.Level1_Code)=3)"
End If

If [Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w14_Code] <> "" Then
strSQL = strSQL & "AND ((tbl_WBS14.Level1_Code)=4)"
End If

strSQL = strSQL & "AND ((tbl_WBS21.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w21_Code]) "
strSQL = strSQL & "AND ((tbl_WBS22.code_L1)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![w22_Code]) "
strSQL = strSQL & "AND ((tbl_WBS23.procurementCode)=[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![ProcurementCode]) "
strSQL = strSQL & "AND ((tbl_WBS24.procurementTypeID)="
strSQL = strSQL & "[Forms]![frmEnterHoursMain]![frmEnterHoursSub].[Form]![ProcurementType])); "

Debug.Print strSQL

DoCmd.OpenQuery (strSQL)

How do I correct this problem?

Thanks,
merdeka
   

Answer : Long query in code does not run

merdeka ,

<
I tried "CurrentDB().Execute strSQL" and got an error message "Cannot execute a select query">>

  Yes, sorry.  I didn't notice on my first post that this was a select query.

<>

 The querydef code is correct, but you need a little more.  When you execute a query through Access, it takes care of resolving any parameters you might have used.  When you execute the query in code, you need to take care of that.  So here is the code you need to use:

Dim qdf as querydef
Dim prm as Parameter
Dim rst as recordset
Dim intI as integer

Set qdf = CurrentDB.CreateQueryDef("", strSQL)

For intI = 0 to qdf.Parameters.Count -1
  Set prm = qdf.Parameters(intI)
  prm.value = Eval(prm.name)
Next intI

Set rst = qdf.OpenRecordSet()


  Sorry I didn't read the question more closely.

JimD







Random Solutions  
 
programming4us programming4us