如何在查询数据旁边添加序列号

vx6bjr1n  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(271)

我有一张table叫 tblFriends :
tblFriends 由查询生成 qryFriends . 中的数据和记录数 tblFriends 每天都有变化,但从不超过30。
我想在每个名字旁边生成序列号,但这似乎非常困难。
我已尝试循环插入查询,如下所示:

strSQLaddSEQ = "ALTER TABLE tblFriends ADD SEQ Number;"
DoCmd.RunSQL strSQLaddSEQ

For SEQNum = 1 To 30

   strSqlSEQNum = "INSERT INTO Friends (SEQ) Values(" & SEQNum & ");"
   DoCmd.RunSQL strSqlSEQNum

Next SEQNum

这总是导致我的序列号显示在数据下面,即使它是一个新创建的字段。
如何在数据旁边添加1-30之间的序列号?或者至少我该如何为我拥有的数据添加一个序列号?
(也许?)有没有一种方法可以循环每一行(逐行)复制到一个新表,然后在那里创建一个序列号并重复30次?即使我需要一行一行地做这个,我也不在乎。
例如,如果我只需要去:

1 - Bikes
2 - Food
3 - Money
4 - Shoes
5 - Computers 
6 - Clothes
7 - Soda

但是我怎么才能像这样一排一排地走,并且保持我从一开始就有的顺序呢?是否存在循环过程?
初始记录顺序很重要,需要维护。

fbcarpbf

fbcarpbf1#

我想我找到了解决我自己问题的方法…但是我还没有测试它,因为我现在在家,没有我的工作电脑。
假设我创建了一个名为tblseqnames的表,其中包含字段“names”和“seq”。

Dim rs As Recordset

Dim strNames As String

Set rs = CurrentDb.OpenRecordset("qryFriends", dbOpenDynaset, dbSeeChanges)

strSQL = "INSERT INTO tblSeqNames (Names, SEQ) VALUES ('" & strNames & "', " & SEQNum & ")"

For SEQNum = 1 To 30

    strNames = rs.Fields("Name").Value

        DoCmd.RunSQL strSQL

    rs.MoveNext

Next SEQNum
yquaqz18

yquaqz182#

可以有一个外部函数,将窗体作为参数传递。
实现起来非常简单。研究在线评论:

' Creates and returns a sequential record number for records displayed
' in a form, even if no primary or unique key is present.
' For a new record, Null is returned until the record is saved.
'
' Implementation, typical:
'
'   Create a TextBox to display the record number.
'   Set the ControlSource of this to:
'
'       =RecordNumber([Form])
'
'   The returned number will equal the Current Record displayed in the
'   form's record navigator (bottom-left).
'   Optionally, specify another first number than 1, say, 0:
'
'       =RecordNumber([Form],0)
'
'   NB: For localised versions of Access, when entering the expression, type
'
'       =RecordNumber([LocalisedNameOfObjectForm])
'
'   for example:
'
'       =RecordNumber([Formular])
'
'   and press Enter. The expression will update to:
'
'       =RecordNumber([Form])
'
'   If the form can delete records, insert this code line in the
'   AfterDelConfirm event:
'
'       Private Sub Form_AfterDelConfirm(Status As Integer)
'           Me!RecordNumber.Requery
'       End Sub
'
'   If the form can add records, insert this code line in the
'   AfterInsert event:
'
'       Private Sub Form_AfterInsert()
'           Me!RecordNumber.Requery
'       End Sub
'
' Implementation, stand-alone:
'
'   Dim Number As Variant
'
'   Number = RecordNumber(Forms(IndexOfFormInFormsCollection))
'   ' or
'   Number = RecordNumber(Forms("NameOfSomeOpenForm"))
'
'
' 2018-09-14. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RecordNumber( _
    ByRef Form As Access.Form, _
    Optional ByVal FirstNumber As Long = 1) _
    As Variant

    ' Error code for "There is no current record."
    Const NoCurrentRecord   As Long = 3021

    Dim Records             As DAO.Recordset

    Dim Number              As Variant
    Dim Prompt              As String
    Dim Buttons             As VbMsgBoxStyle
    Dim Title               As String

    On Error GoTo Err_RecordNumber
    If Form Is Nothing Then
        ' No form object is passed.
        Number = Null
    ElseIf Form.Dirty = True Then
        ' No record number until the record is saved.
        Number = Null
    ElseIf Form.NewRecord = True Then
        ' No record number on a new record.
        Number = Null
    Else
        Set Records = Form.RecordsetClone
        Records.Bookmark = Form.Bookmark
        Number = FirstNumber + Records.AbsolutePosition
        Set Records = Nothing
    End If

Exit_RecordNumber:
    RecordNumber = Number
    Exit Function

Err_RecordNumber:
    Select Case Err.Number
        Case NoCurrentRecord
            ' Form is at new record, thus no Bookmark exists.
            ' Ignore and continue.
        Case Else
            ' Unexpected error.
            Prompt = "Error " & Err.Number & ": " & Err.Description
            Buttons = vbCritical + vbOKOnly
            Title = Form.Name
            MsgBox Prompt, Buttons, Title
    End Select

    ' Return Null for any error.
    Number = Null
    Resume Exit_RecordNumber

End Function

它是我的项目vba.rownumbers的一部分,在这里您可以找到许多其他方法来枚举行,每个方法都有一些优点和缺点。

相关问题