473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to copy data of same row to another table using INSERT INTO?

Hi friend,

I have a table named Hire Purchase details and another one named ScheduleT. In the table Hire Purchase Details I have a field named "HDCapital" and in the "ScheduleT" I have field named capital. A schedule of payment will be created and then payment will be received afterwards.

I have a button to record New payment in the table "ScheudleT". On click the following code is executed "Private Sub Command156_Click()".

It works fine. But I need to copy the "Capital" on the same row that payment is received from the table "ScheduleT" to the table "Hire Purchase details" field "HDCapital"


Expand|Select|Wrap|Line Numbers
  1. Private Sub AmountPaid_AfterUpdate()
  2.  
  3.    If AmountPaid > AmountDue Then
  4.         RegularPayment = AmountDue
  5.         ExtraPayment = AmountPaid - AmountDue
  6.     Else
  7.         RegularPayment = AmountPaid
  8.         ExtraPayment = 0
  9.     End If
  10.  
  11.  
  12.     DoRecalc
  13.  
  14. End Sub

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command11_Click()
  2.  
  3.     If MsgBox("Are you SURE? This will erase all payment data and create a new schedule?", vbYesNoCancel) <> vbYes Then Exit Sub
  4.  
  5.     If IsNull(Forms![Customers Hire Purchase Add]!AmountDue) Then
  6.         MsgBox "Credit amount should not be null"
  7.         Exit Sub
  8.     End If
  9.  
  10.     DoCmd.SetWarnings False
  11.     '/////////////////// This will insert Rs 0 In Hire Purchase Details
  12.     Dim S As String
  13.     Dim P As Currency
  14.     Dim D As Date
  15.  
  16.  
  17.  
  18.     S = InputBox("Monthly Instalment", "Payment", 0)
  19.  
  20.     If S = "" Then Exit Sub
  21.     P = CCur(S)
  22.  
  23.     S = Nz(InputBox("Date", "Date", Date), 0)
  24.     If S = "" Then Exit Sub
  25.     D = CDate(S)
  26.  
  27.     DoCmd.SetWarnings False
  28.     DoCmd.RunSQL "INSERT INTO [Hire Purchase Details] (CreditInvoiceNo,Amount,InstalmentDate,PaymentType,Cashier) " & _
  29.     "VALUES (" & Forms![Customers Hire Purchase Add]!CreditInvoiceNum & "," & P & ", Date(),'Cash',[TempVars]![CurrentUserID])"
  30.  
  31.     DoCmd.SetWarnings True
  32.  
  33.     '//////////////////// end
  34.  
  35.  
  36.     DoCmd.RunSQL ("DELETE * FROM ScheduleT WHERE LoanID=" & Forms![Customers Hire Purchase Add]!CreditInvoiceNum)
  37.     Me.Requery
  38.     DoCmd.GoToControl "MonthSN"
  39.  
  40.     Dim BBal As Currency
  41.     Dim Counter As Long
  42.     Dim CurDate As Date
  43.     Dim X As Integer
  44.     Dim TotalPrin As Currency, Correction As Currency
  45.  
  46.     BBal = Forms![Customers Hire Purchase Add]!ChargeablePrice
  47.     Counter = 1
  48.     CurDate = Forms![Customers Hire Purchase Add]![Date of Sales]
  49.  
  50.     While BBal > 0
  51.         DoCmd.GoToRecord , , acNewRec
  52.         MonthSN = Counter
  53.         Counter = Counter + 1
  54.         DueDate = CurDate
  55.         CurDate = DateAdd("m", 1, CurDate)
  56.         AmountPaid = 0
  57.         RegularPayment = 0
  58.         ExtraPayment = 0
  59.         OpeningBalance = BBal
  60.         AmountDue = Forms![Customers Hire Purchase Add]!MonthlyInstalment
  61.         Charges = OpeningBalance * (Forms![Customers Hire Purchase Add]!InterestRate / 12)
  62.         Capital = AmountDue - Charges
  63.         If BBal < Forms![Customers Hire Purchase Add]!MonthlyInstalment Then
  64.             ClosingBalance = 0
  65.             TotalPrin = DSum("Capital", "ScheduleT", "LoanID=" & Forms![Customers Hire Purchase Add]!CreditInvoiceNum) + Capital
  66.             Correction = Forms![Customers Hire Purchase Add]!ChargeablePrice - TotalPrin
  67.             AmountDue = AmountDue + Correction
  68.             Capital = Capital + Correction
  69.         Else
  70.             ClosingBalance = OpeningBalance - Capital
  71.         End If
  72.         BBal = ClosingBalance
  73.     Wend
  74.  
  75.    Me.Requery
  76.  
  77.     DoCmd.SetWarnings True
  78.  
  79. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command19_Click()
  2.  
  3.     DoRecalc
  4.  
  5. End Sub
  6.  
  7. Public Sub DoRecalc()
  8.  
  9.     On Error Resume Next
  10.     Dim BBal As Currency, TotalPrin As Currency, TotalExtra As Currency
  11.     BBal = Forms![Customers Hire Purchase View and Edit]!ChargeablePrice
  12.  
  13.  
  14.     ' clear existing calculations
  15.     Me.Requery
  16.     DoCmd.SetWarnings False
  17.     DoCmd.RunSQL "UPDATE ScheduleT SET Capital=0 WHERE LoanID=" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum
  18.  
  19.     DoCmd.GoToControl "MonthSN"
  20.     DoCmd.GoToRecord , , acFirst
  21.     While Not IsNull(MonthSN)
  22.         OpeningBalance = BBal
  23.         If OpeningBalance = 0 Then
  24.             AmountDue = 0
  25.             Capital = 0
  26.             Charges = 0
  27.             ClosingBalance = 0
  28.         Else
  29.             AmountDue = Forms![Customers Hire Purchase View and Edit]!MonthlyInstalment
  30.             Charges = OpeningBalance * (Forms![Customers Hire Purchase View and Edit]!InterestRate / 12)
  31.             Capital = AmountDue - Charges
  32.             If BBal < Forms![Customers Hire Purchase View and Edit]!MonthlyInstalment Then
  33.                 ClosingBalance = 0
  34.                 TotalPrin = DSum("Capital", "ScheduleT", "LoanID=" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum) + Capital
  35.                 TotalExtra = DSum("ExtraPayment", "ScheduleT", "LoanID=" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum)
  36.                 Correction = Forms![Customers Hire Purchase View and Edit]!ChargeablePrice - TotalPrin - TotalExtra
  37.                 AmountDue = AmountDue + Correction
  38.                 Capital = Capital + Correction
  39.  
  40.             Else
  41.                 ClosingBalance = OpeningBalance - Capital - ExtraPayment
  42.             End If
  43.             BBal = ClosingBalance
  44.         End If
  45.  
  46.         DoCmd.GoToRecord , , acNext
  47.  
  48.     Wend
  49.  
  50.     Me.Requery
  51.     DoCmd.SetWarnings True
  52.  
  53.  
  54. End Sub
  55.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command156_Click()
  2. DoRecalc
  3. End Sub
  4.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command32_Click()
  2.  
  3.     Dim S As String
  4.     Dim P As Currency
  5.     Dim D As Date
  6.     Dim K As Currency
  7.     Dim L As Currency
  8.  
  9.     S = InputBox("The Monthly Instalment is ROUNDED to as a Whole. E.g. Rs 1423.15 will be Rounded to as Rs 1424.", "Payment", -Int(-Forms![Customers Hire Purchase View and Edit]!MonthlyInstalment), 0)
  10.  
  11.     If S = "" Then Exit Sub
  12.     P = CCur(S)
  13.  
  14.     S = Nz(InputBox("Date", "Date", Date), 0)
  15.     If S = "" Then Exit Sub
  16.     D = CDate(S)
  17.  
  18.  
  19.     ' Add to Hire Purchase Details
  20.     DoCmd.SetWarnings False
  21.  
  22.     DoCmd.RunSQL "INSERT INTO [Hire Purchase Details] (CreditInvoiceNo,Amount,InstalmentDate,PaymentType,Cashier) " & _
  23.     "VALUES (" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum & "," & P & ", Date(),'Cash',[TempVars]![CurrentUserID])"
  24.  
  25.     DoCmd.SetWarnings True
  26.     Me.Requery
  27.  
  28.     ' Find first unpaid item in Schedule of Payment
  29.     DoCmd.GoToControl "MonthSN"
  30.     DoCmd.GoToRecord , , acFirst
  31.     While RegularPayment >= AmountDue
  32.         DoCmd.GoToRecord , , acNext
  33.     Wend
  34.  
  35.     If IsNull(MonthSN) Then
  36.         MsgBox "No payment to add to"
  37.         Exit Sub
  38.     End If
  39.  
  40.     AmountPaid = AmountPaid + P
  41.     AmountPaid_AfterUpdate
  42.  
  43.     K = DSum("AmountDue", "ScheduleT", "LoanID=" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum)
  44.     L = DSum("RegularPayment", "ScheduleT", "LoanID=" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum)
  45.  
  46.    If (K - L) <= 0 Then
  47.    DoCmd.RunSQL "Update ([Hire Purchase Customers]) set AccStatus = 'Account Settled'" & _
  48.    "Where ([Hire Purchase Customers].[AccStatus])='Instalment' and ([Hire Purchase Customers].CreditInvoiceNum)= " & Me.LoanID & ""
  49.  
  50.     MsgBox "Account Settled"
  51.  
  52.  
  53.     End If
  54.  
  55.  
  56. End Sub
  57.  
I'm sorry for my english.

Please can you help. Thanks
Nov 10 '14 #1
2 1173
Seth Schrock
2,965 Expert 2GB
The query would follow the form of
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [Hire Purchase Details] (HDCapital)
  2. SELECT [Field Name]
  3. FROM ScheduleT
  4. WHERE Your Criteria here
Nov 11 '14 #2
I tried to write the code but can't figure it out.
Expand|Select|Wrap|Line Numbers
  1.     ' Add to Hire Purchase Details
  2.     DoCmd.SetWarnings False
  3.  
  4.     DoCmd.RunSQL "INSERT INTO [Hire Purchase Details] (CreditInvoiceNo,Amount,InstalmentDate,PaymentType,Cashier) " & _
  5.     "VALUES (" & Forms![Customers Hire Purchase View and Edit]!CreditInvoiceNum & "," & P & ", Date(),'Cash',[TempVars]![CurrentUserID])"
  6.  
  7.  
  8.  
  9.    DoCmd.RunSQL "INSERT INTO [Hire Purchase Details] (HDCapital)" & _
  10.     "Values(Capital)" & _
  11.     "FROM ScheduleT" & _
  12.     "Where ([AmountPaid])=" & Me.AmountPaid & ""
  13.  
  14.     DoCmd.SetWarnings True
  15.     Me.Requery
Can you write it for me?
Nov 12 '14 #3

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

Similar topics

0
by: Chris Nighswonger | last post by:
------=_NextPart_000_0013_01C352C0.6B0A6E30 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi All, Is it possible in MySQL to use UPDATE to update...
1
by: Otto Blomqvist | last post by:
Hello ! I have two tables with identical schema. I want to copy all data from Table A to Table B, but table B might already have some of table A:s data (each record is identified using...
2
by: pattie | last post by:
I hava been trying to add data into a table using INSERT into unsuccessfully.Doing it manually works,and all other constructs works like SELECT,UPDATE.So what might be a cause. My syntax ...
2
parshupooja
by: parshupooja | last post by:
Hi, I have table which has 2 columns username password i want to insert username values from by copying data from another table whereas password shd be randomly generated Thank You
1
by: yasinirshad | last post by:
HI, Can anyone tel me How to copy data from a table in one database to a table in another database using sqlserver2005. Thanks in advance.
1
by: cathycros | last post by:
Hi, I'm trying to take data from varchar fields in one table and copy it to Nvarchar fields in another table. (Long story - now dealing with multiple languages, not enough space in row in current...
1
by: Aaitaman Tamang | last post by:
All, Any Help to solve my problems? I have been trying add some more table using below VBA codes but still i have not got any ides how to do this. As you all can see below...
4
by: kisho | last post by:
i have created on table Customer which have column as primary key and IDENTITY(10000000,1). What i want is how can i insert values for the table using INSERT INTO Customers values(<values>) query...
0
by: Javed Jahangiri | last post by:
I want to import Excel File Data in oracle table using C#. I have fetched the data from excel file but getting problem on transfering this data into oracle table.Below is my code: string filpath =...
10
by: Joe Y | last post by:
My Access DB handles product formulation and specification. In the formula form, many text boxes, such as Cost or ingredient statement, are calculated fields based on each ingredient data in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.