Having Problems Copying and Pasting Records

GMSRE

Registered User.
Local time
Today, 07:49
Joined
Apr 27, 2011
Messages
29
Hello,

I am working in Access 2007

I am trying to copy and paste a record using VBA code. I am also trying to increment RecComIDNO after the record has been copied and pasted. Listed below is the code I am using. I can’t figure out why the code will not copy and paste the record in the table called “InputTable”. Your help will greatly be appreciated. Thank you. GMSRE

Private Sub CopyRecord_Click()
On Error Resume Next
Set db = CurrentDb
boCopy = False
Dim strSQL As String
'Copy the community record
If Not IsNull(Me.RecComIDNO) And Not IsNull(Me.CommunityName) Then
strSQL = "SELECT * FROM InputTable " & _
"WHERE CommunityName = '" & Me.CommunityName & "' AND RecComIDNO = " & Me.RecComIDNO
Set rsCopy = db.OpenRecordset(strSQL, dbOpenSnapshot)
If (rsCopy.RecordCount > 0) Then 'Me.RecComIDNO > 0
Me.cmdPaste.Enabled = True
Me.cmdPaste.SetFocus
boCopy = True
End If
Else
MsgBox "You must have a current Community record."
End If
End Sub

Public Function NextID() As Long
On Error Resume Next
'Generate the next stream id for a quad
Dim rs As DAO.Recordset
Dim strSQL As String
Dim lngID As Long

lngID = 1
strSQL = "SELECT RecComIDNO FROM InputTable WHERE CommunityName = '" & Forms!DataForm!CommunityName & "'"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

If (rs.RecordCount > 0) Then
rs.MoveLast
lngID = rs.RecordCount + 1
End If
rs.Close
NextID = lngID
End Function

Private Sub cmdPaste_Click()
On Error Resume Next
Dim rsTable As DAO.Recordset
Dim fld As DAO.Field
Dim strField As String
Dim ReccomID As Long
Dim strFind As String
Dim ID As Long

'Find the record that matches the control.


'Paste the Community record
If Not IsNull(Forms!DataForm!CommunityName And boCopy = True) Then
ID = NextID()
Set rsTable = db.OpenRecordset("InputTable", dbOpenDynaset)
rsTable.AddNew
rsTable!RecComIDNO = ID
rsTable!CommunityName = Forms!DataForm!CommunityName
For Each fld In rsCopy.Fields
strField = fld.Name
If (strField <> "RecComIDNO" And strField <> "CommunityName") Then
rsTable(fld.Name) = fld.Value
End If
Next
rsTable.Update
rsTable.Close
Me.Requery
strFind = "RecComIDNO = " & ID & " AND CommunityName = '" & Forms!DataForm!CommunityName & "'"
Me.RecordsetClone.FindFirst strFind
Me.Bookmark = Me.RecordsetClone.Bookmark
Me.CopyRecord.SetFocus
Me.cmdPaste.Enabled = False
boCopy = False
Else
MsgBox "You must copy a record and select a Community First.", vbCritical, "Error"
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom