In this video you will find out how to load a table into a recordset object, loop through the records, and vba calculate 2 fields. Also you’ll find out how to make a user defined function and call it from a query.
Need to know how to insert into a table? See this video – MS Access SQL
'======================================================== 'BY LOEBLCOM SERVICES 2012 'ERIK LOEBL(713)409-7041 'EMAIL: erik@loeblcomservices.com 'WEB: http://loeblcomservices.com '======================================================== Option Compare Database Sub loop_products() 'in this example, we simply load the Products table into a & _ recordset object, and loop through the table. Dim rst As Object Dim strSQL As String strSQL = "SELECT UnitPrice,UnitsInStock FROM Products" Set rst = CurrentDb.OpenRecordset(strSQL) rst.MoveFirst Do Until rst.EOF Debug.Print rst.Fields("UnitsInStock") rst.MoveNext Loop End Sub Sub loop_products_and_calculate() 'in this example, we simply load the Products table into a & _ recordset object, and loop through the table calculating 2 fields, & _ UnitPrice * UnitsInStock Dim rst As Object Dim strSQL As String Dim dblResult As Double strSQL = "SELECT UnitPrice,UnitsInStock FROM Products" Set rst = CurrentDb.OpenRecordset(strSQL) rst.MoveFirst Do Until rst.EOF dblResult = rst.Fields("UnitPrice") * rst.Fields("UnitsInStock") Debug.Print "The result = " & dblResult rst.MoveNext Loop End Sub Function calculate_this(num1, num2) As Double 'in this example, we simply calculate the 2 passed numbers Dim dblResult As Double dblResult = num1 * num2 calculate_this = dblResult End Function
You can download the database file, by clicking here.
How To Escape Apostrophe In SQL Update Query
If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
How can I interact with other Office applications (Excel) using VBA in Access?
Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]
How To Create A Parameter Query In Access
A parameter query changes your ordinary static access query to be more dynamic and interactive. It will ask you a question about what you want to search for, allowing you to do a search query multiple times instead of just once. You can do your parameter query straight from the QBE (Query By Example) Editor, […]