MS Access VBA等效于Ctrl+'?

发布于 2025-02-04 02:12:28 字数 477 浏览 3 评论 0原文

我有一个艰难的时间如何从我的用户表格上的上一个记录中正确复制特定的字段数据。我没有代码示例可以显示,但我的请求非常简单。

目前,在12个字段中,我有6个经常重复数据。我可以单击并 ctrl +'(“从上一个记录中的同一字段插入值”),它执行我想要的任务。但是,它为任务增加了很多时间。我只想编写VBA代码以对那些特定字段执行该命令。

我无法获得sendkeys工作。 dlast似乎有时会提供随机数据。我觉得这应该是一个非常简单的请求,但是由于某种原因,我没有找到功能解决方案。

I am having a difficult time how to properly copy specific field data from previous records on my user form. I don't have a code sample to show but my request is very simplistic.

Currently, out of 12 fields, I have 6 that I often repeat data. I can click on and press Ctrl+' ("Insert the value from the same field in the previous record") and it performs the task I want. However, it adds a lot of time to the task. I simply want to write VBA code to perform that command to those specific fields.

I haven't been able to get SendKeys to work. DLast appears to provide random data at times. I feel like this should be a very simple request but for some reason I am not finding a functional solution for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不要摆弄数组或查询 - 使用dao 的功率:

Private Sub CopyButton_Click()

    CopyRecord
    
End Sub
  • 如果选择了记录,请复制此记录。
  • 如果选择了新记录,请复制最后一个(上一个)记录。

Private Sub CopyRecord()
  
    Dim Source        As DAO.Recordset
    Dim Insert        As DAO.Recordset
    Dim Field         As DAO.Field
    
    ' Live recordset.
    Set Insert = Me.RecordsetClone
    ' Source recordset.
    Set Source = Insert.Clone
    
    If Me.NewRecord Then
        ' Copy the last record.
        Source.MoveLast
    Else
        ' Copy the current record.
        Source.Bookmark = Me.Bookmark
    End If
  
    Insert.AddNew
    For Each Field In Source.Fields
        With Field
            If .Attributes And dbAutoIncrField Then
                ' Skip Autonumber or GUID field.
            Else
                Select Case .Name
                    ' List names of fields to copy.
                    Case "FirstField", "AnotherField", "YetAField"  ' etc.
                        ' Copy field content.
                        Insert.Fields(.Name).Value = Source.Fields(.Name).Value
                End Select
            End If
        End With
    Next
    Insert.Update
    
    Insert.Close
    Source.Close
    
End Sub

顺便说一句,这也是一个很好的例子,说明了RecordSetClone和recordset的clone clone - 第一个是“表单的记录”,而又是第二个是独立副本。

这也意味着表单将自动更新和立即

Don't fiddle with arrays or queries - use the power of DAO:

Private Sub CopyButton_Click()

    CopyRecord
    
End Sub
  • If a record is selected, copy this.
  • If a new record is selected, copy the last (previous) record.

Private Sub CopyRecord()
  
    Dim Source        As DAO.Recordset
    Dim Insert        As DAO.Recordset
    Dim Field         As DAO.Field
    
    ' Live recordset.
    Set Insert = Me.RecordsetClone
    ' Source recordset.
    Set Source = Insert.Clone
    
    If Me.NewRecord Then
        ' Copy the last record.
        Source.MoveLast
    Else
        ' Copy the current record.
        Source.Bookmark = Me.Bookmark
    End If
  
    Insert.AddNew
    For Each Field In Source.Fields
        With Field
            If .Attributes And dbAutoIncrField Then
                ' Skip Autonumber or GUID field.
            Else
                Select Case .Name
                    ' List names of fields to copy.
                    Case "FirstField", "AnotherField", "YetAField"  ' etc.
                        ' Copy field content.
                        Insert.Fields(.Name).Value = Source.Fields(.Name).Value
                End Select
            End If
        End With
    Next
    Insert.Update
    
    Insert.Close
    Source.Close
    
End Sub

This also, by the way, is an excellent example of the difference between the RecordsetClone and the Clone of a recordset - the first being "the records of the form", while the second is an independant copy.

This also means, that the form will update automatically and immediately.

谁与争疯 2025-02-11 02:12:28

只要编辑简单表的简单表格,并且绑定的数据字段名称与控件名称匹配,则可以逃脱将

If Me.Recordset.AbsolutePosition > 0 Then
  With Me.Recordset.Clone()
    .AbsolutePosition = Me.Recordset.AbsolutePosition - 1
    
    Dim control_name As Variant
    For Each control_name In Array("field1", "field2", "field3", "field4", "field5", "field6")
      Me.Controls(control_name).Value = .Fields(control_name).Value
    Next
  End With
End If

其分配给同一表单上的单独按钮。

Provided that it's a simple form to edit a simple table, and that the bound data field names match the control names, you may get away with

If Me.Recordset.AbsolutePosition > 0 Then
  With Me.Recordset.Clone()
    .AbsolutePosition = Me.Recordset.AbsolutePosition - 1
    
    Dim control_name As Variant
    For Each control_name In Array("field1", "field2", "field3", "field4", "field5", "field6")
      Me.Controls(control_name).Value = .Fields(control_name).Value
    Next
  End With
End If

which you assign to a separate button on the same form.

美煞众生 2025-02-11 02:12:28

您已经有一个好主意帖子了。

您也可以说将功能放在之前的插入事件中。此事件只有在您开始输入新的recrd时就会发射,并且它变得肮脏。

因此,也许这样做:

Private Sub Form_BeforeInsert(Cancel As Integer)

   Dim rstPrevious     As DAO.Recordset
   Dim strSQL          As String
   
   strSQL = "SELECT TOP 1 * FROM tblPeople ORDER BY ID DESC"
   
   Set rstPrevious = CurrentDb.OpenRecordset(strSQL)
   
   ' auto file out some previous values
   
   If rstPrevious.RecordCount > 0 Then
   
     Me.Firstname = rstPrevious!Firstname
     Me.LastName = rstPrevious!LastName
           
  End If

End Sub

要设置控件/字段的“列表”或“数组”的一些好主意,因此您不必编写很多代码。 (如在其他帖子/答案中所建议的那样)

You have a good idea post here already.

You could also say place a function in the before insert event. This event ONLY fires when you start typing into a NEW reocrd, and it becomes dirty.

So, maybe this:

Private Sub Form_BeforeInsert(Cancel As Integer)

   Dim rstPrevious     As DAO.Recordset
   Dim strSQL          As String
   
   strSQL = "SELECT TOP 1 * FROM tblPeople ORDER BY ID DESC"
   
   Set rstPrevious = CurrentDb.OpenRecordset(strSQL)
   
   ' auto file out some previous values
   
   If rstPrevious.RecordCount > 0 Then
   
     Me.Firstname = rstPrevious!Firstname
     Me.LastName = rstPrevious!LastName
           
  End If

End Sub

And some good ideas in say having a "list" or "array" of controls/fields to setup, so you don't have to write a lot of code. (as suggested in the other post/answer here)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文