访问VBA:如何获得连续的形式记录的自动编号如何获得、形式、编号、VBA

2023-09-09 21:23:02 作者:兮妤

我有一个视图类型连续纸,即基于查询返回的记录的形式,我想记录有像1,2,3号标签。在列出的顺序:

有什么办法来产生一个标签或文本框会自动出现?

任何帮助AP preciated!谢谢

编辑:继承人的查询code:

 昏暗的QD作为DAO.QueryDef
    设置QD = CurrentDb.CreateQueryDef(pairsOrdered,SELECT * FROM allPairs为了通过计数递减)

    昏暗的RS作为DAO.Recordset
    设置RS = CurrentDb.OpenRecordset(pairsOrdered)
    如果不RS.EOF然后
        Me.Label22.Visible = 0
    结束如果

    如果Me.OpenArgs =25然后
        记录源=选择前25 *从pairsOrdered
    结束如果

    如果Me.OpenArgs =50然后
        记录源=选择前50 *从pairsOrdered
    结束如果

    如果Me.OpenArgs =全部,
        记录源=选择pairsOrdered *
    结束如果
 

解决方案

如果您的表包含一个数字主键,你可以使用DCOUNT()在查询中生成一个序列号。在这个例子中,id是一个数和主键。

  SELECT DCOUNT(*,MyTable的,ID< =&放大器; [M]。[ID])作为sequence_num,m.id,m.some_text
FROM MyTable的为M
ORDER BY DCOUNT(*,MyTable的,标识&其中; =与& [米] [ID]);
 
VBA小白的福音 如何在EXCEL中实现连续编号自动更新打印

您可以更改ORDER BY指现场前pression的序号值的字段列表,而不是满场的EX pression,如果preFER。

  ORDER BY 1;
 

无论哪种方式,查询应该产生一个可编辑的记录源的形式,但显然,你不能直接编辑sequence_num ...所以设置控件绑定到它的启用=否的属性和/或锁定=是。

您可以使用子查询,而不是DCOUNT EX pression为sequence_num,但你会得到一个只读的记录。

此方法不需要一个数字主键,但你需要一个唯一的字段或字段的组合,它是独一无二的。你也可以用文字或日期字段做。

修改:如果您在窗体中删除行,做Me.Requery形式的更新后确认事件。如果添加你的记录结束前要显示的行,做Me.Requery话,太。如果您对现有行编辑的唯一字段(S)值不变。

I have a form of view type "continuous forms" that is returning records based on a query, and I want the records to have a number label like 1, 2, 3.. in the order listed:

Is there any way to generate a label or textbox automatically there?

Any help is appreciated! Thanks

EDIT: Heres the query code:

    Dim qd As DAO.QueryDef
    Set qd = CurrentDb.CreateQueryDef("pairsOrdered", "select * from allPairs order by Count desc")

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("pairsOrdered")
    If Not rs.EOF Then
        Me.Label22.Visible = 0
    End If

    If Me.OpenArgs = "25" Then
        RecordSource = "select top 25 * from pairsOrdered"
    End If

    If Me.OpenArgs = "50" Then
        RecordSource = "select top 50 * from pairsOrdered"
    End If

    If Me.OpenArgs = "all" Then
        RecordSource = "select * from pairsOrdered"
    End If

解决方案

If your table includes a numeric primary key, you can use DCount() in your query to generate a sequence number. In this example, id is a number and the primary key.

SELECT DCount("*","MyTable","id<=" & [m].[id]) AS sequence_num, m.id, m.some_text
FROM MyTable AS m
ORDER BY DCount("*","MyTable","id<=" & [m].[id]);

You can change the ORDER BY to refer to the field expression's ordinal value in the field list, rather than the full field expression, if you prefer.

ORDER BY 1;

Either way, the query should produce an editable record source for your form, though obviously you won't be able to edit the sequence_num directly ... so set the properties of the control you bind to it as Enable=No and/or Locked=Yes.

You could use a subquery instead of the DCount expression for sequence_num, but then you will get a read-only recordset.

This approach does not require a numeric primary key, but you do need a unique field or combination of fields which is unique. You can also do it with text or date fields.

Edit: If you delete rows in the form, do Me.Requery in the form's After Update Confirm event. If you add a row which you want displayed before the end of the recordset, do Me.Requery then, too. Same if you edit values in the unique field(s) for existing rows.