具有新主键(VBA)的重复记录

编程入门 行业动态 更新时间:2024-10-28 19:33:57
本文介绍了具有新主键(VBA)的重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个很大的记录要复制,然后用新版本的表单打开一个带有新主键ID的表单.可以在Access VBA中完成此操作,而不必遍历所有字段来复制数据吗?

I have a very large record that I'm trying to duplicate, then open a form with the new version with a new primary key ID. Can this be done in Access VBA without having to iterate through all the fields to copy the data?

谢谢!

推荐答案

最快和最简单的方法是使用DAO和以下形式的 RecordsetClone :

The fastest and simplest way is to use DAO and the RecordsetClone of the form:

Private Sub cmdDuplicate_Click() Dim rstSource As DAO.Recordset Dim rstInsert As DAO.Recordset Dim fld As DAO.Field If Me.NewRecord = True Then Exit Sub Set rstInsert = Me.RecordsetClone Set rstSource = rstInsert.Clone With rstSource If .RecordCount > 0 Then ' Go to the current record. .Bookmark = Me.Bookmark With rstInsert .AddNew For Each fld In rstSource.Fields With fld If .Attributes And dbAutoIncrField Then ' Skip Autonumber or GUID field. ElseIf .Name = "SomeFieldToPreset" Then rstInsert.Fields(.Name).Value = SomeValue ElseIf .Name = "SomeFieldToExclude" Then ' Leave blank Else ' All other fields. ' Copy field content. rstInsert.Fields(.Name).Value = .Value End If End With Next .Update ' Go to the new record and sync form. .MoveLast Me.Bookmark = .Bookmark .Close End With End If .Close End With Set rstInsert = Nothing Set rstSource = Nothing End Sub

这会将表单从当前记录移到新记录.您可以轻松地对其进行修改以选择新的ID,然后使用新记录打开其他表单.

This moves the form from the current record to the new record. You can easily modify that to pick the new ID and open the other form with the new record.

更多推荐

具有新主键(VBA)的重复记录

本文发布于:2023-10-16 06:55:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!