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

Create New Blank Record In a Form

P: 10
Hi

Using Access 2007, I have a form created that uses a combo box to select records from an underlying table. I want the user to be able to create a new record with absolutely no pre-set data in it to allow them to key in the new data themselves. I am really stuck as to how to do this.

I have added a cmd button to the form and have tried using the following code that I picked up from here:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdNew_Click()
  2.  
  3.         Dim dbsVehicle_Sales As DAO.Database
  4.         Dim rsttblAECarAuctionDisposals As DAO.Recordset
  5.  
  6.         Set dbsVehicle_Sales = CurrentDb
  7.         Set rsttblAECarAuctionDisposals = dbsVehicle_Sales.OpenRecordset("tblAECarAuctionDisposals", dbOpenDynaset)
  8.  
  9.         With rsttblAECarAuctionDisposals
  10.             .AddNew
  11.                 ![Registration] = strStringVariable
  12.                 ![Tag] = strStringVariable
  13.                 ![VAT Status] = strStringVariable
  14.                 ![Vehicle Description] = strStringVariable
  15.                 ![Buyer] = strStringVariable
  16.                 ![Date Sold] = dteDateVariable
  17.                 ![Sale Price (Gross)] = curCurrencyVariable
  18.                 ![VAT] = curCurrencyVariable
  19.                 ![Net Proceeds] = curCurrencyVariable
  20.                 ![Mileage] = lngLongVariable
  21.                 ![Year/Letter] = strStringVariable
  22.             .Update
  23.         End With
  24.  
  25.  
  26. End Sub 


When I execute, it tells me run-time error 3058; Index or primary key cannot contain a Null value.

Registration is my primary key.

Can anyone help please?
Feb 14 '08 #1
Share this Question
Share on Google+
8 Replies


Khriskin
P: 20
I'm a little confused as to what you are trying to accomplish. As a disclaimer, I work in Access 2000 and 2003, so 2007 may have features I don't know about. However, the code you included in the post doesn't seem to do what you are looking for.

The code is setup to create a new record within the table, but it does so using variables that are not defined (strStringVariable, dteDateVariable, etc). Thus all of the variables default to NULL, which spawns the 3058 error you are getting. Also, you set dbsVehicle to be a copy of your current database, but then never use it in the code.

If you are just trying to let the user enter a new record, it might be easier to have the button open a pop-up form that is set to Data Entry. This would give the user a blank record to fill in, as well as allow you some control over the input validation.

Hope that helps! ^_^
Feb 14 '08 #2

P: 10
Ok, thanks. How do I get the button to open a pop up form for them to enter a new record?
Feb 14 '08 #3

Khriskin
P: 20
You'd need to create the form first, then you can use the DoCmd.OpenForm to open it, just like an ordinary form. Make sure you set the pop-up form to Pop Up (opens in front of other windows), Modal (keeps focus until closed), and Data Entry (new record entry only).

Also, you'll need to do some basic error trapping on the pop-up form to make sure the users don't close it before all the information is gathered (as well as put in valid information).

How familiar are you with creating forms? How access-savvy are your target users? This may be something you can create with the built-in Form Wizards, but it may also require some coding.

Hope that helps! ^_^
Feb 14 '08 #4

P: 10
You'd need to create the form first, then you can use the DoCmd.OpenForm to open it, just like an ordinary form. Make sure you set the pop-up form to Pop Up (opens in front of other windows), Modal (keeps focus until closed), and Data Entry (new record entry only).

Also, you'll need to do some basic error trapping on the pop-up form to make sure the users don't close it before all the information is gathered (as well as put in valid information).

How familiar are you with creating forms? How access-savvy are your target users? This may be something you can create with the built-in Form Wizards, but it may also require some coding.

Hope that helps! ^_^
Right, I already have my form created and I am attempting to use it as both a form to display data, but also to allow input. Would I be better to use one form to display the data that is locked and have a second form for input that is only displayed when the users select the "New Record" button?

I have only been doing this for a short space of time and as you can probably guess I am having to learn as I go along!!

As far as error trapping goes, that's going to be something new for me as well, but I suppose the sort of thing you refer to is to ensure that all relevant fields are completed before the form allows them to exit? I'll give it a go and if/when I get stuck, there'll be a new post!

Thanks for your help....
Feb 15 '08 #5

P: 59
DoCmd.GoToRecord , , acNewRec
Feb 15 '08 #6

P: 10
DoCmd.GoToRecord , , acNewRec
That's what I wanted - thanks!! Realise it's crude, but it does what I need.
Feb 15 '08 #7

Khriskin
P: 20
Right, I already have my form created and I am attempting to use it as both a form to display data, but also to allow input. Would I be better to use one form to display the data that is locked and have a second form for input that is only displayed when the users select the "New Record" button?
Sorry about that! I misunderstood what you were trying to do, I thought the main form you were talking about was for displaying data only, not that you also wanted to use it for data entry. (Doh!)

Glad you found something that works! ^_^
Feb 15 '08 #8

P: 10
Sorry about that! I misunderstood what you were trying to do, I thought the main form you were talking about was for displaying data only, not that you also wanted to use it for data entry. (Doh!)

Glad you found something that works! ^_^
No worries, I think it was much more me misleading you by trying to use code (in the first post) that was clearly not understood by me!!!

Anyway, thanks for trying to help despite me confusing the heck out of it!!
Feb 15 '08 #9

Post your reply

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