2009-08-21 47 views
8

是否有任何方式使用SQL填充Access窗體的文本字段的值?SQL作爲Access窗體的控制源字段

我讀過,它不可能簡單地輸入SQL作爲控制源。這是真的?

感謝任何HALP :)

- 編輯 -

我需要執行此查詢;

SELECT tblCaseIssues.IssueDesc FROM tblCaseIssues INNER JOIN tblCaseNewHS_Issues ON tblCaseIssues.ID = tblCaseNewHS_Issues.IssueID WHERE(tblCaseNewHS_Issues.HS_ID = 81)) 
+0

是否有某些原因數據不應該包含在表單的基礎記錄源中? – 2009-08-21 21:54:55

回答

9

很肯定這是真的SQL,但你可以使用的功能:
=DLookUp("field_name","table_name","any_fieldname = 'value'")

+0

是否有可能做一些如 SELECT tblCaseIssues.IssueDesc 複雜FROM tblCaseIssues INNER JOIN tblCaseNewHS_Issues ON tblCaseIssues.ID = tblCaseNewHS_Issues.IssueID WHERE(tblCaseNewHS_Issues.HS_ID = 81) 具有這樣的功能? – Jambobond 2009-08-21 13:40:05

+2

在Access中創建一個所有表都已加入的查詢。然後在你的DBLookup中,你可以用查詢的名字替換table_name(對不起,表示一個表,假設查詢也可以)。注意:在VBA中有更好的方法。 – JeffO 2009-08-21 14:22:29

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 
1

這可能是最容易使用的一個組合框和行來源設爲您的查詢,或者,DAO是原產於訪問。

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 
2

只要把你的sql查詢並保存爲查詢。

在文本框中

然後,就地方:

=(使用DLookup(「IssuesDesc」,」查詢的名稱」))

我在一個相當大的損失,所有這些海報暗示的敲敲罷了代碼在哪裏根本不需要。只需將您的sql保存爲查詢,然後使用dlookup()函數作爲文本框的數據源,即可完成。

+0

由於第一個答案已經提出了您的解決方案,但OP仍在尋求一種方法,他可以利用SQL語句作爲查詢的來源。 – 2009-08-22 17:35:40

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 
+1

最好包含一些解釋/上下文,而不是隻有代碼的答案。 – EJoshuaS 2016-12-22 16:01:11

+1

歡迎來到Stack Overflow!雖然這段代碼可能會回答這個問題,但爲什麼和/或代碼如何回答這個問題提供了額外的背景,這提高了它的長期價值。不鼓勵使用僅有代碼的答案。 – Ajean 2016-12-22 17:00:43

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()函數作爲文本框的控制源,爲什麼不寫一個查詢函數並將該函數用作控制源?」試試看,它解決了我的情況。

相關問題