Solved INSERT INTO NOT WORKIG (1 Viewer)

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
Private Sub Product_ID_AfterUpdate()

INSERT INTO tblPayments ([Product Value])
SELECT [AMOUNT] FROM tblScale

WHERE tbl.Payments.[Product ID] = tblScale.[ID]


End Sub

Please help with this, i have not used Access for many years.
how do i write this correctly?

Many thanks

Sepp
 

June7

AWF VIP
Local time
Yesterday, 17:26
Joined
Mar 9, 2014
Messages
5,475
If you want to select a specific group of records to pull from tblScale, then need to reference field in tblScale for the criteria, not tblPayments. A WHERE clause linking tblPayment.[Product ID] to tblScale.ID makes no sense for an INSERT action. You also have a period between tbl and Payments that should not be there.
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
Thank you very much,

INSERT INTO tblPayments ([Product Value])
SELECT [AMOUNT] FROM tblScale
WHERE tblScale.[ID] = tblPayments.[Product ID]

Is this correct?

Many thanks

Sepp
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
I just entered the above, but,

it still turns red
INSERT INTO tblPayments ([Product Value])
SELECT [AMOUNT] FROM tblScale
WHERE tblScale.[ID] = tblPayments.[Product ID]

Thank you for your help

Sepp

1676188629188.png
 
Last edited:

ebs17

Well-known member
Local time
Today, 03:26
Joined
Feb 7, 2020
Messages
1,949
You have an event procedure (VBA). In this procedure you write a SQL code. The SQL code is not executed simply by being present, you have to define what you want to do with the SQL statement.

Code:
Private Sub Product_ID_AfterUpdate()
   Dim sSQL As String
   sSQL = "INSERT INTO tblPayments ([Product Value])" & _
          " SELECT [AMOUNT] FROM tblScale WHERE tbl.Payments.[Product ID] = tblScale.[ID]"
   CurrentDb.Execute sSQL, dbFailOnError
End Sub
Now that was just the technical execution. The sense and formulation of the append query would have to be discussed separately, also why one inserts a constant statement as SQL statement in VBA instead of calling a stored query.
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
Dear ebs17,

i thank you very much for your kind help, could you please elaborate on your last sentence?

Many thanks

Sepp

further i an error too few parameters

1676190484155.png
 
Last edited:

June7

AWF VIP
Local time
Yesterday, 17:26
Joined
Mar 9, 2014
Messages
5,475
Why is tblScale and tblProducts involved in this action? Normally, records would be pulled from one table filtered by some input from form, as in:

INSERT INTO tblPayments ([Product Value]) " & _
SELECT [Amount] FROM tblScale WHERE tblScale.ID = " & Me.tbxScaleID

And surely you need to populate other fields besides Product Value, such PaymentDate, ProductID, CustomerID. What are you really trying to accomplish with this process?

Suggest you provide sample data either as tables in post or attach file.
 

ebs17

Well-known member
Local time
Today, 03:26
Joined
Feb 7, 2020
Messages
1,949
The code line says: Execute the SQL statement, on error reset everything back to the beginning and report the error. Only with the execution of the SQL statement it is determined that there are errors. Before that, the statement is just plain text.

Now you would have to think about the query. I could now say what all is wrong there. But first you have to talk about what the query is supposed to do, because only from the sense results a useful formulation of the query, and only with this formulation you also implement the necessary syntax.
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
Dear June7,

i enclose a zip file (Mercury.dbs) and the form involved is frmPayments with sfrmPayments
i am selecting a Type from tblScale into tblPayments, and want to update the correct amount also,
the date is the current date, the date until is as follows
"
=IIf([Scale]="4",DateAdd("ww",1,[DATE_PAID]),IIf([Scale]="5",DateAdd("m",1,[DATE_PAID]),DateAdd("yyyy",1,[DATE_PAID])))"

I thank you for helping me, it is many years since a last used ACCESS....

kind regards

Sepp
 

Attachments

  • MERCURY.zip
    592 bytes · Views: 122

June7

AWF VIP
Local time
Yesterday, 17:26
Joined
Mar 9, 2014
Messages
5,475
All you posted is a shortcut to your db, not the actual db.

And it sounds like you want to do an UPDATE not INSERT action.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:26
Joined
Jul 9, 2003
Messages
16,282
The notorious "Insert Into Statement" raises ugly head again!

You might find some useful information in my blog here:-

 
Last edited:

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
All you posted is a shortcut to your db, not the actual db.

And it sounds like you want to do an UPDATE not INSERT action.
SORRY again, here is the file
 

Attachments

  • MERCURY ZIP.zip
    277.5 KB · Views: 75

June7

AWF VIP
Local time
Yesterday, 17:26
Joined
Mar 9, 2014
Messages
5,475
So tblScale are your products?
You have a ProductID of 0 in tblPayments probably because field in table is set with DefaultValue of 0. CustomerID and ProductID should not have default values.

If you want to modify data in existing records, that is an UPDATE, not INSERT. INSERT is to add new records.

You really don't need to have price info in tblPayments. Price can be retrieved by joining tables.

Only reason to save price in tblPayments would be if price can change in future and don't want history to be changed. Price should be saved in record when record is created, not via UPDATE action later.
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
i thank you, so how can i fix it, the person entering should see the correct price
again i thank you for helping me

Many thanks
Sepp
 

June7

AWF VIP
Local time
Yesterday, 17:26
Joined
Mar 9, 2014
Messages
5,475
Combobox for selecting product should be multi-column and display price. Adjust your combobox settings to add Amount field and set for 3 columns and change widths setting. Price will show in dropdown. Then code in AfterUpdate event can populate Product Value. Advise not to use spaces in naming convention. Assign more informative names to objects.

Me!ProductValue = Me.cbxProduct.Column(2)
 

Sepp1945

Member
Local time
Today, 09:26
Joined
Feb 12, 2023
Messages
33
thank you very much, so easy if you know...LOL

Many thanks again

Sepp

PS will fix Naming
Sepp
 

Users who are viewing this thread

Top Bottom