I am copying an existing record to a new record using the "copyRecord" code below.
My problem is I can't get to the new record.
The "copyRecord" code creates the new record ok and the "requery" in the "btnCopy" code returns the table to the first record as expected but the subsequent "docmd.findrecord" fails.
Running the same function "goToNewRecord" from a button on the form also fails until I shift focus on the form to another control.
Now I can access the new record. Why is it so?
The code is in calling order.
newAccno as double and Finder as string are public variables
My problem is I can't get to the new record.
The "copyRecord" code creates the new record ok and the "requery" in the "btnCopy" code returns the table to the first record as expected but the subsequent "docmd.findrecord" fails.
Running the same function "goToNewRecord" from a button on the form also fails until I shift focus on the form to another control.
Now I can access the new record. Why is it so?
The code is in calling order.
newAccno as double and Finder as string are public variables
Code:
Private Sub btnCopy_Click()
If UserLevel = 3 Then
MsgBox "Students are not authorised to copy / paste records!"
Exit Sub
End If
newAccno = Me.txtAccNo
Call copyRecord("Main", newAccno, "New")
Me.Requery
Call gotoNewRecord([Forms]![main collection], finder)
Call setLocked("Z")
Me.txtStatus = "R"
isNewRecord = Me.txtAccNo
Me.txtAccNo.Locked = True
isCopy = True
Me.txtSpec.SetFocus
End Sub
Public Sub copyRecord(strTbl As String, strAcc As Double, strNew As String)
Dim db As Database
Dim rs1 As Recordset
Dim rs2 As Recordset
Dim sNum As Integer
Dim lNum As Integer
Dim strname As String
finder = Trim(str(strAcc))
isCopy = False
sNum = 0
Set db = CurrentDb
Set rs1 = db.OpenRecordset(strTbl, dbOpensnapshot)
Set rs2 = db.OpenRecordset("Main", dbOpenDynaset)
lNum = rs1.Fields.Count - 1
rs1.MoveLast
If rs1!AccessionNumber <> strAcc Then
rs1.MoveFirst
rs1.FindFirst "Accessionnumber = " & finder
End If
rs2.Edit
rs2.AddNew
rs2!Label = True
sNum = 1
rs2![AccessionNumber] = getAccno()
finder = rs2!AccessionNumber
sNum = 2
Do Until sNum = lNum
strname = rs1.Fields(sNum).Name
rs2.Fields(strname) = rs1.Fields(sNum)
If strNew = "New" Then
If strname = "Create Date" Then
rs2.Fields(strname) = Date
End If
If strname = "createdby" Then
rs2.Fields(strname) = strFullName
End If
If strname = "Genus" And Nz(rs1.Fields(sNum), "") = "" Then
rs2.Fields(strname) = "X"
End If
End If
sNum = sNum + 1
Loop
rs2.Update
rs2.Close
rs1.Close
DoCmd.SetOrderBy "AccessionNumber" 'If I don't do this and the next line, the form is blank.
DoCmd.GoToRecord , , acLast
End Sub
Public Function gotoNewRecord(ByVal frm As Form, strFindWhat As String)
Dim str As String
Dim nTries As Integer
frm.Dirty = False
strFindWhat = Trim(strFindWhat)
DoCmd.FindRecord strFindWhat, acStart, False, acAll
End Function