Solved In form, move to another record with known ID (1 Viewer)

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
This one's been bugging me for a few weeks now, and I haven't gotten an answer on two other forums.

I'm in a form on a record. I programmatically copy that record to a new record. After doing that I know the ID of the new record.
How can I "move" to that new record in the same open form with VBA ?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 23:00
Joined
Oct 29, 2018
Messages
21,600
How about?
Code:
Me.Recordset.FindFirst "ID=" & varID
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:00
Joined
Feb 28, 2001
Messages
27,444
If you have the ID, ...

Code:
Me.RecordsetClone.FindFirst "ID = " & varID
If Not Me.RecordsetClone.NotFound Then
    Me.Recordset.Bookmark = Me.RecordsetClone.Bookmark
End if
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:00
Joined
May 7, 2009
Messages
19,249
if you are using SQL (which i think you are doing since it does not automatically put you on the New record).
you first Requery your form and execute Either of the 2 previous replies.

if you want to be on the New record created you can use:

Code:
with docmd
   .RunCommand acCmdSelectRecord
   .RunCommand acCmdCopy
   .RunCommand acCmdPasteAppend
end with
 

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
How about?
Code:
Me.Recordset.FindFirst "ID=" & varID
I gave it a try, it goes to the wrong record. Should go to the record id in green, but went to the one in pink (hey, I didn't choose these colors that the snip tool uses).
220920Copy1.jpg

220920Copy2.jpg
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:00
Joined
May 7, 2009
Messages
19,249
what is AppId? a function?
how does the function work?
 

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
If you have the ID, ...

Code:
Me.RecordsetClone.FindFirst "ID = " & varID
If Not Me.RecordsetClone.NotFound Then
    Me.Recordset.Bookmark = Me.RecordsetClone.Bookmark
End if
Gets a runtime error:
220920Copy3.jpg
 

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
what is AppId? a function?
how does the function work?
AppId is a constant in the form, not important here.
The actual record copy code (I copied it from elsewhere, modified it some, but it seems to work) is a few sub layers deep, but the important section of code that copies looks like this:

Code:
Dim fimr As String
  Dim rs1 As Recordset, rs2 As Recordset, F As Field
  Set rs1 = CurrentDb.OpenRecordset("SELECT * FROM " & aTblNm1 & " WHERE " _
    & aWhereCond)
  '  While Not rs1.EOF
  Set rs2 = CurrentDb.OpenRecordset(aTblNm2)
  rs2.AddNew
  For Each F In rs1.Fields
    temp1 = "]" & F.Name & "]"
Debug.Print temp1
    If InStr(1, gSp(6), temp1) Then
      'don 't include the skipped item
    Else
      fimr = ""
      If InStr(1, gSp(7), temp1) Then
        ' write extraction routines
        temp2 = fSarrayPosition(gSp(7), F.Name, "]")
        fimr = fSarrayExtract(gSp(8), temp2, "]")
        Select Case fimr
          Case "*User*"
            fimr = SysCtrl(2, 1)
          Case "*Now*"
            fimr = Now()
          Case "*Now.*"
            fimr = Format(Now(), "yyyy.mm.dd.hh.nn.ss")
          Case Else
            ' use data passed in
        End Select
      End If
      If fimr = "" Then
        rs2(F.Name) = rs1(F.Name).Value
      Else
        rs2(F.Name) = fimr
      End If
    End If
  Next
  rs2.Update
  rs2.Bookmark = rs2.LastModified
  gSp(1) = rs2.Fields(0)
  rs1.MoveNext
  '  Wend
  rs2.Close
  rs1.Close
  Set rs2 = Nothing
  Set rs1 = Nothing
ExitCd:
  Exit Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:00
Joined
May 7, 2009
Messages
19,249
yes use .NoMatch not .NotFound

also i noticed from the code you post that gsp(1) is already being set to .Field(0) (is field(0) same as "EntityID"?)

sorry but you need to Requery the Form's Recordset first because you are using a Different Recordset on
Adding new record.

Requery, then .FindFirst.

if you do not want to requery use the Form's Recordset to add the record.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:00
Joined
Feb 28, 2001
Messages
27,444
Whoops, my bad. I always confuse .NoMatch and .NotFound - that is probably the source of the error.

Apologies, @twgonder, for shooting from the hip - and missing. The snippet I showed you using .RecordsetClone is the code that the command button wizard will build for you. I added the test for failure to find it because if you DO fail to find it, the bookmark copy won't work right, so would have traded one error for another.
 

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
also i noticed from the code you post that gsp(1) is already being set to .Field(0) (is field(0) same as "EntityID"?)

sorry but you need to Requery the Form's Recordset first because you are using a Different Recordset on
Adding new record.

Requery, then .FindFirst.

if you do not want to requery use the Form's Recordset to add the record.
I ran across some confusion on .Field(0), with some claiming it's always the ID and others saying no. So if it fails I'll go back to the field name proper. If I can remember how.
 
Last edited:

Minty

AWF VIP
Local time
Today, 07:00
Joined
Jul 26, 2013
Messages
10,382
I ran across some confusion on .Field(0), with some claiming it's always the ID and others saying no. So if it fails I'll go back to the field name proper. If I can remember how.
Only if it's the first field in the record set being used.
Always specify the field name unless you are retrieving a single field record set with just the ID, it removes any ambiguity.
 

moke123

AWF VIP
Local time
Today, 02:00
Joined
Jan 11, 2013
Messages
4,012
I ran across some confusion on .Field(0), with some claiming it's always the ID and others saying no. So if it fails I'll go back to the field name proper.
It shouldn't be too confusing. I almost exclusively have the primary key as the first field in a table. Therefore rs.Fields(0) always points to the PKey. It just depends where it is positioned in the fields collection. Using rs.fields("MyPKeyField") will work no matter where the field lies in the collection.
 

moke123

AWF VIP
Local time
Today, 02:00
Joined
Jan 11, 2013
Messages
4,012
I cant really tell what exactly you're doing as all the if's and select cases are confusing and you don't show all your code.

From what I can tell you are trying to copy all the form fields to a new record and then move the forms bookmark to that record.

here's a simple example that adds all the forms fields and field values to a dictionary object. The arguments passed are the name of the primary key field name and the primary key value of the record being copied. The primary key field is then removed from the dictionary .
We then loop through the dictionary in the .addnew section of the code and return the new ID. Then the forms bookmark is set to the new PK.
 

Attachments

  • TWG.accdb
    528 KB · Views: 66

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
Thanks for all the help. I now have Burt Charles Smith copying like a mad man.
My next step will be to add sequential numbers instead of date to the Entity code. New question for that.
220920Copy4.jpg

I cant really tell what exactly you're doing as all the if's and select cases are confusing and you don't show all your code.

From what I can tell you are trying to copy all the form fields to a new record and then move the forms bookmark to that record.

here's a simple example that adds all the forms fields and field values to a dictionary object. The arguments passed are the name of the primary key field name and the primary key value of the record being copied. The primary key field is then removed from the dictionary .
We then loop through the dictionary in the .addnew section of the code and return the new ID. Then the forms bookmark is set to the new PK.

I cant really tell what exactly you're doing as all the if's and select cases are confusing and you don't show all your code.

From what I can tell you are trying to copy all the form fields to a new record and then move the forms bookmark to that record.

here's a simple example that adds all the forms fields and field values to a dictionary object. The arguments passed are the name of the primary key field name and the primary key value of the record being copied. The primary key field is then removed from the dictionary .
We then loop through the dictionary in the .addnew section of the code and return the new ID. Then the forms bookmark is set to the new PK.

I cant really tell what exactly you're doing as all the if's and select cases are confusing and you don't show all your code.

From what I can tell you are trying to copy all the form fields to a new record and then move the forms bookmark to that record.

here's a simple example that adds all the forms fields and field values to a dictionary object. The arguments passed are the name of the primary key field name and the primary key value of the record being copied. The primary key field is then removed from the dictionary .
We then loop through the dictionary in the .addnew section of the code and return the new ID. Then the forms bookmark is set to the new PK.

I cant really tell what exactly you're doing as all the if's and select cases are confusing and you don't show all your code.

From what I can tell you are trying to copy all the form fields to a new record and then move the forms bookmark to that record.

here's a simple example that adds all the forms fields and field values to a dictionary object. The arguments passed are the name of the primary key field name and the primary key value of the record being copied. The primary key field is then removed from the dictionary .
We then loop through the dictionary in the .addnew section of the code and return the new ID. Then the forms bookmark is set to the new PK.
This page seems to have gone nuts on me wehen replying, and I can't see why.

@moke123 Thanks for your assist. What all the code is doing isn't all that important. It works. I copied it, modified it and think I understand it. I was looking for a way to get the newly created-copied record to appear in the form. It seems you are doing a read-write-read-write while my code just does a read-write. If I understand you correctly.
 

moke123

AWF VIP
Local time
Today, 02:00
Joined
Jan 11, 2013
Messages
4,012
It seems you are doing a read-write-read-write while my code just does a read-write. If I understand you correctly.
No, my code only reads the recordset once into a dictionary and writes it to the table and it only uses one recordset as opposed to 2.

It's this part of the code which sets the forms bookmark
Code:
Private Sub Command4_Click()
    Dim rsID As Long
    Dim rs As DAO.Recordset

    Me.Dirty = False  'save record

    rsID = frmCopy("PersonID", Me.PersonID) 'copy the record and get the new PKey

    Me.Recordset.Requery

    Set rs = Me.Recordset

    rs.FindFirst "PersonID = " & rsID

    If Not rs.NoMatch Then
        Me.Bookmark = rs.Bookmark
    End If

End Sub


I hope you're aware that these table names need fixing. Get rid of the spaces and special characters.
 

twgonder

Member
Local time
Today, 01:00
Joined
Jul 27, 2022
Messages
178
No, my code only reads the recordset once into a dictionary and writes it to the table and it only uses one recordset as opposed to 2.

I hope you're aware that these table names need fixing. Get rid of the spaces and special characters.
I'm not sure what you mean by "dictionary".
What spaces are you seeing in a table name? Underscore is not a "special character" it is a common delimiter used for coding.

220920Naming.jpg


Although I don't use it to separate Order_Details, I do use it in the multi-language and helps parts of my system. I want to process on tbl_ names, but not on tbld_ and tblc_ records which don't contain user data. It's easier to just pluck the _ field than try and build a bunch of logic for different size portions of a name. I do the same with names for text boxes and combo boxes in forms to process on those objects.

The Au in the name means "Application Universal" as Pr means "Payroll". This allows me to isolate tables based on their application modules. And to create a similar name for different sub modules without clashing.
 
Last edited:

Users who are viewing this thread

Top Bottom