I'm trying to run code that will copy fields into a new table, moving them from a _New table to the original table. The VBA code that does this works as such:
SQLStatement = CStr("INSERT INTO " & TName & " SELECT * FROM " & TName & "_New")
Log.WriteLine "Running query with string """ & SQLStatement & """ "
QueryTimer = Timer
DoCmd.RunSQL SQLStatement
Log.WriteLine "Query time: " & (Timer - QueryTimer)
The log is just a handful of procedures in a class module I threw together. Its output on the error is
#142921: Running query with string "INSERT INTO Records SELECT * FROM Records_New"
#142941: Error Date/Time: 7/21/2009 2:29:40 PM
#142941: Error # & Description: 3162, You tried to assign the Null value to a variable that is not a Variant data type.
I can confirm that TName and SQLStatement are both valid strings at the time the SQL operation is run, and that the sources (Records and Records_New) are both valid. Option Explicit is set elsewhere in the file to avoid any confusion from typos. The error is thrown on the DoCmd line.
Why would this have a Null value, even though DoCmd.RunSQL doesn't return a value?
Can you post the table descriptions for Records and Records_New tables?
I would wager that you are trying to insert a NULL value into one of the columns of the "Records" table (and the column description is NOT NULL).
Hope this helps.
I think it will help if you also change the insert statement to be more specific about which columns it is inserting/selecting. You are asking for bugs by being so non-specific.
This may seem like it is non-responsive to your answer, but I suspect that the columns in the select table and destination table are either not lined up, or there is a field in the destination table that disallows null.
Try this:
In a new Query (in SQL view) paste your query "INSERT INTO Records SELECT * FROM Records_New" in and try to run it manually. I bet you get a more specific error and can troubleshoot the query there before running it with the added complexity of the code around it.
INSERT INTO Statement (Microsoft Access SQL)
Your SQL INSERT statement is incorrect - it should be:
INSERT INTO Records SELECT * FROM [Records_New];
Here's what you need to use:
CStr("INSERT INTO " & TName & " SELECT * FROM [" & TName & "_New)"];")
Maybe Timer needs parens?
QueryTimer = Timer()
Related
I have a MS Access 2016 database (*.accdb) with 20+ Tables. Fields in each of them vary slightly from Table to Table. I've no VBA experience, so I'm sticking only to the SQL query below (redacted).
SQL script
myvar below is the parameter I'd like to be prompted when the script is run so that I enter the Table I want the changes applied to.
PARAMETERS
[myvar] TableID;
UPDATE
[myvar]
INNER JOIN
Excel_Data ON [myvar].[Part Number] = Excel_Data.[Part Number]
SET
[myvar].[Value] = '?',
[myvar].Description = Excel_Data.Description,
[myvar].[Ref] = '?'
.
.
.
WHERE
[myvar].Description Is Null;
Output
Error message:
Too few parameters. Expected 0.
What I need
I prefer a solution for above in a SQL script form as above, not involving VBA, preferably. I'd like to enter the Table name when prompted so the script knows which table to UPDATE. FYI: The PARAMETERS work when it is not a Table as I've shown in my script above.
Help/advise is highly appreciated.
EDIT 1
Since it seems not possible to use parameters as Table names, could you suggest a VBA solution? A sample code, perhaps?
As said in the comments, you can't really solve this without VBA.
You can store your SQL query in a string, and use a placeholder to indicate the tablename. Then get the tablename using an inputbox and replace the placeholder with the tablename.
Dim sqlString As String
sqlString = "UPDATE [%Placeholder%] " & vbCrLf & _
"INNER JOIN Excel_Data ON [%Placeholder%].[Part Number] = Excel_Data.[Part Number] " & vbCrLf & _
"SET [%Placeholder%].[Value] = '?', " & vbCrLf & _
...
"WHERE [%Placeholder%].Description Is Null;"
sqlString = Replace(sqlString, "%PlaceHolder%", InputBox("Enter a tablename"))
CurrentDb.Execute sqlString
In a more mature solution, I'd create a form with a combobox containing all available table names, and add a function to sanitize tablenames (replace "]" with "]]")
Seems a small issue with an sql update query. But I can not get my head around this issue. Not very much acquainted with sql.
Based on a selection, selected rows from a table (7DARphases) are copied and inserted to another table (7tblDAR). And the Project ID (PRJID column) for all the inserted rows should be updated with a fixed value (PID) taken from the form.
Issue is: I am facing syntax issue in the part where the fixed value needs to be inserted in the PRJID column of added rows.
Code is:
Set dbs = CurrentDb
PID = Me.PRJID.Value
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI) SELECT [7DARphases].Phase, [7DARphases].Deliverable, [7DARphases].Link_PIM_temp, [7DARphases].Link_PIM_WB, [7DARphases].Description, [7DARphases].Accept_criteria, [7DARphases].Deliv_type, [7DARphases].TollgateID, [7DARphases].Workstream, [7DARphases].Accountable, [7DARphases].ClientRACI FROM 7DARphases WHERE ((([7DARphases].SolType) Like '*2PP*')) ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
sqlUpdt = "update 7tblDAR set PRJID = '" & Me.PRJID.Value & "' from 7tblDAR where tblDAR.PRJID = """""
dbs.Execute sqlstr, dbFailOnError
dbs.Execute sqlUpdt, dbFailOnError
The 'sqlstr' works fine and inserts rows.
But 'sqlUpdt' gives error:
"Run-time error 3075: Syntax error (missing operator) in query expression ..."
Can you please help me out with this.
Plus, if possible, can you suggest to perform this action in one query itself.
Why not put the value in when you insert the values?
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI, PRJID)
SELECT . . .,
'" & Me.PRJID.Value & "'
WHERE [7DARphases].SolType Like '*2PP*')
ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
This saves the trouble of having to update it later.
The SQL statement below inserts values into a new table, in a new sheet (TempPoints). If I remove the last part of the statement (in bold) the query runs OK however when I add it back in to the statement I receive the following error message; '[ODBC EXCEL Driver] Data Type Mismatch in criteria'.
I have replicated the tables and query in SQLServer and it runs OK and as expected which proves the full statement is OK. Is this a limitation of the Excel ODBC driver??
Thank you in advance.
strSQL = "INSERT INTO [TempPoints$] (REFP, PointItemRef, LoadItemRef, PointDES, Iotype, Subtype, Notes) " & _
"SELECT " & tempref & ", [PointsDB$].ItemRef, [LoadstoPointsDB$].LoadRef, [PointsDB$].PointDES, [PointsDB$].Iotype, [PointsDB$].Subtype, [PointsDB$].Notes " & _
"FROM [LoadsToPointsDB$] INNER JOIN [PointsDB$] ON [LoadsToPointsDB$].PointRef = [PointsDB$].ItemRef " & _
"WHERE [LoadsToPointsDB$].LoadRef = " & moditemref & " AND [PointsDB$].SystemComp = 'Y' " & _
**"AND NOT EXISTS (SELECT * FROM [TempPoints$] WHERE [TempPoints$].PointItemRef = [PointsDB$].ItemRef)"**
Thank you for those that viewed this, but I seem to have found the answer.
There is a previous insert into statement that loads data in the table, once the data was being loaded into it, the table was storing the numbers as text and not integers. After removing the rows and ensuring the format of the columns was set to integer the SQL query began to function as expected.
I have a table which is all VarChar() fields.
I fill the dataset with the following SQL (using vb.net):
Select * From archive_naerns90_2006_q3.NAERNS90_Calls Where siteid = 'NAERNS90-02-627303'
I get this error:
Input string was not in a correct format.Couldn't store <02-627303> in agencyid Column. Expected type is Double.
When I run this query from PgAdmin it works fine. I am running this same bit of code on dozens of other tables with no problems.
The value 02-627303 referenced in the error is the value in the agencyid column for that record, but there are no Double fields in the table. Full disclosure - there were two, but I changed them from Double to VarChar and neither were the agencyid coplumn.
The other kicker is this only happens on some records in that table.
I am guessing I have some corrupt table. I copied the table, but the copy has the same problem. And Since it works in PgAdmin but not from my code it also may have something to do with npgsql.
Edit: I was asked to add the code around the problem area as this looked like an Insert error. I am not doing any Inserts at all in this function.
SQL = "Select * From " & MlocRow("currentdb") & "." & SubTablesArr(Y) & " Where siteid = '" & DataRow("siteid") & "'"
Adapter.SelectCommand = New NpgsqlCommand(SQL, MLConnect)
Adapter.Fill(subDT)
My ConnectionString is:
"HOST=192.168.0.133;DATABASE=masterlists;USER ID=myuser;PASSWORD=mypassword"
Thanks,
Brad
You can type cast that col to varchar() explicitly...
When I run the following query:
INSERT INTO outRawTbl
SELECT *
FROM (select * from [out|noRFI_BS_noRT]
union all
select * from [out|noRFI_BS_RT]) AS [%$###_Alias];
I get this error:
Microsoft Access set 3854 field(s) to Null due to a type conversion failure
When I run the query as:
INSERT INTO outRawTbl
SELECT *
FROM (select * from [out|noRFI_BS_noRT]
union
select * from [out|noRFI_BS_RT]) AS [%$###_Alias];
then I don't get this type conversion error, and all the data inserts successfully. I need to use UNION ALL because some of the fields in the queries are Memo fields and they'll be truncated to 255 chars if I use UNION.
I guess I can start trying to insert fields one at a time from the queries, but there are around 50 fields in each query and that will take a lot of time. Is there a quick way to find out which field is causing the problem with UNION ALL?
EDIT:
Solved and uncovered another problem. I took Gordon's idea of running the queries as separate INSERT operations instead of using UNION ALL. I then iterated through each field in the query, doing a separate insert to find the field that was causing the conversion error, using this code:
Sub findProblemField()
Dim qdf As QueryDef
Dim sql As String
Dim fld As Field
For Each qdf In CurrentDb.QueryDefs
If InStr(qdf.Name, "out|") Then
For Each fld In qdf.Fields
sql = "insert into outrawtbl select top 1 " & _
"[" & qdf.Name & "].[" & fld.Name & "] from [" & qdf.Name & "]"
CurrentDb.Execute sql, dbFailOnError
Next
End If
Next
End Sub
This led me to discover that one of the fields that is a string in the SELECT query is a Date/Time in the destination field, and a blank string value is what's throwing the error. Working on resolving this now...
I have no idea why MS Access would get a failure for union all but not for union. It should be producing the same data. I also don't know why a memo field would be truncated in one case, but not the other.
However, the easiest way to solve your problem with union all is to do two inserts:
INSERT INTO outRawTbl
select * from [out|noRFI_BS_noRT];
insert into outRawTbl
select * from [out|noRFI_BS_RT];