So I'm trying to insert 120 rows into a new table and it keeps throwing "Query too complex" errors at me. It works fine upto 90 rows.
This is the query:
SQL = "INSERT INTO " & newtable & " ([" & sourceQ & "_" & master & "],
[" & sourceQ & "_" & fld.Name & "])" & " SELECT
[" & sourceQ & "." & master & "], [" & sourceQ & "." & fld.Name & "]"
& " FROM " & sourceQ
I have tried playing with where clauses adding WHERE [" & sourceQ & ".Expr1] like ('Field09?') but it still throws those errors.
I even tried a simple one that should work like WHERE [" & sourceQ & ".Expr1] = 'Field001', which should just be 1 row, but it still throws the error.
Anyone have any ideas?
edit: debug
INSERT INTO tblCalc ([qJoinKeyAndStudent_<>], [qJoinKeyAndStudent_0 1 0 1])
SELECT [qJoinKeyAndStudent.<>], [qJoinKeyAndStudent.0 1 0 1]
FROM qJoinKeyAndStudent WHERE [qJoinKeyAndStudent.Expr1] like ('*09#')
While Access is capable of handling spaces in field and table names, you need to encapsulate both the field and table names in separate brackets:
INSERT INTO tblCalc ([qJoinKeyAndStudent_<>], [qJoinKeyAndStudent_0 1 0 1])
SELECT [qJoinKeyAndStudent].[<>], [qJoinKeyAndStudent].[0 1 0 1]
FROM qJoinKeyAndStudent WHERE [qJoinKeyAndStudent].[Expr1] like ('*09#')
Does that work?
Related
i had this error when i was trying to INSERT records to a local table with VBA.
I have checked the data type and putting in the quotes for the the short text data type but it doesn't work.
table_newid = "SELECT Cint(t1." & id_name(i) & "_new), " & Replace(select_column_str, local_table_name, "t2") & " FROM " & vbCrLf & _
"(SELECT CInt(DCount(""[" & id_name(i) & "]"", """ & qry_distinct_id_name & """, ""[" & id_name(i) & "]<="" & [" & id_name(i) & "])) as " & id_name(i) & "_new, * FROM " & qry_distinct_id_name & ") AS t1 " & vbCrLf & _
"LEFT JOIN " & local_table_name & "_ALL as t2 " & vbCrLf & _
"ON t1." & id_name(i) & " = t2." & id_name(i) & " " & vbCrLf & _
"WHERE t2.database = '" & database_name & "'"
strQuery = "INSERT INTO " & local_table_name & "_temp (" & temp_field(i) & ", " & Replace(select_column_str, local_table_name & ".", "") & ") " & vbCrLf & table_newid
Debug.Print strQuery
DoCmd.SetWarnings False
db.Execute strQuery
DoCmd.SetWarnings True
From the debug.print, i have got:
INSERT INTO TblLUMachineTypes_temp (MachTypeID_new, MachTypeID, MachTypeCode, MachTypeMod, MachTypeDesc, MachTypeDisc, NewCode, Approved, mttime, CreatedBy, CreatedTS, ModifiedBy, ModifiedTS)
SELECT t1.MachTypeID_new, t2.MachTypeID, t2.MachTypeCode, t2.MachTypeMod, t2.MachTypeDesc, t2.MachTypeDisc, t2.NewCode, t2.Approved, t2.mttime, t2.CreatedBy, t2.CreatedTS, t2.ModifiedBy, t2.ModifiedTS FROM
(SELECT CInt(DCount("[MachTypeID]", "qry_TblLUMachineTypes_id_distinct", "[MachTypeID]<=" & [MachTypeID])) as MachTypeID_new, * FROM qry_TblLUMachineTypes_id_distinct) AS t1
LEFT JOIN TblLUMachineTypes_ALL as t2
ON t1.MachTypeID = t2.MachTypeID
WHERE t2.database = 'CPM-252-2'
When i copied this query and execute it manually, it works fine but not with VBA. Any idea?
Thanks in advance.
Remove all the & vbCrLf from your code, they are not necessary and I assume they corrupt the SQL syntax.
I found the problem. I found that qry_distinct_id_name query table has a Dlookup function in there that returns a string value, which will work when executing the query manual but doesn't work when you run it with VBA. So I have re-written the code to put in the quote before and after dlookup() function.
A) I'm collecting data from legacy applications; data elements are containing spaces and hyphens (CBR - CR - 22 - 2) or (CBR-CR-22-2)
B) In VBA, when building function and passing SQL parameters with those data elements, the code is generating errors (many!)
C) I narrowed down to identify that ALL errors were caused by the format of the data elements
D) The data element causing the problem is a PRIMARY KEY in about 30 tables and is generated by the legacy applications
(see code) I tried syntax:
A) strDataElm
B) [strDataElm]
C) & """ & strDataElm & """
strSourceSql = "SELECT " & strSourceFld & " FROM " & strSourceTbl & " WHERE " & strSourceFld & " = " & strDataElm
in the immediate window it gives
SELECT NumeroCtl FROM tblLnkCtrl WHERE RISKID = CBR - CR - 22 - 2
In a ideal world, I'd like the strDataElm being passed as is i.e. CBR - CR - 22 2 or without spaces CBR-CR-22-2; removing the hyphens would bring issues because the data is a primary key and reused in other applications..
As it is mentioned it is better to use parametrized queries, but I know - way to concatenate string is too pleasurable.
Try this:
strSourceSql = "SELECT " & strSourceFld & " FROM " & strSourceTbl & " WHERE [" & strSourceFld & "] = [" & strDataElm & "]"
if you pass the field reference
And this, if you pass the value itself
strSourceSql = "SELECT " & strSourceFld & " FROM " & strSourceTbl & " WHERE " & strSourceFld & " = " & "'" & strDataElm & "'"
I have a dropdown box in an MS Access form which is populated by the following select query:
strSQL = "SELECT [Process] " _
& "FROM [dbo_tbl_Area_Process] " _
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & ";"
Me.Process.RowSource = strSQL
I would like to add Active = -1 as a second criteria to the query to further limit the selections.
I have tried, so far unsuccessfully to add this second criteria and am at a loss as to how to proceed. Any help from the community would be most appreciated.
I have tried the following where conditions:
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " and Active =-1"
This does not return any results.
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " and Active ="-1""
This has a compile error:
Expected:end of statement
Following on from mintys comment regarding linked SQL server tables I changed the query to the following:
strSQL = "SELECT dbo_Tbl_Area_Process.Process " _
& "FROM dbo_Tbl_Area_Process " _
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " AND Active=True"
Adding the table references to the SELECT and FROM lines gives me the outputs I expected.
Thanks all for your comments
why the following msaccess vba statement fails when add the where statement?
I want to check if the field has value
Set rsMySet = CurrentDb.OpenRecordset("PivotTblInvDepts")
'Start the count at the position number of the first column-oriented field
'Remember that Recordsets start at 0
For i = 3 To rsMySet.Fields.Count - 1
'*********************************************************************************************************************
'Use the recordset field.name property to build out the SQL string for the current field
strSQL = "INSERT INTO TabularDepts ([Depts], [Code], [Description], [Qty])" & _
"SELECT" & "'" & rsMySet.Fields(i).Name & "'" & " AS Dept," & _
"[PivotTblInvDepts].[Code],[PivotTblInvDepts].[Description]," & _
"[" & rsMySet.Fields(i).Name & "]" & _
"FROM PivotTblInvDepts;" & _
"WHERE" & " (rsMySet.Fields(i).Name)>0"
Try removing the ; in FROM PivotTblInvDepts;.
Also you need space at the end of the previous line. "[" & rsMySet.Fields(i).Name & "]" & _ should be "[" & rsMySet.Fields(i).Name & "] " & _. Likewise, make sure to add a space so that you do no result in FROM PivotTblInvDeptsWHERE
Your current SQL reads something like this
INSERT INTO TabularDepts ([Depts], [Code], [Description], [Qty])
SELECT '<data>' As Dept,
[PivotTblInvDepts].[Code],[PivotTblInvDepts].[Description],
[<data>]
FROM PivotTblInvDepts;
WHERE <condition>
After removing the ;, the insert will be cleaner.
I need some help with my query I am trying to insert to a table see above code:
CurrentDb.Execute ("INSERT INTO tblWarehouseItem ( whiwrhID, whiItemName, whivatName," whiVatRate, whiimtID, whiQty, whiPrice, whisupID, whiDateIn, whiExpiryDate,whiwhiID ) " & _
" Values (" & rs!WID & "," & Chr(34) & rs!N & Chr(34) & "," & Chr(34) & rs!VN & Chr(34) & "," & rs!VR & "," & rs!IID & "," & intQtyForm & "," & rs!PR & "," & rs!SID & "," & CDate(rs!DIN) & "," & CDate(rs!EXD) & "," & rs!ID & ")")
In my table the CDate(rs!EXD) and CDate(rs!DIN) are stored as time.
When I compile my query in the immediate window I get Dates.
INSERT INTO tblWarehouseItem ( whiwrhID, whiItemName, whivatName, whiVatRate, whiimtID, whiQty, whiPrice, whisupID, whiDateIn, whiExpiryDate,whiwhiID ) Values (2,"ITEM10","A",19,14,4,20,10,21/07/14,26/05/14,60)
How can I make my query to insert the date in the table?
thanks in advance
If you run that 21/07/14 is passed to Access and interpreted as an integer resulting from 12 divide by 7 divide by 14 which when converted to a date is just a time.
Delimit dates with #:
.. ",#" & CDate(rs!DIN) & "#," ..