Results 1 to 7 of 7
  1. #1
    Soule is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2011
    Posts
    58

    Moving Code Between Data Tables Problem

    Hi, Everyone,

    I'm trying to adapt code for an .accdb form button in the main form class module that moves a piece of data from a field in one table to the same named field in another table. Not copies it, but actually moves it...as if it were being cut and pasted. The first table is full of "unused" movie codes, and I want the second to hold them as "used". I want to keep them separated with "a table between them" because I don't want any of these movie codes wasted (they cost money). I understand that programming between two tables instead of one is more complex, but from the user-end point of view, I think one table could get messy much more easier. Also, having the data move from table to table will simply be more aesthetic for my users who don't know much about Access. I understand that I could add a movie code "Status" field to the first table and write code that populates it as "used" once I have done doing with it what I'm going to, but I really, really want to move it between the two tables. I can't find public code that can execute this move, so I'm trying to adapt code that just copies between tables. So far, the block of code I have to "cut/paste" move it is as follows:

    Code:
    Option Explicit
    Option Compare Database, Private Sub, etc. .....
     
    ' This block moves (not copies) that same movie code from "Unused Movie Code Table" to "Used Movie Code Table".
     
    If gcfHandleErrors Then On Error GoTo PROC_ERR
    Public Const gcfHandleErrors As Boolean = False
     
    CurrentDb.Execute "INSERT INTO [Used Movie Code Table].MovieCode" & "SELECT TOP 1 [Unused Movie Code Table].MovieCode" & "FROM [Unused Movie Code Table]" & "ORDER BY MovieCode ASC", 
            
    Debug.Print ("Move moviecode from Unused to Used table")
     
    PROC_EXIT:
    Exit Sub
     
    PROC_ERR:
    MsgBox ("Error moving movie code from Unused to Used table." & Err.Number & ") " & Err.Description, vbExclamation + vbOKOnly, _”Move Movie Code Between Tables”
     
    Resume PROC_EXIT
    Would this code move it or just copy it? Or not work at all? (I can't test it because it's a block in the middle of a bunch of other button automation code).

    Alternately, I was advised to set-up the above code using sSQL statements first, followed by the CurrentDb.Execute statement...

    Code:
    ' This block moves (not copies) that same movie code from "Unused Movie Code Table" to "Used Movie Code Table".
     
    sSQL = "INSERT INTO [Used Movie Code Table].Movie Code"
    sSQL = sSQL & " SELECT TOP 1 [Unused Movie Code Table].Movie Code"
    sSQL = sSQL & " FROM [Unused Movie Code Table]"
    sSQL = sSQL & " ORDER BY Movie Code ASC"
     
    Debug.Print ("Move moviecode from Unused to Used table")
     
    CurrentDb.Execute sSQL
     
    PROC_EXIT:
    Exit Sub
     
    PROC_ERR:
    MsgBox ("Error moving movie code from Unused to Used table." & Err.Number & ") " & Err.Description, vbExclamation + vbOKOnly, _”Move Movie Code Between Tables”
     
    Resume PROC_EXIT
    Is this just a layout thing to read the code easier? Will the code run better using this sSQL structure with the Execute command at the end? I thought that method was outmoded?

    Thank you so much for taking the time to look at this code problem. You guys have been consistantly educating me in the past month, and saving my neck on a surprise coding project that is a true trial by fire. You'll never know how your unselfishness with your knowledge is appreciated. Any thoughts on this are welcomed.

    Frank

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    There is no 'move' action. You must INSERT into one, then DELETE from the other. That requires two separate SQL action statements.
    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
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    That would copy it. You would need to run a delete query after that to delete it from the first table. Using a string variable makes it much easier to debug the SQL if it gets complicated:

    http://www.baldyweb.com/ImmediateWindow.htm

    If it was something simple, I would do it your first way:

    CurrentDb.Execute "DELETE * FROM TableName"

    Further, in your original code:

    CurrentDb.Execute "INSERT INTO [Used Movie Code Table].MovieCode" & "SELECT TOP 1..."

    The concatenation in red is not only unnecessary and inefficient, you introduced an error. You would end up with:

    "INSERT INTO [Used Movie Code Table].MovieCodeSELECT..."

    Note the lack of a space before SELECT, which would have caused a syntax error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Soule is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2011
    Posts
    58
    Thanks, Ya'll. I'm going to ditch this 2-table move entirely and go with a simpler single table with a flag field that populates with a date the user enters on the form. That will both keep the code cleaner and shorter. The users don't need Db aesthetics. Ha!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    That would be the more normalized design anyway, so I think that's a good idea.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,959
    I agree, better design.

    Maybe don't even need code. Could have the field default to Date() or Now() to automate the timestamp.
    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.

  7. #7
    Soule is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2011
    Posts
    58
    Quote Originally Posted by June7 View Post
    I agree, better design.

    ...Could have the field default to Date() or Now() to automate the timestamp.
    Yeah, I could do that with another lostfocus out of the date/time control. Less room for error with the user. Thanks!

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

Similar Threads

  1. Problem moving database to another computer
    By DB2010MN26 in forum Access
    Replies: 4
    Last Post: 11-22-2011, 04:23 PM
  2. moving tables between databases
    By TheShabz in forum Programming
    Replies: 4
    Last Post: 11-15-2010, 05:54 PM
  3. Moving data between tables
    By seeter in forum Programming
    Replies: 1
    Last Post: 08-13-2010, 08:08 AM
  4. Have 3 tables - problem with pulling data for query
    By wulfhund in forum Database Design
    Replies: 2
    Last Post: 08-13-2010, 05:38 AM
  5. Tables and “all data query” ID problem
    By mashe in forum Queries
    Replies: 5
    Last Post: 08-22-2009, 06:05 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