如何在查询数据旁边添加序号
Posted
技术标签:
【中文标题】如何在查询数据旁边添加序号【英文标题】:How to add sequential numbers next to data from query 【发布时间】:2020-06-23 00:23:46 【问题描述】:我有一个名为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
这总是导致我的 SEQ 编号显示在数据下方,即使它是新创建的字段。
如何在我的数据旁边简单地添加 1-30 的序列号?或者至少如何为我拥有的数据添加序列号?
(也许?)有没有办法遍历每一行(逐行)复制到一个新表,然后在那里创建一个 SEQ 编号并重复 30 次?即使我需要逐个字段逐行执行此字段,我也不在乎。
例如,如果我需要简单地去,我可以:
1 - Bikes
2 - Food
3 - Money
4 - Shoes
5 - Computers
6 - Clothes
7 - Soda
但是我如何像这样逐行进行并保持从一开始就有的顺序?有循环过程吗?
初始记录顺序很重要,需要维护。
【问题讨论】:
没错,在 Access 查询中并不简单,但在文本框具有 RunningSum 属性的 Access 报告中很简单。如果要保存到表中,则需要 UPDATE 操作 SQL(而不是 INSERT)或如您所指示的,将记录写入另一个表。如果您需要维护原始记录顺序,则需要在创建记录时输入一个序列值。为什么不使用自动编号字段?为什么表中的记录数会上下波动? 【参考方案1】:我想我已经找到了自己问题的解决方案......但我还没有测试它,因为我现在在家并且没有我的工作电脑。
假设我创建了一个名为 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
【讨论】:
【参考方案2】:您可以有一个外部函数,将表单作为参数传递。
实现起来非常简单。研究内联 cmets:
' 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 的一部分,您会在其中找到许多其他枚举行的方法,每种方法都有一些优点和缺点。
【讨论】:
以上是关于如何在查询数据旁边添加序号的主要内容,如果未能解决你的问题,请参考以下文章