Access VBA recordets - updating a field based on the result of a function that uses other fields as input

StackOverflow https://stackoverflow.com/questions/18938979

  •  29-06-2022
  •  | 
  •  

Question

I have a simple_table with 4 fields: a,b,x,P

I am trying to update the field p based on the output of a function that uses the other fields as input parameters. In this case the function is an excel function. I was using SQL server but really need to access some statistical functions. So yesterday I opened access for the first time. Eeek. I've spent the last day trying to learn vba and following various tutorials on recordsets.

The bit I'm struggling with is how to I update a the P field based on the other fields? In a loop?

Thanks very much.

Dim objExcel As Excel.Application
Set objExcel = CreateObject("Excel.Application") 
'Test it works
MsgBox objExcel.Application.BetaDist(0.4, 2, 5)
'OK, that works :)

'set up the ADO stuff
Dim cnn1 As ADODB.Connection
Dim MyRecordSet As New ADODB.Recordset

Set cnn1 = CurrentProject.Connection
MyRecordSet.ActiveConnection = cnn1

'Load data into MyRecordSet
MySQLcmd = "SELECT * FROM simple_table"
MyRecordSet.Open MySQLcmd

'HELP WITH THE NEXT BIT PLEASE!
'Some kind of loop to go through the recordset to set the field P
' equal to the result of the excel function betadist(x,a,b)
'I imagine looping through something like the following semi pseudo code ???  
    myRecordSet.Fields(“P”).Value = objExcel.Application.BetaDist(myRecordSet.Fields(“x”).Value, myRecordSet.Fields(“a”).Value, myRecordSet.Fields(“b”).Value)
'end of the loop

objExcel.Quit
Set objExcel = Nothing
MyRecordSet.Close
cnn1.Close
Set MyRecordSet = Nothing
Set cnn1 = Nothing
Was it helpful?

Solution

Since your code works with "Dim objExcel As Excel.Application", that means you have a reference set for the Excel object library. In that case, you don't need a full Excel application instance in order to use the BetaDist function. You can set an object variable to Excel.WorksheetFunction and call the function as a method of that object. However, I don't know whether that makes a significant difference. I didn't test the CreateObject("Excel.Application") alternative.

In this sample, I used a DAO recordset instead of ADO. The reason is I've found DAO can be significantly faster with native Access (Jet/ACE) data sources. You can switch to ADO if you prefer, but I don't see an advantage.

Notice I opened the table directly rather than via a query. The DAO dbOpenTable option can also benefit performance.

With those details out of the way, it's just a simple matter of looping through the recordset, calling the function with values from the current row, and storing the function's result in the P field ... pretty much what you outlined in your pseudo-code. :-)

Dim objWFunction As Object ' Excel.WorksheetFunction
Dim MyRecordSet As DAO.Recordset
Dim db As DAO.database

Set objWFunction = Excel.WorksheetFunction ' Excel reference required
Set db = CurrentDb
Set MyRecordSet = db.OpenRecordset("simple_table", dbOpenTable)
With MyRecordSet
    Do While Not .EOF
        'Debug.Print objWFunction.BetaDist(!x, !a, !b)
        .Edit
        !p = objWFunction.BetaDist(!x, !a, !b)
        .Update
        .MoveNext
    Loop
    .Close
End With

Set MyRecordSet = Nothing
Set db = Nothing
Set objWFunction = Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top