Please Help for VBA compare 2 tables add new and edit (1 Viewer)

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
hi,,
whould you please help on Please Help for VBA compare 2 tables add new and edit
i have 2 tables tamptable and table1 ,i need
1..add new on table1 when nomatch
2..if match then qty+qty in table1
3.delete datds in temptable.

thank you you all

KHT
 

Attachments

  • _Update and del.accdb
    1.3 MB · Views: 321
  • 2021-12-23_144635.jpg
    2021-12-23_144635.jpg
    184.3 KB · Views: 312
Last edited:

June7

AWF VIP
Local time
Yesterday, 19:22
Joined
Mar 9, 2014
Messages
5,466
Shouldn't save aggregate calculation. Should just enter raw data records and do calcs with query or report design.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:22
Joined
May 7, 2009
Messages
19,229
you only need to create 2 querys (one for insert and the other for update).
see the two queries in design view.
run your form.
 

Attachments

  • _Update and del.accdb
    1.3 MB · Views: 307

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
hi...arnelgp
thank you very much again..
but its has some promble
i have try
first step. when table1 no data..it run probably
second step. i keep 1 old record (date and lotid are same) and add 1 new record in temptable
it cannot sumup the same record
because same date ,same lotid cant dupicate in table1
the result need LOTID666, INdate 04/12/2021 total=12 in 1 record
please ref to attached..
 

Attachments

  • 2021-12-23_184947.jpg
    2021-12-23_184947.jpg
    74.6 KB · Views: 261
  • 2021-12-23_184905.jpg
    2021-12-23_184905.jpg
    38.2 KB · Views: 283

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:22
Joined
May 7, 2009
Messages
19,229
did you first Create an Index on Table1 (index On LotID, No Duplicate)?
 

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
the thing is eg.. 0n 1/1/2021 user input :ID=123; indate=1/1/2021 Qty=2.. and run update temptable is empty
on 2/1/2021 user input again :ID=123; indate=1/1/2021 Qty=2.. and ID=124; indate=2/1/2021 Qty=3 and run update
so need sumup 1/1/2021
 

June7

AWF VIP
Local time
Yesterday, 19:22
Joined
Mar 9, 2014
Messages
5,466
Did you even consider comment in post 2? Saving calculated aggregate data is poor design. There is no data support for the value in table. If user is entering data then save that as a record. Do the summary calculation in query or report. Life will be a lot simpler and less painful.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 11:22
Joined
May 7, 2009
Messages
19,229
you need to run 2 queries.
one the insert query, to insert new LotID (if it does not exists, if it already exists don't worry it
wont get inserted again, as long as you create the Index on LotID Without Duplicate).

then you run the Update query.
 

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
@arnelgp..
attached is the sample
thank you
 

Attachments

  • _Update and del.accdb
    1.4 MB · Views: 306

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
Did you even consider comment in post 2? Saving calculated aggregate data is poor design. There is no data support for the value in table. If user is entering data then save that as a record. Do the summary calculation in query or report. Life will be a lot simpler and less painful.
thank you for reply
I am really sorry, I am not very understanding how to do it.
 

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
you need to run 2 queries.
one the insert query, to insert new LotID (if it does not exists, if it already exists don't worry it
wont get inserted again, as long as you create the Index on LotID Without Duplicate).

then you run the Update query.
need delete Itemexists otherwise the sum will be wrong.
i am using below 4 steps complicated but work.
whould you please help to simplify?


Private Sub Command1_Click()
'1..Markdown Itemexists(Itemexists = Yes in TempTable).
CurrentDb.Execute "UPDATE TempTable LEFT JOIN Table1 ON (TempTable.InDate = Table1.InDate) AND (TempTable.LotID = Table1.LotID) SET TempTable.Itemexists = Yes WHERE Table1.LotID Is Not Null AND Table1.QTY Is Not Null"
'2..INSERT INTO Table1 that items notexists
CurrentDb.Execute "INSERT INTO Table1 ( LotID, InDate, QTY ) SELECT TempTable.LotID, TempTable.InDate, TempTable.QTY FROM TempTable WHERE TempTable.Itemexists=No"
'3..Delete items notexists(after INSERT left Itemexists for sumup )
CurrentDb.Execute "DELETE TempTable.*, TempTable.Itemexists FROM TempTable WHERE TempTable.Itemexists =No"
'4..Sumup Itemexists(if any)
If DCount("*", "temptable") > 0 Then
Dim rst As Object
Dim rst1 As Object
Set rst = New ADODB.Recordset
rst.ActiveConnection = CurrentProject.Connection
rst.Open "temptable", , adOpenKeyset, adLockOptimistic
Set rst1 = New ADODB.Recordset
rst1.ActiveConnection = CurrentProject.Connection
rst1.Open "table1", , adOpenKeyset, adLockOptimistic
rst.MoveFirst
Do Until rst.EOF
rst1.MoveFirst
rst1.Filter = "LotID = " & rst!LotID & " AND InDate = #" & rst!InDate & "#"
rst1!qty = Nz(rst!qty, 0) + Nz(rst1!qty, 0)
rst1.Update
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
rst1.Close
Set rst1 = Nothing
End If
MsgBox "Complet"
End Sub
 

uncleh

Member
Local time
Today, 11:22
Joined
Mar 1, 2020
Messages
31
Shouldn't save aggregate calculation. Should just enter raw data records and do calcs with query or report design.
you meane use temptable for calculation directly? i afraid lots of record will slowdown the calculat
 

Users who are viewing this thread

Top Bottom