Blank record being added in addition to completed one

MCCDOM

Registered User.
Local time
Today, 06:51
Joined
Oct 30, 2014
Messages
84
Hi There,

I have a form which I use to add stock to the stock table. It uses the following code to do this:
Code:
Private Sub btnAdd_Click()

    Dim iCtr As Integer
    'Calculates how many entries to put into the table based on value in txtQty
    For iCtr = 1 To Me.txtQty
    'Adds all fields into the stock table
        CurrentDb.Execute "INSERT INTO Stock (Item, Make, Model, Cost, DatePurchased, WarrantyPeriod) VALUES ('" & Me.txtItem & "','" & Me.cboMake & "','" & Me.cboModel & "','" & Me.txtCost & "','" & Me.txtDate & "','" & Me.cboWarranty & "')"
    Next
    'Clears all data fields
    Me!txtItem = Null
    Me!cboMake = Null
    Me!cboModel = Null
    Me!txtCost = "0.00"
    Me!txtQty = Null
    Me!txtDate = Null
    Me!cboWarranty = Null
    
    Me.cboMake.Requery
    Me.cboModel.Requery
    
End Sub
This code on its own works without issues. I have since then added a query that summaries the contents of my stock table and shows me only the unique record and gives a quantity of how many there are. For example I have 5 keyboards in my stock table each with a unique ID. This query shows me a table showing only one row for keyboards but gives a value in the quantity column of 5. For some reason this query effects my above add stock code and causes a blank record to be added into the stock table evrytime I add stock. Why might this be please and how do I prevent it? The SQL for my summary query is as follows:
Code:
SELECT Stock.Item, Stock.Make, Stock.Model, Count(Stock.ID) AS Quantity
FROM Stock
GROUP BY Stock.Item, Stock.Make, Stock.Model;

Kind regards,

Dom
 
Why might this be please and how do I prevent it?

Your form is already bound to the stock table. Entering values into the controls adds data to the table through the form automatically.
Your INSERT INTO is creating a duplicate record. (Add a unique key to your table.)
Clearing the form controls clears the values from the original (bound) record, leaving the blank record.


Delete the 'recordsource' property for the form and 'control source' for each control.
 
Thanks static. Clearing all the 'record source' and 'control source' properties has done the trick.
Many thanks,

Dom
 

Users who are viewing this thread

Back
Top Bottom