有没有办法使用SQL填充Access Form的文本字段的值?
我已经读过,不可能简单地输入SQL作为控制源。这是真的吗?
感谢任何挫折:)
- 编辑 -
我需要执行此查询;
SELECT tblCaseIssues.IssueDesc FROM tblCaseIssues INNER JOIN tblCaseNewHS_Issues ON tblCaseIssues.ID = tblCaseNewHS_Issues.IssueID WHERE(tblCaseNewHS_Issues.HS_ID = 81))
答案 0 :(得分:9)
很确定这是真正的SQL,但你可以使用这个功能:
=DLookUp("field_name","table_name","any_fieldname = 'value'")
答案 1 :(得分:2)
您可以将字段的控制源设置为函数名称。该函数可以轻松执行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
答案 2 :(得分:2)
最简单的方法是使用组合框并将Row Source设置为您的查询,或者,DAO是Access的原生。
Private Sub Form_Current()
''Needs reference to Microsoft DAO 3.x Object Library
Dim db As Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strResult As String
strSQL = "SELECT ci.IssueDesc FROM tblCaseIssues ci " _
& "INNER JOIN tblCaseNewHS_Issues cni ON ci.ID = cni.IssueID " _
& "WHERE cni.HS_ID = 81"
Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount > 0 Then
Do While Not rs.EOF
strResult = strResult & ", " & rs!IssueDesc
rs.MoveNext
Loop
strResult = Mid(strResult, 3)
Else
strResult = "Not found"
End If
Me.TextBoxName = strResult
End Sub
答案 3 :(得分:2)
只需将您的SQL查询保存为查询即可。
然后在文本框中,放置:
=(dlookup(“IssuesDesc”,“查询名称”))
我对所有这些海报都造成了相当大的损失,这些海报暗示了代码的重击,而根本不需要代码。只需将您的sql保存为查询,然后使用dlookup()函数作为文本框的数据源,您就完成了。
答案 4 :(得分:0)
Private Sub Form_Load()
Me.Text0 = CurrentDb.OpenRecordset("SELECT COUNT(name) AS count_distinct_clients FROM (SELECT DISTINCT name FROM Table1 WHERE subject='Soc') AS tmp;").Fields(0)
End Sub
答案 5 :(得分:0)
我已经创建了以下函数来解决这个问题。 我喜欢这个解决方案,因为您不必处理阻塞导航窗格或长时间变通方法的已保存查询。
Public Function DAOLookup(SQLstatement As String)
'once you are finished with your SQL statement, it needs to be
'formatted for VBA and it also needs to be on one line.
'example, you would set the control source of a text box to the following
'=DAOLookup("Select ls_number FROM FROM ls INNER JOIN ls_sort ON ls.ls_id = ls_sort.ls_id WHERE ls_sort.number =" & forms!frmMenu!combo_sort & ";")
'Please note, this function only work for single column single row sql statements
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset(SQLstatement)
If Not rs.BOF Then rs.MoveFirst
If rs.BOF And rs.EOF Then Exit Function
DAOLookup = rs(0)
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
End Function
这就是我向老板解释的方式。 "您可以使用DLookUp()函数作为文本框的控件源。为什么不编写一个执行查询的函数并将该函数用作控制源?" 试一试,它解决了我的情况。