Create New Blank Record In a Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimrand
    New Member
    • Feb 2008
    • 10

    Create New Blank Record In a Form

    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:

    Code:
    Private Sub cmdNew_Click()
            
            Dim dbsVehicle_Sales As DAO.Database
            Dim rsttblAECarAuctionDisposals As DAO.Recordset
            
            Set dbsVehicle_Sales = CurrentDb
            Set rsttblAECarAuctionDisposals = dbsVehicle_Sales.OpenRecordset("tblAECarAuctionDisposals", dbOpenDynaset)
            
            With rsttblAECarAuctionDisposals
                .AddNew
                    ![Registration] = strStringVariable
                    ![Tag] = strStringVariable
                    ![VAT Status] = strStringVariable
                    ![Vehicle Description] = strStringVariable
                    ![Buyer] = strStringVariable
                    ![Date Sold] = dteDateVariable
                    ![Sale Price (Gross)] = curCurrencyVariable
                    ![VAT] = curCurrencyVariable
                    ![Net Proceeds] = curCurrencyVariable
                    ![Mileage] = lngLongVariable
                    ![Year/Letter] = strStringVariable
                .Update
            End With
            
    
    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?
  • Khriskin
    New Member
    • Feb 2008
    • 20

    #2
    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 (strStringVaria ble, 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! ^_^

    Comment

    • jimrand
      New Member
      • Feb 2008
      • 10

      #3
      Ok, thanks. How do I get the button to open a pop up form for them to enter a new record?

      Comment

      • Khriskin
        New Member
        • Feb 2008
        • 20

        #4
        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! ^_^

        Comment

        • jimrand
          New Member
          • Feb 2008
          • 10

          #5
          Originally posted by Khriskin
          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....

          Comment

          • blad3runn69
            New Member
            • Jul 2007
            • 59

            #6
            DoCmd.GoToRecor d , , acNewRec

            Comment

            • jimrand
              New Member
              • Feb 2008
              • 10

              #7
              Originally posted by blad3runn69
              DoCmd.GoToRecor d , , acNewRec
              That's what I wanted - thanks!! Realise it's crude, but it does what I need.

              Comment

              • Khriskin
                New Member
                • Feb 2008
                • 20

                #8
                Originally posted by jimrand
                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! ^_^

                Comment

                • jimrand
                  New Member
                  • Feb 2008
                  • 10

                  #9
                  Originally posted by Khriskin
                  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!!

                  Comment

                  Working...