2016-09-26 1 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

あなたの答えをありがとう。私は後で使うためにそれを私の心に記します。今私は私の方法でOKです。私の方法は1.あなたのアイデアと同じクエリー+自動番号フィールド(ID)の見出しを持つ空白のテーブルを作成します。 2.クエリを実行し、クエリが作成されたテーブルに追加されます。私はこのプロセスにフォームを使用しました。フォームのロード時に、テーブルの内容が削除され、クエリが実行され、空白のテーブルに追加されます。それから私はテーブルを使用します。テーブルに自動番号フィールドを追加していただきありがとうございます。 –

関連する問題