簡體   English   中英

VBA訪問運行SQL檢查行數

[英]VBA access run sql check row count

我試圖根據sql查詢的結果在窗體上顯示一個字段。 由於我不知道vba的訪問權限,因此我很掙扎,也不知道我要去哪里。 幫助將不勝感激。

Dim RecordSt As Recordset
Dim dBase As Database
Dim stringSQL As String

Set dBase = CurrentDb()

stringSQL = "SELECT * FROM Table1 WHERE ID = 2"

DoCmd.RunSQL (stringSQL)

If RecordSt.Fields.Count > 0 Then
Me.Other.Visible = True
Else
Me.Other.Visible = False
End If

您的代碼中有很多問題。

首先,Docmd.RumSQL(stringSQL)不返回任何內容,並且它與您的記錄集RecordSt不相關。

另外,RecordSt.Fields.Count將計算表的FIELDS而不是所選RECORDS的數量。

這是使用ADODB的解決方案。 您可能必須在VBA編輯器的“工具”->“參考”菜單中添加對ADO的引用(如果沒有6.1,則可以選擇其他版本):

在此處輸入圖片說明

Dim rst As new ADODB.Recordset
Dim stringSQL As String

stringSQL = "SELECT * FROM Table1 WHERE ID = 2"

rst.Open SQL, CurrentProject.AccessConnection
If rst.RecordCount > 0 Then
    Me.Other.Visible = True
Else
    Me.Other.Visible = False
End If
If DCount("*", "table1", "id = 2") > 0 Then
   Me.Other.Visible = True
Else
   Me.Other.Visible = False
End if

甚至更快:

Me.Other.Visible = (DCount("*", "table1", "id = 2") > 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM