您可以將現場的控制源設置爲一個函數名。該函數可以輕鬆地執行你的SQL,和/或傳入一個變量。這是我的簡單鍋爐板,用於執行SQL語句到記錄集並返回第一個值的函數。在我的世界中,我通常會包含一個非常具體的where子句,但是您肯定可以使任何此功能在您的需求中更加健壯。
=fnName(sVariable, iVariable)
Public Function fnName(_
sVariable as String, _
iVariable as Integer _
) As String
On Error GoTo Err_fnName
Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
sSQL = ""
Set con = Access.CurrentProject.Connection
Set rst = New ADODB.Recordset
rst.Open sSQL, con, adOpenDynamic, adLockOptimistic
If rst.BOF And rst.EOF Then
'No records found
'Do something!
Else
'Found a value, return it!
fnName = rst(0)
End If
rst.Close
Set rst = Nothing
con.Close
Set con = Nothing
Exit_fnName:
Exit Function
Err_fnName:
Select Case Err.Number
Case Else
Call ErrorLog(Err.Number, Err.Description, "fnName", "", Erl)
GoTo Exit_fnName
End Select
End Function
是否有某些原因數據不應該包含在表單的基礎記錄源中? – 2009-08-21 21:54:55