2016-09-26 75 views

回答

1

對不起,錯過了查詢部分原因不明,所以這裏是一個完全不同的答案......

使用此功能,讀取記錄只有一次,然後一個集合,是在商店的ID查找速度要快得多:

Public Function RowCounter(_ 
    ByVal strKey As String, _ 
    ByVal booReset As Boolean, _ 
    Optional ByVal strGroupKey As String) _ 
    As Long 

' Builds consecutive RowIDs in select, append or create query 
' with the possibility of automatic reset. 
' Optionally a grouping key can be passed to reset the row count 
' for every group key. 
' 
' Usage (typical select query): 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True)); 
' 
' Usage (with group key): 
' SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True)); 
' 
' The Where statement resets the counter when the query is run 
' and is needed for browsing a select query. 
' 
' Usage (typical append query, manual reset): 
' 1. Reset counter manually: 
' Call RowCounter(vbNullString, False) 
' 2. Run query: 
' INSERT INTO tblTemp (RowID) 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable; 
' 
' Usage (typical append query, automatic reset): 
' INSERT INTO tblTemp (RowID) 
' SELECT RowCounter(CStr([ID]),False) AS RowID, * 
' FROM tblSomeTable 
' WHERE (RowCounter("",True)=0); 
' 
' 2002-04-13. Cactus Data ApS. CPH 
' 2002-09-09. Str() sometimes fails. Replaced with CStr(). 
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1. 
' 2008-02-27. Optional group parameter added. 
' 2010-08-04. Corrected that group key missed first row in group. 

    Static col  As New Collection 
    Static strGroup As String 

    On Error GoTo Err_RowCounter 

    If booReset = True Then 
    Set col = Nothing 
    ElseIf strGroup <> strGroupKey Then 
    Set col = Nothing 
    strGroup = strGroupKey 
    col.Add 1, strKey 
    Else 
    col.Add col.Count + 1, strKey 
    End If 

    RowCounter = col(strKey) 

Exit_RowCounter: 
    Exit Function 

Err_RowCounter: 
    Select Case Err 
    Case 457 
     ' Key is present. 
     Resume Next 
    Case Else 
     ' Some other error. 
     Resume Exit_RowCounter 
    End Select 

End Function 

請研究包括您的典型用法的內聯文檔。

+0

謝謝你的回答。但我想要的是「我運行查詢。查詢添加自動編號字段」。我想我無法在查詢的設計視圖中添加自動編號字段。我的意思是像在表中添加自動編號字段。 –

+0

謝謝你的回答。我會在腦海中標記出來供以後使用。現在我可以用我的方法。我的方法是1.創建一個與查詢+自動編號字段(ID)相同標題的空白表作爲您的想法。 2.運行查詢並將查詢追加到創建的表中。我爲這個過程使用了一個表單。在表單加載時,表格內容將被刪除,查詢將運行並附加到空白表格。然後我用桌子。感謝您的想法,在表中添加自動編號字段。 –

相關問題