Results 1 to 8 of 8
  1. #1
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49

    You tried to assign the null value to a variable that is not a variant data type

    We searched online for a solution to this error to no avail.

    When we run the "Upsert" code below with tables local to access, tbl537 is updated with values from tblTemp. We do get errors, but changes are reflected.

    Code:
    UPDATE tbl537 
    LEFT JOIN tblTemp ON tbl537.LabID = tblTemp.LabID 
    SET tbl537.LabID = tblTemp.LabID, 
    tbl537.Date = tblTemp.Date, 
    tbl537.PFOS = tblTemp.PFOS;
    We have switched from storing tables in MY SQL online to MS SQL Server in house because performance online was dismal.

    We put tbl537 on the server (dbo_tbl537), as well as tblTemp (dbo_tblTemp)

    When we edit the code to point to the dbo files stored on the server, we get the null error.
    Other than the primary key, everything is set to allow null values.

    We're wondering if the errors we get when we run the code on tables stored locally are related to the null error we get when the tables are stored on MS Server.


    We're also wondering if there is an issue due to the fact that tbl537 has more records than tblTemp.

    Database is attached.

    Thanks for taking a look.
    Attached Files Attached Files

  2. #2
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    232
    Try
    Code:
    UPDATE tbl537 INNER JOIN tblTemp ON tbl537.LabID = tblTemp.LabID SET tbl537.[Date] = tblTemp.Date, tbl537.PFOS = tblTemp.PFOS;
    You can only update related records, so a INNER join will do.
    There is no need to update the ID; the ID exists in tbl537 otherwis you can not relate it to the Temp record.
    Groeten,

    Peter

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    For an upsert query this

    tbl537
    LEFT JOIN tblTemp

    is the wrong way round

  4. #4
    zpy2 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2023
    Posts
    4
    execute sql on link table or pass through to sql server?

  5. #5
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    A couple of questions:

    First off, CJ said I have the statement the wrong way around. We are trying to update tbl537 with values from tblTemp. I'm definitely confused as the above query does just that when executed.
    xps35's code works as well.

    Secondly, we have moved tables to MS Server. If I execute the following code working with tblTemp (local) and dbo_tbl537 (on the server) we get this error:
    "You tried to assign the null value to a variable that is not a variant data type".
    dbo_tbl537 was created by exporting local tbl537.

    I cannot find any differences in the structue between tbl537 and dbo_tbl537.

    Code:
    UPDATE dbo_tbl537 LEFT JOIN tblTemp ON dbo_tbl537.LabID = tblTemp.[LabID] SET dbo_tbl537.LabID = tblTemp.[LabID], dbo_tbl537.[Date] = tblTemp.Date, dbo_tbl537.PFOS = tblTemp.PFOS;
    or
    Code:
    UPDATE dbo_tbl537 INNER JOIN tblTemp ON dbo_tbl537.LabID = tblTemp.LabID SET dbo_tbl537.[Date] = tblTemp.Date, dbo_tbl537.PFOS = tblTemp.PFOS;
    Both of the code snippets above give the null variant error when trying to execute the code on a table located on the server.


    So...

    1) If I do have the code backwards, I am confused as to how to edit the snippet so that it would upsert values from tblTemp to dbo_tbl537.

    2) How do I get rid of the null variant error I get when executing code that points to a table that is on our server.

    Final note: I get the same errors executing xps35's code on local tables as I do when I execute my original code LOCALLY. When I use either snippet with a table stored on the server - null variant error.

    I think it's time to go have a cold craft beer

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    1 just swap the two table names over - left join tblTemp to tbl537
    2 the way you have your join now, you will get nulls if there is no match

    If you don’t believe me, see this link for how to construct an upsert query in access
    https://stackoverflow.com/questions/...ccess#37014004

  7. #7
    chromachem is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Feb 2016
    Posts
    49
    Of course I believe CJ.
    I did look at the link when I first started. I should have looked longer and saved myself some angst.
    It just seems so wrong that when what is wanted is to update the values in tbl537 one references updating tblTemp.
    I edited the code (below) to reflect CJ's input, and it works just fine!
    Then I went back to the link and reread this comment:
    @EmoryLu see PipperChip remark: the code updates a. The trick is that with that construction, if there is no matching row in a, the row will be created. That is the principle of an UPSERT (UPdate or inSERT)

    After thinking about it a while, and looking up what left join does, it makes sense.

    "The LEFT JOIN command returns all rows from the left table, and matching rows from the left table. The result is null from the right side, if there is no match."


    Code:
    UPDATE tblTemp 
    LEFT JOIN dbo_tbl537 ON tblTemp.LabID = dbo_tbl537.[LabID] 
    SET 
    dbo_tbl537.LabID = tblTemp.[LabID], 
    dbo_tbl537.[Date] = tblTemp.Date, 
    dbo_tbl537.PFOS = tblTemp.PFOS;

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    It just seems so wrong that when what is wanted is to update the values in tbl537 one references updating tblTemp.
    it's just syntax/grammar.

    Most developers will default to a left join, but if you want it to 'look right', try using a right join.

    Not tested as an upsert but you can try

    Code:
    UPDATE tbl537 
    RIGHT JOIN tblTemp ON tbl537.LabID = tblTemp.LabID 
    SET tbl537.LabID = tblTemp.LabID, 
    tbl537.Date = tblTemp.Date, 
    tbl537.PFOS = tblTemp.PFOS;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 08-17-2023, 02:33 AM
  2. Replies: 2
    Last Post: 12-31-2018, 07:30 PM
  3. Replies: 6
    Last Post: 11-16-2013, 06:06 PM
  4. Replies: 1
    Last Post: 04-25-2013, 02:52 PM
  5. Replies: 2
    Last Post: 09-27-2010, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums