Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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 March 8th, 2004, 05:42 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default copy and append records from table-A to table B

I need to copy records from table-A then append them to table-B. These record were previously selected via combo box. I no longer need to select records to append to table-B. I would like to do this via code, looping thru the records and appending them to the other table in the order stored in the combo box til EOF.

[u]Combo box</u> is as follows:
SELECT tblCust.[Item No]
FROM tblCust
GROUP BY tblCust.[Item No]
ORDER BY tblCust.[Item No];


Append Query is as follows:

INSERT INTO tblB ( RecordNo, [Item No], [Cust No])
SELECT tblA.RecordNo, tblA.[Item No], tblA.[Cust No],
FROM tblA
WHERE (((tblA.[Items No])=[Forms]![frmAppenCust]![Combo0]) AND ((tblA.[Cust No]) Like "D*"))
ORDER BY tblA.RecordNo, tblA.[Item No],tblA.[Cust No];

[u]The second Append query</u> is as follows:

INSERT INTO tblB ( RecordNo, [Item No], [Cust No])
SELECT tblA.RecordNo, tblA.[Item No], tblA.[Cust No],
FROM tblA
WHERE (((tblA.[Items No])=[Forms]![frmAppenCust]![Combo0]) AND ((tblA.[Item No]) Like "A*"))
ORDER BY tblA.RecordNo, tblA.[Item No], tblA.[Run No], tblA.[Cust No];

MY CODE IS AS FOLLOWS:
Sub sResetCustID()
    Dim rstoCustNo As Recordset
    Dim defoqdf As QueryDef
    Dim stroItemNo As String
    Dim db As Object
    Set db = CurrentDb()
    Set rstoCustNo = db.OpenRecordset("pulloItemNum")
    stroItem = rstoCustNo![Item No]
    DoCmd.OpenQuery "qryApCustD"
DoCmd.OpenQuery "qryApCustA"
    rstoCustNo.MoveFirst
    Do Until rstoCustNo.EOF
    Loop
End Sub

I know this code is totaling wrong. I am looping, but when I try to append the data, I get this prompt [Forms]![frmAppenCust]![Combo0])

So I changed my Code to:
Sub sResetCustID()
    Dim rstoCustNo As Recordset
    Dim defoqdf As QueryDef
    Dim stroItemNo As String
    Dim db As Object
    Set db = CurrentDb()
   Set rstoCustNo = db.OpenRecordset("SELECT DISTINCT tblCust.[Item No]FROM tblCust ORDER BY tblCust.[Item No]")
    Set defoqdf = db.QueryDefs("apCustD")
    stroItem = rstoCustNo![Item No]
       rstoCustNo.MoveFirst
    Do Until rstoCustNo.EOF
       With DoCmd
    .RunSQL "SELECT tblA.RecordNo, tblA.[Item No], tblA.[Cust No] FROM tblA WHERE (tblA.[Item No] = ?) AND ((tblA.[Cust No] Like "D*") OR ((tblA.[Cust No] Like "A*")) ORDER BY tblA.RecordNo, tblA.[Item No], tblA.[Cust No];"
    End With

Using this code doesn't show how it is being appended to tblB.

I am using MSAccess 2002. I am getting a error on ((tblA.[Cust No] Like "D*") OR ((tblA.[Cust No] Like "A*")) It doesnt like D*


In matters of style, swim with the current. In matters of principle, stand like a rock. Thomas Jefferson
-------\\\|///
--------(oo)
--oOOo-(_)-oOOo--
__________________
In matters of style, swim with the current. In matters of principle, stand like a rock. Thomas Jefferson

-------\\\\\\|///
--------(oo)
--oOOo-(_)-oOOo--
 
Old March 8th, 2004, 07:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use single quotes (') rather than double quotes (") inside the RunSQL statement

I am a loud man with a very large hat. This means I am in charge
 
Old March 8th, 2004, 07:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should elaborate:
.RunSQL "SELECT tblA.RecordNo, tblA.[Item No], tblA.[Cust No] FROM tblA WHERE (tblA.[Item No] = ?) AND ((tblA.[Cust No] Like 'D*') OR ((tblA.[Cust No] Like 'A*')) ORDER BY tblA.RecordNo, tblA.[Item No], tblA.[Cust No];"

Or - to space it out a bit nicer:

Code:
.RunSQL "SELECT tblA.RecordNo, tblA.[Item No], tblA.[Cust No] " _
  & "FROM tblA " _
  & "WHERE (tblA.[Item No] = ?) " _
  & "AND ((tblA.[Cust No] Like 'D*') " _
  & "OR ((tblA.[Cust No] Like 'A*')) " _
  & "ORDER BY tblA.RecordNo, tblA.[Item No], tblA.[Cust No];"



I am a loud man with a very large hat. This means I am in charge
 
Old March 9th, 2004, 01:02 AM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am confused. A combo can only have one item selected at a time so no looping is involved. The reference to
the combo:

WHERE (((tblA.[Items No])=[Forms]![frmAppenCust]![Combo0])

will only use the currently selected value in the combo. Also, it will also require delimiters appropriate to the data type in the bound field field of the combo.

I'm not seeing where the Order By clause is relevant here either and, there is no need to append a single record at a time as it possible to append a whole batch with a single append. I'm also curious about the grouping in the combo rowsource and last but not least, using a space in field names in a table is not a good practice.

Without knowing what you are trying to accomplish with the combo box, it is difficult to provide assistance. I would expect that [Cust No] ought to be unique in the table on which the combo is based but that too is unclear.

Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]
 
Old March 9th, 2004, 01:43 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You are right; the combo box can only use one item at a time. There is more than (1) item to select from. I wanted to loop thru each one of them without selecting them. I want to append each group automatically to the other table. This process is a resort records and get the D's on top of the A's per group of records.

In matters of style, swim with the current. In matters of principle, stand like a rock. Thomas Jefferson

-------\\\|///
--------(oo)
--oOOo-(_)-oOOo--
 
Old March 9th, 2004, 01:59 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your suggestion,

I will try this. After I select the fields in tableA, how do I append them to tableB?

In matters of style, swim with the current. In matters of principle, stand like a rock. Thomas Jefferson

-------\\\|///
--------(oo)
--oOOo-(_)-oOOo--
 
Old March 9th, 2004, 02:02 PM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

AllRighty Then,

The thing with the single quote is new. I will try that. I will let you know if it works.

In matters of style, swim with the current. In matters of principle, stand like a rock. Thomas Jefferson

-------\\\|///
--------(oo)
--oOOo-(_)-oOOo--





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy whole structure of table in #temp table maulik77 SQL Server 2000 2 December 21st, 2006 02:42 AM
need to copy and add new records in table chacquard Access VBA 12 May 2nd, 2005 09:58 AM
copy one table at server1 to another table Jane SQL Server 2000 2 January 31st, 2005 11:11 AM
Append Records From One Table to Another Table twsinc Access VBA 4 February 29th, 2004 03:04 PM
thousands records enter one table to another table mateenmohd SQL Server 2000 3 July 17th, 2003 07:52 AM





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