UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Duplicate Command Button, Access 2016    
 
   
mike60smart
post Feb 14 2018, 12:15 PM
Post#1


UtterAccess VIP
Posts: 12,864
Joined: 6-June 05
From: Dunbar,Scotland


Hi Everyone

I have a Main Form to display Customer Information

Then a Tab Control which has on one of the Pages a Subform.

Within this Subform I have another Tab Control and on one of the Pages a Subform. On this subform I tried to add a Command Button to Duplicate the Current Record.

I get the following error:-

Attached File  error.JPG ( 13.87K )Number of downloads: 0


Any help in resolving this would be appreciated.

Go to the top of the page
 
DanielPineault
post Feb 14 2018, 01:24 PM
Post#2


UtterAccess VIP
Posts: 6,220
Joined: 30-June 11



When are you getting this error, when you try to create the control or when you try to click the button?
Go to the top of the page
 
mike60smart
post Feb 14 2018, 01:50 PM
Post#3


UtterAccess VIP
Posts: 12,864
Joined: 6-June 05
From: Dunbar,Scotland


Hi Daniel

When I click the Button.

I know it works on a standard Main / Subform configuration so I believe it is to do with the configuration of my Forms.

Go to the top of the page
 
DanielPineault
post Feb 14 2018, 02:21 PM
Post#4


UtterAccess VIP
Posts: 6,220
Joined: 30-June 11



What's the code behind the button?
The subform allows the entry of new records?
Go to the top of the page
 
mike60smart
post Feb 14 2018, 03:04 PM
Post#5


UtterAccess VIP
Posts: 12,864
Joined: 6-June 05
From: Dunbar,Scotland


Hi Daniel

Yes the subform is a Continuous Form set to allow new entries.

The button was created by using the Wizard which created the following Embedded Macro

Attached File  Macro.JPG ( 35.1K )Number of downloads: 3
Go to the top of the page
 
mike60smart
post Feb 14 2018, 04:17 PM
Post#6


UtterAccess VIP
Posts: 12,864
Joined: 6-June 05
From: Dunbar,Scotland


Hi Daniel

I managed to do the Duplicate using the following Code:-

CODE
Private Sub cmdInsert_Click()

    On Error GoTo cmdInsert_Click_Error

Dim lngCustomerJobID As Long
Dim dteWorkDay As String
Dim lngEmployeeID As Long
Dim lngHourlyRate As Long
Dim lngHrsWorked As Long

lngCustomerJobID = Me.CustomerJobID
dteWorkDay = Me.WorkDay
lngEmployeeID = Me.EmployeeID
lngHourlyRate = Me.HourlyRate
lngHrsWorked = Me.HrsWorked


Dim dbsMydbs As DAO.Database
    Dim rstMyTable As DAO.Recordset
    Me.Dirty = False
        Set dbsMydbs = CurrentDb
        Set rstMyTable = dbsMydbs.OpenRecordset("tblJobWorkDays")
        With rstMyTable
            .AddNew
            !CustomerJobID = Me.CustomerJobID
            !WorkDay = Me.WorkDay
            !EmployeeID = Me.EmployeeID
            !HourlyRate = Me.HourlyRate
            !HrsWorked = Me.HrsWorked
            .Update
        End With

    MsgBox "New record has been added.", vbInformation, "Complete"

        rstMyTable.Close
        Set rstMyTable = Nothing
        Set dbsMydbs = Nothing
Forms![frmCustomerJobs]![frmCustomerJobsSubform]![frmJobWorkDayssubform].Form.Requery



    
    On Error GoTo 0
    Exit Sub

cmdInsert_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdInsert_Click, line " & Erl & "."

End Sub


Many thanks
thanks.gif
Go to the top of the page
 
DanielPineault
post Feb 14 2018, 07:53 PM
Post#7


UtterAccess VIP
Posts: 6,220
Joined: 30-June 11



You dim and the set a bunch of variable, but never use them. Below is a cleaned up version (I hope you don't mind)

CODE
Private Sub cmdInsert_Click()
    On Error GoTo Error_Handler
    Dim dbsMydbs              As DAO.Database
    Dim rstMyTable            As DAO.Recordset
    
    If Me.Dirty = True Then Me.Dirty = False
    Set dbsMydbs = CurrentDb
    Set rstMyTable = dbsMydbs.OpenRecordset("tblJobWorkDays")
    With rstMyTable
        .AddNew
        !CustomerJobID = Me.CustomerJobID
        !WorkDay = Me.WorkDay
        !EmployeeID = Me.EmployeeID
        !HourlyRate = Me.HourlyRate
        !HrsWorked = Me.HrsWorked
        .Update
    End With

    MsgBox "New record has been added.", vbInformation, "Complete"
    Forms![frmCustomerJobs]![frmCustomerJobsSubform]![frmJobWorkDayssubform].Form.Requery

Error_Handler_Exit:
    On Error Resume Next
    If Not rstMyTable Is Nothing Then
        rstMyTable.Close
        Set rstMyTable = Nothing
    End If
    If Not dbsMydbs Is Nothing Then Set dbsMydbs = Nothing
    Exit Sub

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: cmdInsert_Click" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Sub



Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    17th November 2018 - 03:45 AM