我有一個simple_table有4場函數的結果: A,B,X,P訪問VBA recordets - 更新字段基於使用其他字段輸入
我試圖更新域p基於使用其他字段作爲輸入參數的函數的輸出。在這種情況下,該函數是一個excel函數。 我正在使用SQL服務器,但確實需要訪問一些統計函數。所以昨天我第一次打開訪問。 Eeek。我花了最後一天嘗試學習vba,並關注記錄集上的各種教程。
我掙扎的一點是如何根據其他字段更新P字段?在一個循環?
非常感謝。
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
請注意VBA代碼中的引號。樣例中的一行使用了類型設置引號......「P」。這將是一個問題。您需要使用標準(ASCII 43)引號來避免麻煩......'「P」' – HansUp