By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,870 Members | 2,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,870 IT Pros & Developers. It's quick & easy.

Add Record to Subform from Main Form - Getting Null Error on PK

P: 3
I added an "Add Record" button to my main form. The Embedded Macro successfully creates a new record on the subform.

When I press any key any field I get the following error:
"You tried to assign the Null value to a variable that is not a Variant data type".

When I click OK I can proceed, but why am I getting this error? What can I do to remove it (either with VBA or within the Embedded Macro)?

Thank you in advance!
Nov 20 '13 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 5K+
P: 5,397
};-) SNIDE ANSWER: Give the variable a value or change the data type - (you did see the smile yes?! Macros, YUCK)


Ahh the wonderful hell of Macros. This is one reason most of us avoid them when possible - the error messages (if you get one) are almost useless. Sort of like telling a person they can't breath water when they're drowning... true, but not really helping!

In ACC2010 there is an error traping method for macros, I've played with it some, and it could be better (still not helping that swimmer get out of the water).
With that said... you can attempt to single step thru the macro:
SingleStep Macro Action ACC2010 (click Here)
With this being an embedded macro it may be a hit or miss for working.
If this works, you should find the the code that is causing the error... hopefully a simple fix, if not, take note of the code that caused the error and then:

As I said above, this is telling you that one of the parmeters that was created is not getting a value - most likely refering to an empty control on your form; however, without the actual macro-code, we're not going to be able to help you with either MC or VBA

HOWEVER, all isn't lost:

If you are using ACC2010
Open your form in design.
Right click on the control that has you embedded macro.
Events tab
On Click event - click on the [...] button
select all of the text in shown in the Macro Editor
<ctrl><c> to copy
come back to this thread.
Click on the [CODE/] button in the toolbar
place the cursor between the inserted [code] tags
Your macro text will now be pasted into the post.
(if you found the code from the single step where the error occured, please note which line it is located)
We'll go from there

IF you are using ANY other version of Access. You will have to open the macro and then cross type the information from the macro editor into the post here between the [code] tags.
Nov 20 '13 #2

P: 3
Thanks zmbd. I'm on Access 2007 so I just typed it out below. You're right that macros are a pain, if there's an [Event Procedure] I can run instead I'd be glad to start there.

Expand|Select|Wrap|Line Numbers
  1. Condition
  2. Action: "OnError"
  3. Arguments: "Next, "
  5. Condition
  6. Action: "GoToRecord"
  7. Arguments: ", , New, "
  9. Condition: "[MacroError]<>0"
  10. Action: "MsgBox"
  11. Arguments: "=[MacroError].[Description], Yes, None, "
Nov 21 '13 #3

Expert Mod 5K+
P: 5,397
Just to verify that what should be there is there I recreated your MC in ACC2010
(and a reason for you to move to ACC2010 - XML encoded macros - :)

You could actually take the following XML, copy, open the macro editor in ACC2010 and paste it in!
Still - YUCK)

The reason I did this was just make sure that there wasn't anything missing.... it's been a long time since I had to mess with macros:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-16" standalone="no"?>
  2. <UserInterfaceMacros xmlns="">
  3.    <UserInterfaceMacro For="Command11" Event="OnClick">
  4.       <Statements>
  5.          <Action Name="OnError"/>
  6.          <Action Name="GoToRecord">
  7.             <Argument Name="Record">New</Argument>
  8.          </Action>
  9.          <ConditionalBlock>
  10.             <If>
  11.                <Condition>[MacroError]&lt;&gt;</Condition>
  12.                <Statements>
  13.                   <Action Name="MessageBox">
  14.                      <Argument Name="Message">=[MacroError].[Description]
  15.                      </Argument>
  16.                   </Action>
  17.                </Statements>
  18.             </If>
  19.          </ConditionalBlock>
  20.       </Statements>
  21.    </UserInterfaceMacro>
  22. </UserInterfaceMacros>
This matches what you have...
So now we need to take a very careful look at all of the remaing structure of your form.

Somewhere, you have some code that is expecting a value and when you move to the new record, the value is unset and the code is tossing the error; however, there isn't any error trapping in place for that code.

Hopefully there are only one or two places where there isn't any error trapping, if not, then you may have to hunt and peck. I would start with whatever control had focus, then the form's on current event, after that - add error trapping to all of the underlying code.

This is why EVERY code block I write will always have at minimum:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3. '(...)
  4. Sub somename()
  5.    '(...)
  6.    On Error GoTo zerrtrap
  7.    '(...)
  8. zcleanup:
  9.    '(...)
  10. Exit Sub
  11. zerrtrap:
  12.    MsgBox "error# Sub_[somename] " & Err.Number & vbCrLf & "error: " & Err.Description & vbCrLf & "source: " & Err.Source
  13.     Resume zcleanup
  14. Exit Sub
Yes, this the actual basic template I use with the exception of the first two lines, I have these set at the VBE level. Yes, at least to start with, even my one and two "action" line codes will have error trapping... especially if it will be ran using a "runtime" environment. (The production error code may include specfic code to handle certain errors, to send me an email, or even to record the error to a table or offline text file). Feel free to modify and use my template.
Nov 21 '13 #4

P: 3
Wow, thanks! Now when I create a new record, it does so successfully but my PK field gets populated with an existing value (the first PK value in my table).

The subform shows two records, both the existing record with that PK and the new record with just the PK filled in.
Nov 21 '13 #5

Expert Mod 15k+
P: 31,489
I'm not clear what you're reporting here. Are you saying that Z's post was extremely helpful, but that you're not quite there yet? You need more help to get it to work?
Nov 21 '13 #6

Expert Mod 5K+
P: 5,397
Different problem... new thread
However, similar solution, you are going to have to go back in to the design of either the form, the query (if used), and/or the table.

PK fields, from the start, at the table level should be set to prevent a duplicated value from the start - if you didn't do this then you are in for some tough times.
Nov 21 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.