2016-09-26 2 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. 조회를 실행하고 조회가 작성된 표에 추가됩니다. 이 과정에 양식을 사용했습니다. 양식로드시 테이블 내용이 제거되고 쿼리가 실행되어 빈 테이블에 추가됩니다. 그런 다음 테이블을 사용합니다. 표에 자동 번호 입력란을 추가해 주셔서 감사합니다. –

관련 문제