473,406 Members | 2,867 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,406 software developers and data experts.

looping through a query field by field..

Hey guys,
I have an assignment to do which is as follows: First I am given some stock price data, I made a table for it tha stores the data, but the I want to caclulate the returns of these stock prices(each stock price is in a field, and my ID is the date)..the idea is to use just a nested for loop that goes through each each row for a specific company(field) calculates the return and the moves to the next company and does the same task.. I know that in Excel it is just a command but I am assigned to do it in Access and I simply dont know VBA.. Any help would be strongly appreciated.. Thanks!
Jan 21 '16 #1

✓ answered by jforbes

Sure, you can write a function that opens up a RecordSet and loops through it. MSDN has an example of looping a RecordSet: https://msdn.microsoft.com/en-us/lib.../ff820966.aspx

But again, Set based process will probably work better for you in the long run. Especially since it sounds like you are grouping the records by Customer and performing some math on the Group. This is a good example of Set Based Aggregates: https://support.office.com/en-us/art...4-8c8dbbe41c8a

16 5324
i dont know but a proper pseudocode should be like that:

Expand|Select|Wrap|Line Numbers
  1. go to the specific database
  2. go to this query which is based on the table prices
  3. for i=2 to length(size(query,2)%loop through the columns (i dont care about the dates)
  4. for j=2 to length(size(query,1)&loop through the rows
  5. returns(i,j)=log((prices(i,j)/prices(i-1,j-1)
  6. end
  7. end
  8. show me the modified query
Jan 21 '16 #2
Also something else which should be correct but again I dont know how:
First I made just a table with the companies and I passed the unique ID. Then I made the table of the stock prices so it is a time series so it is like date and the fields are the companies. But I would like to say that the name of the fields which is for me 1,2,3 Etc are the IDs from the table companies. I was looking if there is a possibility for a composite key this one that will have the date and the company ID. Guys I appreciate for the help!!
Jan 21 '16 #3
jforbes
1,107 Expert 1GB
Fair warning, your post will most likely get removed as this site is more about real world examples than doing someone's homework for them.

But before then, if I were you, I would spend some time in the Query Editor to try to get a good grasp on what it is about. You are looking too closely at the problem and you are trying to write procedural program to do the heavy lifting, while Access Queries are set based. This might help, http://sqlmag.com/t-sql/programming-sql-set-based-way
Jan 21 '16 #4
jforbes thanks for the remark..Actually I am in an internship so it is not directly homework, but thank you for pointing out. But cant I write a funtion in the VBA editor that takes as an input the table prices and returns the query?
Also I see the recordset command where you ho through each record. But in time series data you need to go through each field. Is there an equivalent fieldset?
Jan 21 '16 #5
jforbes
1,107 Expert 1GB
Sure, you can write a function that opens up a RecordSet and loops through it. MSDN has an example of looping a RecordSet: https://msdn.microsoft.com/en-us/lib.../ff820966.aspx

But again, Set based process will probably work better for you in the long run. Especially since it sounds like you are grouping the records by Customer and performing some math on the Group. This is a good example of Set Based Aggregates: https://support.office.com/en-us/art...4-8c8dbbe41c8a
Jan 21 '16 #6
well this is exactly my problem.. everyone who uses access they talk about customers orders etc which is cross section data...So the record idea is useful. For me it is not! because at each field I have the companies and I dont care about the record which in my case it is the stock price of the companies at each particular day..
Jan 21 '16 #7
Rabbit
12,516 Expert Mod 8TB
It sounds like your data is not normalized, which is why you think you need to use VBA and loop through every row and every column.

But it's most likely not necessary at all. It's very unclear what you are trying to accomplish, because it sounds like you already arrived at what solution you want to implement rather than describing the issue and letting us guide you to the optimal solution.
Jan 21 '16 #8
Thanks Rabbit for the reply. Well I am familiar with Matlab for instance, and I know that if I have to calculate some stuff and I have a matrix I have to loop to the matrix etc... Well Matlab is handy when it comes to matrices. Now I really dont know what is optimal really because I dont know what lies behind VBA. I dont know even the difference between SQL and VBA (Sorry for that) so yeah, I just approach the problem based on Matlab

I can describe the problem once more. I created a table with companies that have unique ID and another table of daily stock prices that span 5 years. My total goal is to create an index but this is for later.. Anyhow I want to create a query that maipulates the table of the stock prices. I need to calculate the returns for the stock prices. For instance say stock A closed at t=1 at 20$ and at t=2 is at 30$ then the return is 50%. This is what I want to do basically for all the rows(time) for all the columns(companies) The thing is that I am uaware of Access VBA.. The employer told me that first I have to set up the database and optimize it and the to create the index. So basically I have to do everything in Access and Excel. And by the way the data matrix is huge so the OPTIMIZATION is important for them.
And of course they told me that when data enter the database so the stock prices for instance are updated, the query should display the results. I just got the job and I try to read as much as possible to understand how to do it and they gave me a deadline for the step 1 which is the database construction so yeah...And the hilarious part is that this was not part of the agreement but yeah I am an intern...
Jan 21 '16 #9
guys if you have a table or how do you go through each row? and if you want to relate a certain cell in the field with the previous one how do you do it?
Jan 21 '16 #10
Rabbit
12,516 Expert Mod 8TB
It would help if you posted sample data along with results.
Jan 22 '16 #11
Hey Rabbit,
Sorry for being a bit late for replying but was trying to get familiar with VBA. VBA looks beautiful and I really want to get better! Now with respect to the project here I have some sample data of 10 companies with their prices downloaded from datastream. I have to calculate the returns in a query. My boss told me that we should insert the data in a proper database because Excel cannot handle such a big bunch of data. So as to do the index they cover more than 250 companies out of which we pick the best according to some financial metrics. Now I am in the step again where I put the raw data (prices) in the database, where I created a table. The second step is to do a query that calculates the returns but as you know I am stuck there. Check out how the data looks like and if you have any recommendations what to do so as to make a query you are welcome!! At first sight if you see the data along each row we have the prices of 10 companies for a specific date. And as I told you it is really weird because I should specify the key and I don't know whether the companies ID is my best option since it is a time series.Assume that the keys is 1 up until 10! Really if you can propose and guide me it would be great. Learning from pros would make my learning curve steeper and my stress will go away hopefully. Thank you again all you guys for possible recommendations'! The results are next to the prices! it is actually the logarithm of the prices of the next divided by the logarithm of the prices of the previous day basically!
Attached Files
File Type: xlsx sampleData.xlsx (372.3 KB, 282 views)
Jan 26 '16 #12
Hey guys,
I have a question any hint would be greatly appreciated. I set up a table with the stock prices of my companies and then I did a query that takes the fields date prices and now I am up to calculating the returns. My code though does not work: I did the following: I tried to go through each cell of the field prices and then I calculated the differences of the logarithms of the prices but then my result I tried to append it as a new field in the query and it told me that the query is not updatable. So I try to overcome the problem by coding a function that takes as an input a field from my query and the output is a new field with the returns of the stock prices! then I can just append the field to my query. Is it wise or not? I dont have experience in Access so I am not sure if that would be correct. Any help would be greatly appreciated! thank you for your time and assistance!
Feb 4 '16 #13
Here is the code that i try to work on it. However it says that there is a run time error 3219 because there is an invalid operation in the query. I try to put the result in the newly created field of my query. Here is the code hope it helps to get what I mean
Feb 4 '16 #14
Expand|Select|Wrap|Line Numbers
  1. Public Sub lists()
  2.  
  3.     CalcReturns
  4.  
  5.  
  6.  
  7. End Sub
  8.  
  9. Public Function CalcReturns(Optional RequiredFld As Field) As Field
  10. Dim db As DAO.Database
  11.  
  12. Dim qdf As DAO.QueryDef 'use this for a query
  13. Dim rs As DAO.Recordset
  14. Dim fld As DAO.Field
  15. Dim temp As Double
  16. Dim result As Double
  17. Dim temp2 As Double
  18. Set db = CurrentDb
  19. Set qdf = db.QueryDefs("ReturnsQry")
  20. Set rs = qdf.OpenRecordset
  21. Set RequiredFld = db.QueryDefs("ReturnsQry").Fields("CompanyPrice")
  22. Set CalcReturns = db.TableDefs("tblPrices").Fields("CompanyReturns")
  23.  
  24. With rs
  25.  
  26. Do Until rs.EOF
  27.  
  28. For Each fld In rs.Fields
  29. Debug.Print fld.Name
  30.  
  31. If fld.Name = "CompanyPrice" Then
  32. temp = fld.Value
  33. rs.MoveNext
  34. temp2 = fld.Value
  35. .AddNew
  36. CalcReturns.Value = Log(temp2 / temp)
  37. .Update
  38. Debug.Print CalcReturns.Value
  39. rs.MoveNext
  40. DoCmd.OpenQuery "ReturnsQry"
  41. End If
  42. Next
  43.  
  44. Loop
  45.  
  46. End With
  47. rs.Close
  48. db.Close
  49. DoCmd.OpenQuery "ReturnsQry"
  50. Set rs = Nothing
  51. Set fld = Nothing
  52. Set qdf = Nothing
  53.  
  54. Set db = Nothing
  55. End Function
Feb 4 '16 #15
when it goes to :
Expand|Select|Wrap|Line Numbers
  1. CalcReturns.Value = Log(temp2 / temp)
it throws an error! any help would be greatly appreciated!
Feb 4 '16 #16
zmbd
5,501 Expert Mod 4TB
jakehey1: Please check your Bytes.com Inbox. I've sent you a copy of my resources boilerplate.
You simply must master the basics contained therein in order to effectively use Access.
Feb 6 '16 #17

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

Similar topics

3
by: WC Justice | last post by:
I need to build an UPDATE statement that copies the values of roughly 40 fields from a table that stores standard or default values into a table of specific contracts. There are 8 or so fields in...
3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
5
by: TheSho | last post by:
Overview: Each Preventative Maintenance (PM) check has to have a running log of the days that it was completed. For each date, I need to know how many days it took since the last check. I will...
0
by: MLH | last post by:
I have an A97 query (qryVehiclesNowners2) that has a table field in it named . Depending on the selections made in a number of criteria choices on a form, a field on the form will have string...
11
by: MLH | last post by:
I have a query field... Description: & "2-tone" When I run it, the output in that field is empty. If I change the name to MyDescription - it works fine. I noticed the query field has a...
6
by: Laetitia Clark | last post by:
Is it possible to force a Decimal type field from an ODBC source to become a Double type field in the destination table when using a Make Table query ? Thanks in advance. Laetitia South...
3
by: sbowman | last post by:
I'm trying to get a query in access to give me a parsed string if the field contents are not null and the value from another field if the field contents are null. I have the following in the...
8
by: Lewe22 | last post by:
I have a basic query which is selecting all information from another query . As soon as i set the query to show the totals (in order to perform a sum) all information held in a field named is...
1
by: cwoll | last post by:
I have a table with 5 different fields. ID Field Name = This field refers to land not a field in access. 2007 Crop 2008 Crop 2009 Crop I now have a query that just gives me "Field Name" and...
1
by: Cole LI | last post by:
I am trying to use union query to combine 3 queries, all with same fields and size, the only difference are the field status field Qry1 = = Actual Qry2 = = Missing Qry3 = = Pending the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.