Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2007, 06:37 AM
Registered User
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to? Copy rec to new tbl then del old rec

Hi,

I'm new to VBA and am building an ACCESS form that when a Button is clicked:
1. Save the current changes made to record in Table A.
2. Copies the current record from Table A to Table B (appending it)
3. Deletes the current record in Table A.

The purpose of this is to allow a user to click a button to archive the record in Table A to the archive table, Table B.

Hope this makes sense?

Thanks in advance for your help!

 
Old February 21st, 2007, 09:57 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Why would you save a record to table A, and then delete it? Why not just bind the form to table B and forget table A?


mmcdonal
 
Old February 21st, 2007, 10:30 AM
Registered User
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry all, I probably should have told you "why" I want to do this, and it will either make more sense, or someone will come up with a better solution.

Table A is the current "active" database, where as Table B is the "archive" database.

The form that is going to do this record move, first allows the user to add closing information to the record in Table A.

Then when they click on the "save" button, I want the record to then transfer over to the archive table (Table B).

I do not want the record in both places, but do want it in the active file (Table A) and then want it removed from there and moved to the archive file (Table B) once the user clicks on the button.

Hope that makes more business sense?

Thanks!


 
Old February 21st, 2007, 11:23 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Is table B linked to the database with table A, or is it totally disconnected?

mmcdonal
 
Old February 21st, 2007, 11:44 AM
Registered User
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

They are totally disconnected. Separate tables, but the fields are set up exactly the same.

 
Old March 5th, 2007, 01:31 PM
Registered User
 
Join Date: Mar 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would use an append query to copy the data to the other table, then a delete query to remove the data from the current table.

This is how I've done it when I needed to.

-- Steve Olson

 
Old March 5th, 2007, 01:36 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Wasn't this posted somewhere else and we did append and delete queries? Otherwise, Append and delete queries are problematic since you need to take the PK from table A, then move off the record, then add the record to table B, then remove the record from table A while not on the record, so a parameter query generally won't work.

Did you get this done or do you need help with the ADO?



mmcdonal
 
Old March 5th, 2007, 01:52 PM
Registered User
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mmcdonal,

Yes, I would love some help! I can do this in many other languages, on many different platforms, but I haven't learned enough about ACCESS or VBA.

My goal is to take a record from the table where teh active tables are (eg. Table A) and move it to the archive table (eg. Table B). That would mean, removing it from Table A and adding it to Table B.

It seems simple enough, but I'm clueless how to do it in ACCESS! I'm sure it's very simple, but I just haven't "got it" yet.


Thanks in advance!



 
Old March 5th, 2007, 02:31 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I use ADO since I do mostly Access?SQL development. Many of the posters here use DAO since it works slightly better in Access Jet.

To move the record from one table to another, I would assume you have some way of retrieving the PK of the record in TableA you want to move to TableB. How are you doing that? If it is on the form (hidden) you can do this:

I am also assuming for right now that you are in the same database, but you would want to use an ADODB.Connection object if TableB is in another database.

Dim rs1, rs2 As ADODB.Recordset
Dim sSQL1, sSQL2 As String
Dim iPK As Integer

iPK = Me.PKField

sSQL1 = "SELECT * FROM TableA WHERE [PKField] = " & iPK
sSQL2 = "SELECT * FROM TableB"

Set rs1 = New ADODB.Recordset
rs1.Open sSQL1, CurrentPorject.Connection, adOpenDynamic, adLockOptimistic

Set rs2 = New ADODB.Recordset
rs2.Open sSQL2, CurrentPorject.Connection, adOpenDynamic, adLockOptimistic

rs2.AddNew
rs2("FirstField") = rs1("FirstField")
...
rs2.Update

rs1.Close
rs2.Close

sSQL1 = "DELETE FROM TableA WHERE [PKFIeld] = " & iPK
Set rs1 = New ADODB.Recordset
rs1.Open sSQL1, CurrentPorject.Connection, adOpenDynamic, adLockOptimistic

'Don't close the recordset here

Did that work?


mmcdonal
 
Old March 5th, 2007, 04:05 PM
Registered User
 
Join Date: Feb 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

That looks very do-able, and I will try it tonight. Thank you very much! I've used SQL elsewhere (eg. PHP, mainframe, etc.), but hadn't delved into how to embed it into ACCESS! I'll let you know tomorrow if it works, but from looking at it, it looks like it's doing exactly what I need to do!

Thanks again!






Similar Threads
Thread Thread Starter Forum Replies Last Post
the UpdateCommand affected 0 of the expected 1 rec theRealAirness ADO.NET 0 March 28th, 2005 04:23 PM
Urgent rec navigation problems shelbygt22 Access VBA 1 March 18th, 2005 12:59 PM
Forcing a new rec in a subform from a diff form DrSnuggles Access 1 September 15th, 2004 10:13 AM
Force current rec on screen if errors exist wscheiman Access VBA 4 November 4th, 2003 02:39 PM
one rec function for multiple formats vakil Biztalk 2 August 28th, 2003 01:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.