Results 1 to 12 of 12
  1. #1
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6

    Custom Copy and paste in Forms

    Hi,

    Newbie user here. I'm designing an hour tracker for an air ambulance call logger. Essentially, the information for each call is input through a form and stored in its respective table.

    There is a lot of information that needs to be entered for each call (one form per call), some of which is repeating. I've looked at a couple examples where people have copied certain fields to be pasted into the next form however this involves going to the next form (ID of old +1) which does not always work for our purposes.

    Ideally I want to have a custom button called "copy crew" and "paste crew" whereby the "crew information is copied from the current log and pasted to another log further down the line. The VBA I have so far is this:

    Public Sub Command85_Click()


    Dim Pilot As String
    Dim Nurse As String
    'Copy fields to variables
    Pilot = Me!txtPilot
    Nurse = Me!txtNurse

    'Go to a new record
    End Sub
    Public Sub Command87_Click()
    'Reverse the process and plug old values into new record
    Me!txtPilot = Pilot
    Me!txtNurse = Nurse

    End Sub

    So this does not work. Any suggestions?

    Thanks in advance!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Could work if the variables are declared in the module header so they will be available to all subs in the module. Otherwise, the variables depopulate when the sub ends.

    Here is another approach http://access.mvps.org/access/forms/frm0012.htm
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6
    Hi,

    Thanks for the reply.

    So what's the best way to declare the variables in the module header? I've tried:

    Dim Pilot As String
    Dim Nurse As String
    ____________________

    Public Sub Command85_Click()

    'Copy fields to variables
    Pilot = Me!txtPilot
    Nurse = Me!txtNurse

    'Go to a new record

    Public Sub Command87_Click()
    'Reverse the process and plug old values into new record
    Me!txtPilot = Pilot
    Me!txtNurse = Nurse

    End Sub

    This just throws an error..

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    What error? That looks like it should work.

    Examples (every module should have the first two lines in header):

    Option Compare Database
    Option Explicit
    Global gstrArrayTestMethods() As Variant
    Global Const gstrBasePath = "\\dotatufs02\Crm\Lab\Database\"

    ______________

    Option Compare Database
    Option Explicit
    Dim int4Inch As Integer
    Dim int3Inch As Integer
    Dim int2Inch As Integer

    ______________

    Option Compare Database
    Option Explicit
    'used by lbxMaterialType_BeforeUpdate event to request a record save in the After_Update event,
    'to properly manage comparison of the current and old values of the list box
    Dim booUpdateRecord As Boolean
    Dim booTestsEdited As Boolean
    Dim intHeight As Integer 'stores value for number of tests selected for deletion
    Dim intTop As Integer 'stores value for position of the first selected record in Tests recordset
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Here is how I do it:
    Code:
    Private Sub copyrecordbutton_Click()
    On Error GoTo Err_copyrecordbutton_Click
    Dim txtOld1 As Variant
    Dim txtOld2 As Variant
    Dim txtOld3 As Variant
    Dim txtOld4 As Variant
    txtOld1 = txtcurrent1.Value
    txtOld2 = txtcurrent2.Value
    txtOld3 = txtcurrent3.Value
    txtOld4 = txtcurrent4.Value
    RunCommand acCmdRecordsGoToNew
    txtnew1.Value = txtOld1
    txtnew2.Value = txtOld2
    txtnew3.Value = txtOld3
    txtnew4.Value = txtOld4
    Exit_copyrecordbutton_Click:
    Exit Sub
    Err_copyrecordbutton_Click:
    MsgBox Err.Description
    Resume Exit_copyrecordbutton_Click

  6. #6
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6
    Ok so here's an image of the error:
    Click image for larger version. 

Name:	Untitled.png 
Views:	8 
Size:	182.0 KB 
ID:	13256

    Alan,

    I have tried your method. I think where the problem is with my project is that it is not always the next record where the information will need to be duplicated. Occasionally it will be a record 4 or 5 log ID's down the line. I need to be able to copy info from one previous (i.e. a couple records back) and insert that info to the next record.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Sorry, does not clarify for me. The technique works for me. Cannot replicate issue. Want to provide db for analysis? Follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6
    Practice database.zip

    Please find attached.

  9. #9
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    I just ran my code on your db and it worked without issue. I set the Dimension statements inside the code for the command button and did not get an error. I am confused by your statement about not always wanting to insert into the next record, but in your last sentence you say you want it in the next record. My code enters the new data into a new record and not the next record.

  10. #10
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6
    Thanks for your help here.

    Maybe I didn't get your code right in my project Alan. This is what I have:
    Option Compare Database
    Option Explicit
    Private Sub Command85_Click()

    Dim txtPilot As Variant
    Dim txtNurse As Variant


    txtPilot = txtPilot.Value
    txtNurse = txtNurse.Value


    RunCommand acCmdRecordsGoToNew

    txtPilot.Value = txtPilot
    txtNurse.Value = txtNurse

    End Sub

    I'm not sure on 2 things:
    First where does the "paste" command come into play?
    Second when you go txtOld1 = txtcurrent1.Value should "txtcurrent1.Value" be a new field some where?

    If you can show me the code you used that'd be great!

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The variables are not declared in module header. You have declared them within a specific sub. Look at the examples I posted earlier.

    Don't have variables and controls with exactly same names or if you do, qualify the control names with prefix referencing form. You can use the Me alias in this case. Don't have to type .Value. Value is default property of data controls.

    Option Compare Database
    Option Explicit
    Dim txtPilot As Variant
    Dim txtNurse As Variant

    Private Sub Command85_Click()
    txtPilot = Me.txtPilot
    txtNurse = Me.txtNurse
    End Sub

    Public Sub Command87_Click()
    RunCommand acCmdRecordsGoToNew
    Me.txtPilot = txtPilot
    Me.txtNurse = txtNurse
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    pilot3 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2013
    Posts
    6
    Thanks for all your help!

    I tried running the code and it is still giving me the following error message:

    The expression OnClick you entered as the event property setting produced the following error:
    Member already exists in an object module from which the module derives
    * The expression may not result in the name of a macro, the name of a user-defined function, or [Event Procedure].
    * There may have been an error evaluating the function, event or macro.

    Show Help >>
    This error occurs when an event has failed to run because
    the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name to run when the event occurs

    Any thoughts? After a quick search I cannot see any other modules in the project so I'm going to try rebuilding it from scratch to see if that works...

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Copy and Paste Row (vb)
    By Computer202 in forum Programming
    Replies: 7
    Last Post: 03-28-2014, 01:59 AM
  2. Any way to copy paste records from
    By super12 in forum Access
    Replies: 5
    Last Post: 03-05-2013, 11:16 PM
  3. Copy-Paste
    By BorisGomel in forum Access
    Replies: 4
    Last Post: 10-25-2011, 07:17 AM
  4. Copy/paste to new record.
    By xbox1513 in forum Forms
    Replies: 1
    Last Post: 02-23-2011, 04:52 PM
  5. Copy / Paste some fields
    By isnpms in forum Access
    Replies: 2
    Last Post: 08-25-2010, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums