Solved Copy Recordset (1 Viewer)

wanzi

New member
Local time
Today, 11:54
Joined
Dec 24, 2023
Messages
9
I created a small MySQL database for our club using Access 2021 via an ODBC connection, which also works well.
I manage the members using a simple form. If I now create a new member and the following member has the same data. Only the date of birth and first name are different. I just want to copy the current data set and I can't do it. Maybe someone has already solved this and can help me with a template.

Thank you and Merry Christmas!

I solved it like this and it works:
Private Sub cmdCopy_Click()

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryMitglieder", dbOpenDynaset)
Dim strNeueMitNr As String
Dim strNachname As String
Dim strStrasse As String
Dim strOrt As String
strNeueMitNr = neueMitNr("tblMitglieder")
strNachname = Me.Nachname
strStrasse = Me.Strasse
strOrt = Me.Ort

With rs
.AddNew
!MitNr = strNeueMitNr
!Nachname = strNachname
!Strasse = strStrasse
!Ort = strOrt

.Update
.Requery
End With

rs.Close
Set rs = Nothing

'an update with the new copied DS
Me.Requery

'and then go to the last record and display
DoCmd.GoToRecord , , acLast

End Sub
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 02:54
Joined
Oct 29, 2018
Messages
21,473
Hi. Welcome to AWF!

Have you tried the Ribbon menus yet? I think there's a Copy Record button for that.
 

wanzi

New member
Local time
Today, 11:54
Joined
Dec 24, 2023
Messages
9
No ribbon menu, I'm looking for something in VBA.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:54
Joined
Oct 29, 2018
Messages
21,473
No ribbon menu, I'm looking for something in VBA.
There's also the VBA equivalent:
Code:
   DoCmd.RunCommand acCmdSelectRecord
   DoCmd.RunCommand acCmdCopy
   DoCmd.RunCommand acCmdPasteAppend
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:54
Joined
Feb 19, 2002
Messages
43,275
Really, the same address and phone number and email? I would think it would be more trouble to write over the actual differences. I would just use cntl-' to copy the specific fields that are the same.

Another common method is in the afterupdate event, Copy the values you want to propagate to the Default property of the matching control. This lets you copy only a couple of fields rather than the whole record which is probably more useful. Obviously, for the first record you enter after the form is opened, there will be no defaults.
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:54
Joined
Sep 21, 2011
Messages
14,301
When I had to enter over 23K records I used something similar.
I had a checkbox for whether I wanted to copy previous record or not.

1703449738925.png


Then code like this for each field I wanted to copy.
In my case it was Date, Ship and Rank.

Code:
Private Sub Rank_ID_AfterUpdate()
    If Me.chkCopy Then
        Me![Rank_ID].DefaultValue = """" & Me![Rank_ID].Value & """"
    Else
        Me![Rank_ID].DefaultValue = 0
    End If

End Sub
 

wanzi

New member
Local time
Today, 11:54
Joined
Dec 24, 2023
Messages
9
Thanks for the information. I found out for a recordset method and found the following code and adapted to my needs. A new data record is created, but the first is displayed when I close the form and open again. What is missing in the code? Please help!
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:54
Joined
Sep 21, 2011
Messages
14,301
No code supplied?
We are not clairvoyant.
Thanks for the information. I found out for a recordset method and found the following code and adapted to my needs. A new data record is created, but the first is displayed when I close the form and open again. What is missing in the code? Please help!
 

wanzi

New member
Local time
Today, 11:54
Joined
Dec 24, 2023
Messages
9
Excuse, here is the code:
Private Sub cmdCopy_Click()

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("qryMitglieder", dbOpenDynaset)
Dim strNeueMitNr As String
Dim strNachname As String
Dim strStrasse As String
Dim strOrt As String
strNeueMitNr = neueMitNr("tblMitglieder")
strNachname = Me.Nachname
strStrasse = Me.Strasse
strOrt = Me.Ort

With rs
.AddNew
!MitNr = strNeueMitNr
!Nachname = strNachname
!Strasse = strStrasse
!Ort = strOrt
.Update
.Requery
End With

rs.Close
Set rs = Nothing

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:54
Joined
Sep 21, 2011
Messages
14,301
Nothing is missing.
If you want to go to the last record when you open the form, just issue that command on form load.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:54
Joined
Feb 19, 2002
Messages
43,275
It is poor practice to completely duplicate a record.

The "set default" method prevents this because at least ONE of the required fields needs to be different otherwise what would be the point of duplicating a record. This allows your BeforeUpdate validation procedure to keep you from accidentally creating a meaningless duplicate.

A new data record is created, but the first is displayed when I close the form and open again.
When you have duplicate records, there is no way for a query or code to distinguish them and that is why this is such poor practice.
 

Users who are viewing this thread

Top Bottom