Results 1 to 19 of 19

Thread: Moving Records [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Moving Records [RESOLVED]

    I am using Microsoft Access 2003. This is a vba question. I have 2 tables with identical structures. How can I easily copy the 5th record in one table and make it a new record in the other table ?

    Thanks for the help,
    Bryan
    Last edited by Animelion; Jan 21st, 2006 at 01:21 PM. Reason: Problem Resolved
    ~ Animelion

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Moving Records

    You can execute an Append Query or just execute via VBA a INSERT INTO query which will select th desired record from the source table and insert it into the destination table.
    VB Code:
    1. INSERT INTO Table1 VALUES (SELECT * FROM Table2 WHERE Field1 = 5)
    Something like that. I just free typed it in the reply box without testing.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records

    Thanks but either I am entering it wrong or it just plain doesn't work.

    I made a command button with the following code under the click event.

    INSERT INTO tblNames_Storage VALUES (SELECT * FROM tblNames WHERE Field1 = 5)

    *when you have Field1 could I put in ID and then just name the ID# that I want to move ?

    ** what if there are multiple instances.

    Thanks for any clarification you can give.
    ~ Animelion

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Moving Records

    Quote Originally Posted by Animelion
    Thanks but either I am entering it wrong or it just plain doesn't work.

    I made a command button with the following code under the click event.

    INSERT INTO tblNames_Storage VALUES (SELECT * FROM tblNames WHERE Field1 = 5)

    *when you have Field1 could I put in ID and then just name the ID# that I want to move ?

    ** what if there are multiple instances.

    Thanks for any clarification you can give.
    First, are your records number 1 through X using the Field1 field? If so, then you should have gotten the 5th record, and your second question (about multiple instances) would be moot.

    What did you get when you ran that code?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Moving Records

    Field1 = 5 is where you would identify the record to be copied. If its Field1 value is equal to 5 or whatever it is then that record would be the one that gets copied.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records

    Thanks rob, but when I entered it in it doesn't run. It's not recognizing it as anything at all. It just highlights the word insert and ask me to debug. ~Thanks Again
    ~ Animelion

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Moving Records

    Can you post your code and give some info on it?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Moving Records

    Quote Originally Posted by Animelion
    Thanks rob, but when I entered it in it doesn't run. It's not recognizing it as anything at all. It just highlights the word insert and ask me to debug. ~Thanks Again
    What is the specific error message and how are you executing your INSERT statement?

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Moving Records

    Sorry if this sounds dumb, but you aren't actually using "Field1" as the fieldname, are you? If so, use whatever field name is correct.
    One problem you may have is that records in a table aren't guaranteed to be in any particular order (unless you always want the 5th record, regardless of what it is).
    *when you have Field1 could I put in ID and then just name the ID# that I want to move ?
    ** what if there are multiple instances.
    If the ID field is a primary key, it will copy just that one record. If there are multiple instances, it's not a primary key, and will move all records with a matching ID field.
    You could also do a SELECT INTO query; that way if you didn't have the second table, it would be created for you.
    Tengo mas preguntas que contestas

  10. #10

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records

    Thanks again for all the help. Here is the code I am using
    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3.  
    4. Private Sub cmdTable1_to_Table2_Click()
    5. Dim strMove As String
    6. strMove = lstTable1.Column(0, lstTable1.ListIndex + 1)
    7. INSERT INTO tblTable2 VALUES (SELECT * FROM tblTable1 WHERE Last_Name = strMove)
    8. lstTable1.Requery
    9. lstTable2.Requery
    10. End Sub

    Sorry for the delay in response time, but internet usage is limited here.
    ~ Animelion

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Moving Records

    Try something like (new code in red):

    Quote Originally Posted by Animelion
    Thanks again for all the help. Here is the code I am using
    VB Code:
    1. Option Compare Database
    2. Option Explicit
    3.  
    4. Private Sub cmdTable1_to_Table2_Click()
    5. Dim strMove As String[COLOR=DarkRed], strSQL AS String[/COLOR]
    6. strMove = lstTable1.Column(0, lstTable1.ListIndex + 1)
    7. [COLOR=DarkRed]If strMove <> "" Than[/COLOR]
    8.     strSQL = "INSERT INTO tblTable2 VALUES (SELECT * FROM tblTable1 WHERE Last_Name = [COLOR=DarkRed]'" & strMove & "')"
    9.     DoCmd.RunQuery strSQL[/COLOR]
    10.     lstTable1.Requery
    11.     lstTable2.Requery
    12. [COLOR=DarkRed]Else
    13.     MsgBox "No value selected!"
    14. End If[/COLOR]
    15. End Sub
    There are other ways, but this is a quick one. Simply writing an INSERT statement doesn't do anything (it was also incorrect). You may have to SetFocus to the listboxes to requery them.
    Tengo mas preguntas que contestas

  12. #12

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records

    Thanks for the tip, I will try it tonight. One other question though, is there a way to delete all of those records instead of moving them ?
    ~ Animelion

  13. #13

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records

    I just tried your new code, however docmd.RunQuery is not an option for some reason. It doesn't seem to be recognized. Any suggestions ?
    ~ Animelion

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Moving Records

    Do you have references set? Are you using DAO or ADO? DAO is faster if your app is just Access. I've never used Access 2003, don't know why you wouldn't have DoCmd. I did make a mistake; it should be DoCmd.RunSQL, not RunQuery.
    To delete, just use a DELETE statement instead of an INSERT statement.
    Tengo mas preguntas que contestas

  15. #15

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records [RESOLVED]

    The code is now working, however..... It ask if I want to append the record to the new table ? is there a way to supress this or is it just something that can't be bypassed ?
    ~ Animelion

  16. #16
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Moving Records [RESOLVED]

    To turn off most warnings, there are two ways I know of. Somewhere (don't know Access 2003) you should have a setting under Options to turn them off. The VBA way is to use code - DoCmd.SetWarnings = False. Remember to set it back to True afterwards.
    This won't trap for errors - you'll still want to do that. But it'll stop annoying warning messages.
    Why not just turn them off altogether? I have users that want them. Also, the Options setting has to be done on every install of Access. Turning them off on my development machine doesn't affect the user's machine - unless I use code.
    Tengo mas preguntas que contestas

  17. #17

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records [RESOLVED]

    Thanks a bunch, the docmd.setwarnings worked perfectly.
    ~ Animelion

  18. #18

    Thread Starter
    Hyperactive Member Animelion's Avatar
    Join Date
    Jan 2001
    Location
    Jacksonville NC
    Posts
    283

    Re: Moving Records [RESOLVED]

    One last question

    How can I locate a record where Field1 = "27381284". After that I want to change Field3's value of that record to "Sent".

    Thanks again, ~Bryan
    ~ Animelion

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Moving Records [RESOLVED]

    use the following SQL syntax

    UPDATE YourTable SET YourFieldToUpdate = 'Sent' WHERE YourOtherField = '27381284'
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width