473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Moving Code Between Data Tables Problem

34
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:

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Option Compare Database, Private Sub, etc. .....
  3.  
  4. ' This block moves (not copies) that same movie code from "Unused Movie Code Table" to "Used Movie Code Table".
  5.  
  6. If gcfHandleErrors Then On Error GoTo PROC_ERR
  7. Public Const gcfHandleErrors As Boolean = False
  8.  
  9. CurrentDb.Execute "INSERT INTO [Used Movie Code Table].MovieCode" & "SELECT TOP 1 [Unused Movie Code Table].MovieCode" & "FROM [Unused Movie Code Table]" & 
  10. "ORDER BY MovieCode ASC", 
  11.  
  12. Debug.Print ("Move moviecode from Unused to Used table")
  13.  
  14. PROC_EXIT:
  15. Exit Sub
  16.  
  17. PROC_ERR:
  18. MsgBox ("Error moving movie code from Unused to Used table." & Err.Number & ") " & Err.Description, vbExclamation + vbOKOnly, _”Move Movie Code Between Tables”
  19.  
  20. Resume PROC_EXIT
Would this code move it or just copy it? Or not work at all? (I can't test it because of time and 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...

Expand|Select|Wrap|Line Numbers
  1. ' This block moves (not copies) that same movie code from "Unused Movie Code Table" to "Used Movie Code Table".
  2.  
  3. sSQL = "INSERT INTO [Used Movie Code Table].Movie Code"
  4. sSQL = sSQL & " SELECT TOP 1 [Unused Movie Code Table].Movie Code"
  5. sSQL = sSQL & " FROM [Unused Movie Code Table]"
  6. sSQL = sSQL & " ORDER BY Movie Code ASC"
  7.  
  8. Debug.Print ("Move moviecode from Unused to Used table")
  9.  
  10. CurrentDb.Execute sSQL
  11.  
  12. PROC_EXIT:
  13. Exit Sub
  14.  
  15. PROC_ERR:
  16. MsgBox ("Error moving movie code from Unused to Used table." & Err.Number & ") " & Err.Description, vbExclamation + vbOKOnly, _”Move Movie Code Between Tables”
  17.  
  18. 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
Jan 13 '12 #1
4 1608
NeoPa
32,556 Expert Mod 16PB
I know you've considered this already Frank, from the comments you've posted, but I wouldn't feel right if I failed even to notify you that your approach, far from making your life easier, will (and clearly already has) introduced new head-aches. Tables should only ever be created to hold data that is in a different format from other tables in the general run of things. When data is in the same format but has to move between states then it makes much better sense to add a state field to handle it. Database Normalisation and Table Structures may well help with your progressing education. I read your explanation, and it indicates careful consideration, but if I may say without intending to insult in any way, it also shows a lack of appreciation of some of the principles. Users, for instance, need never be exposed to the practicalities of how things are managed under the hood. You could easily use two separate queries run on the one, properly organised, table to represent the two sets of disparate data and each would appear to the users as a recordset of the appropriate records.

Anyway. Enough of all that. I know you didn't come here for that so I'll move on.
Frank:
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?
This is certainly just to make life easier yes. The layout and also the easier access to the actual string to be executed can certainly help at the development stage. The Execute command is common to both versions so I have no way to interpret the second part of that question. It's the main way that a SQL action query string is run in Access.

There are a few problems with your code :
  1. The SQL command only copies the record across. Nowhere that I can see does it remove it from the original table.
  2. In the second set of SQL code creation (using sSQL) the field name has changed and includes an embedded space. This reference will fail unless all such references are enclosed in square brackets [] just as the table names are.
  3. The code always seems to take the first (or lowest in order) [Movie Code] item to copy. You don't explain the rationale here and I see none that makes sense. Not that there is none, just that I see none. Maybe worth explaining if there is one that makes sense.
  4. Moving data from one dataset to another should really be an indivisible process, so should be handled (by a copy followed by a delete) within a single transaction.

That seems to be enough to be getting along with for now.

PS. The sSQL code could be done as :
Expand|Select|Wrap|Line Numbers
  1. Dim sSQL As String
  2.  
  3. sSQL = "INSERT INTO [Used Movie Code Table].[Movie Code] " & _
  4.        "SELECT TOP 1 [Movie Code] " & _
  5.        "FROM   [Unused Movie Code Table] " & _
  6.        "ORDER BY [Movie Code] ASC"
Jan 13 '12 #2
dsatino
393 256MB
Ok, I'm not going to get too deep here since I see NeoPa answered extensively and I'm sure I'm just reiterating what he already said.

You have no need for two tables. You just need to add a Yes/No column and store all the numbers in a single table. Not only is this method easier, but if follows the principles of database normalization.

Your method also leaves the possibility your number being in both tables, or neither table.
Jan 13 '12 #3
soule
34
Thanks, NeoPa and dsatino,

After going broad on multiple forum sites, It's obvious I would be a fool to continue on this 2-table/INSERT...DELETE tack. I'm going to a single table with a new "used on" date field that is simply populated by user form entry. Then I can create one query for un-used codes and one for used. Problem diminished. "Brevity is the essence of wit" - I can't remember who said that. :S Thanks, All.
Jan 13 '12 #4
NeoPa
32,556 Expert Mod 16PB
I knew you sounded intelligent after the first post of yours I read. It's good to feel vindicated :-D
Jan 13 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Creigh Shank | last post by:
On moving my data files from /var/lib to /mnt/ramdisk I seem to be missing something. I've changed every .ini and .conf file I can find (/etc/my.cnf, /etc/init.d/mysql, /etc/init.d/mysqld,...
1
by: Mr. x | last post by:
.... The second time I am sending this email ... .... I want a good solution, please. .... Thanks :) What I need is moving xml data from client to server & vice versa, but not using xml file....
2
by: Betrock | last post by:
This is probably very simple, but I just can't see my way thru it..... Short version: keyed values(numeric)in a lookup table are stored in a main table. They are displayed as text values - the...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
11
by: Fernando Barsoba | last post by:
Hi all, I very much need your help here guys.. I'm working on a IPSec implementation, and when almost finished, I found a considerable problem. I'm sending a particular array + a key to a...
1
by: Daves | last post by:
a SQL query can return multiple data tables eg SELECT expression1 ... SELECT expression2 ... Can I in ASP.Net utilise this to do fewer data fetches from the server, e.g. selectively binding...
1
by: Felix_WafyTech | last post by:
Hi, I'm working with the ObjectDatasource and the application is getting more and more chatty. Is there a way I could make the ObjectDatasource support multiple DataTables that could be...
3
by: Joe Salmeri | last post by:
I have found a data corruption problem with pyodbc. OS = Windows XP SP2 DB = Microsoft Access XP PROBLEM: When selecting columns from a table that are of type Memo the value returned is...
1
by: Max2006 | last post by:
Hi, I am truing to find a pattern for my Business Logic Layer to be able to work fine win ObjectDataSource's Update method. The challenge is ObjectDataSource is not able to work with an...
0
by: 17beach | last post by:
I am moving code from one machine A to another Machine B one project in Machine A has a shared assembly in the GaC of Machine A This project fails to load load on Machine B its actually grayed out...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.