|
-
Jan 11th, 2006, 08:40 AM
#1
Thread Starter
Hyperactive Member
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
-
Jan 11th, 2006, 10:51 AM
#2
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:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 12th, 2006, 09:16 AM
#3
Thread Starter
Hyperactive Member
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.
-
Jan 12th, 2006, 09:19 AM
#4
Re: Moving Records
 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?
-
Jan 12th, 2006, 11:21 AM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 12th, 2006, 11:39 AM
#6
Thread Starter
Hyperactive Member
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
-
Jan 12th, 2006, 11:47 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jan 12th, 2006, 12:05 PM
#8
Re: Moving Records
 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?
-
Jan 12th, 2006, 01:35 PM
#9
Frenzied Member
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
-
Jan 16th, 2006, 12:59 PM
#10
Thread Starter
Hyperactive Member
Re: Moving Records
Thanks again for all the help. Here is the code I am using
VB Code:
Option Compare Database
Option Explicit
Private Sub cmdTable1_to_Table2_Click()
Dim strMove As String
strMove = lstTable1.Column(0, lstTable1.ListIndex + 1)
INSERT INTO tblTable2 VALUES (SELECT * FROM tblTable1 WHERE Last_Name = strMove)
lstTable1.Requery
lstTable2.Requery
End Sub
Sorry for the delay in response time, but internet usage is limited here.
-
Jan 16th, 2006, 03:00 PM
#11
Frenzied Member
Re: Moving Records
Try something like (new code in red):
 Originally Posted by Animelion
Thanks again for all the help. Here is the code I am using
VB Code:
Option Compare Database
Option Explicit
Private Sub cmdTable1_to_Table2_Click()
Dim strMove As String[COLOR=DarkRed], strSQL AS String[/COLOR]
strMove = lstTable1.Column(0, lstTable1.ListIndex + 1)
[COLOR=DarkRed]If strMove <> "" Than[/COLOR]
strSQL = "INSERT INTO tblTable2 VALUES (SELECT * FROM tblTable1 WHERE Last_Name = [COLOR=DarkRed]'" & strMove & "')"
DoCmd.RunQuery strSQL[/COLOR]
lstTable1.Requery
lstTable2.Requery
[COLOR=DarkRed]Else
MsgBox "No value selected!"
End If[/COLOR]
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
-
Jan 16th, 2006, 04:36 PM
#12
Thread Starter
Hyperactive Member
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 ?
-
Jan 16th, 2006, 04:49 PM
#13
Thread Starter
Hyperactive Member
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 ?
-
Jan 16th, 2006, 05:23 PM
#14
Frenzied Member
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
-
Jan 21st, 2006, 01:41 PM
#15
Thread Starter
Hyperactive Member
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 ?
-
Jan 22nd, 2006, 10:59 AM
#16
Frenzied Member
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
-
Jan 22nd, 2006, 01:54 PM
#17
Thread Starter
Hyperactive Member
Re: Moving Records [RESOLVED]
Thanks a bunch, the docmd.setwarnings worked perfectly.
-
Jan 27th, 2006, 02:00 PM
#18
Thread Starter
Hyperactive Member
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
-
Jan 27th, 2006, 02:57 PM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|