Access VBA Open Record Set w/ Select Statement

Clete

Board Regular
Joined
Sep 5, 2014
Messages
62
I have a table called TotalInterest with a field call TotInt. In my code below I am trying to take the value currently in TotInt and add to it the value in YESTERDAYS_INT_Calc from the table Daily Interest. Also, Im not sure I somehow have to loop this as there are many files I am tracking interest for.

Everything seems to work except for the lines in red below.

Thanks for any help

Private Sub Form_Open(Cancel As Integer)
Dim maxDate As Date
Dim x As Currency
Dim y As Currency
Dim z As Currency
maxDate = DMax("SystemDate", "SystemDate")
If maxDate = Date Then
Else
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO SystemDate (SystemDate) VALUES (Date());"
DoCmd.SetWarnings True
DoCmd.OpenQuery ("Daily Interest")
x = CurrentDb.OpenRecordset("select YESTERDAYS_INT_Calc from [Daily Interest]")(0)
y = CurrentDb.OpenRecordset("select TotInt from TotalInterest")(0)
z = x + y

CurrentDb.Execute ("insert into TotalInterest (TotInt) values (" & z & ")")
End If
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How are the highlighted lines not working?
 
Upvote 0
The lines are indeed working... but now I need to set this up on some kind of loop that only adds the value z to the TotInt field of the correct FileNo. FileNo is a unique number in both tables.
 
Upvote 0
go here
Microsoft Access Update Query Examples, SQL Syntax, and Errors
see the sections named
Update with Values from Fields in Another Table
and
Update Using its Own Value


it sounds like you want to take something like this


Code:
select 
   DI.YESTERDAYS_INT_Calc as x, 
   TI.TotInt as y,  
   DI.YESTERDAYS_INT_Calc + TI.TotInt as z
from 
  [Daily Interest] as DI
  inner join 
  TotalInterest as TI
  on 
  DI.FileNo  = TI.FileNo
and turn it into an update statement
 
Upvote 0
Im trying this now with a series of queries.... this is the last one which will give me what I need. When I execute this code in access I get an error saying "Operation must use an updateable query"

Code:
UPDATE TOTALINTEREST INNER JOIN TotalInterestQuery ON [TotalInterestQuery].[v] = TotalInterest.FileNo SET TotInt = TotalInterestQuery.z
WHERE TOTALINTEREST.FileNo = TotalInterestQuery.v
;
 
Upvote 0
Ive also written another query I could call at anytime into my code that does work it's called TotalInterestQuery.

It gives me the field total interest that I want to move into TotInt in the TotalInterest Table
 
Upvote 0

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top