473,396 Members | 1,916 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,396 software developers and data experts.

update & add data to a table using query

64 64KB
Hi,

I have a database table called : "tbl_adviseurs" with the fields;
- ID_adv
- NR_adv (primary key)
- Segment
- ZIP
- TEL
- AM
- AD

Now, every now and again i get an updated list for this table with;
- update rows (so the existing records must be update)
- new record
- deleted records
This is delivered in excel. I then import this data in a table called "import_adv"

I now have a "update Query" that works fine for updating existing records. Fields in the qyery;
- Segment
- Zip
- TEL
- AM
- AD

However it does not append the new records to the existing database and it does not delete records that are not in the "import_adv" table

How can i make sure that new records in the import_adv table are appended tot the tbl_adviseurs ?
How can i make sure that records that are deleted in the import_adv table are also deleted from the tbl_adviseurs ?

Any help would be greatly appreciated!
Pierre
Mar 21 '13 #1
10 7516
zmbd
5,501 Expert Mod 4TB
You'll need at least two queries
One that will update and append the records of interest.
One that will delete the offending records

However, you haven't told us how you know which records to what to...
Mar 21 '13 #2
Seth Schrock
2,965 Expert 2GB
It sounds like a Find Unmatched query might be needed to find the records that are in import_adv and aren't in tbl_adviseurs to know which ones to append. A reverse would tell you which ones to delete.
Mar 21 '13 #3
zmbd
5,501 Expert Mod 4TB
Seth, let us not guess here, let OP tell us.
Mar 21 '13 #4
Pierkes
64 64KB
hi zmdb,

How i know which records to add, delete or modify ?

- The primary key is the key to this. I set the primary key not to the ID but ot the field NR_adv.
This is the number we give to a "adviseur" and is unique.

When i get an updated list of "adviseur" i know;
- which are new entries - new NR_adv that i did not have before
- which entries are deleted - NR_adv that are not in the list anymore
- which entries to modify - all records that have the same NR_adv in both tables

does this answer your questions ?
regards,
Pierre
Mar 22 '13 #5
zmbd
5,501 Expert Mod 4TB
I like to make sure I understand things correctly before proceeding... it's the Chemist in me :) :::

So if I understand this correctly:
You need to have "tbl_adviseurs" be an exact duplicate of "import_adv" ?
Mar 22 '13 #6
Pierkes
64 64KB
Hi zmdb,

You are right, it should be an exact copy.

Regards,
Pierre
Mar 23 '13 #7
zmbd
5,501 Expert Mod 4TB
Well, as I suspect we have linked fields on [NR_adv] we can't just delete the information and import the table; thus, as I stated in post #2 you will need two queries, one of which will be the unmatched as Seth stated.

The query wizard actually doesn't do too bad a job on building the unmatched query... let it do that for you.
Once built, run once to make sure it returns the unmatched records from "tbl_adviseurs", if so, then switch to design mode, look at the ribbon, query type group, a red-X with an exclamation point (bang) - this is a run as delete. Click this, it will change the select to a delete query... now click on the run (just the red Bang)
I do this in two stages by hand just to be sure I have things correctly.
You can save this query as a delete query if you like and then the next time all you have to do is click on it in the navigation pane to run it... I personally don't like doing that - but that's me.

As for your update...
Open your update query in design mode...
Is there a join between the [NR_adv] fields for "Import_adv" and "tbl_adviseurs"?
You need to edit this relationship so right click...
You need to select the option so that all of the records from the transfer table (import_adv) are returned and the matching records from "tbl_adviseurs".
If you do not have this relationship then you need to create this relationship (left click and drag [import_adv]![NR_adv] over to [tbl_adviseurs]![NR_adv].... now right click the line and select the option to return all records from [import_adv] and matching records)

Your SQL should look something like this:
(now the field names will not be the same)
(tbl_people.people_pk = tblstaff.staffid are the related fields)
Expand|Select|Wrap|Line Numbers
  1. UPDATE 
  2.     tbl_people LEFT JOIN
  3.       tblstaff ON 
  4.          tbl_people.people_pk 
  5.             = tblstaff.staffid 
  6. SET tblstaff.staff_FirstName 
  7.      = [tbl_people]![people_FirstName]
  8.      , tblstaff.staff_LastName 
  9.          = [tbl_people]![People_LastName]
  10.      , tblstaff.staff_email 
  11.          = [tbl_people]![people_email];
(of course, in the sql view this will be all on one or two lines.)

and Bob's Your Uncle, you should be done.


Once these queries are running correctly, you can automate the process by creating either VBA or Macro code to call each query... I prefer VBA as I'd do a double check for stuff.
Mar 23 '13 #8
nico5038
3,080 Expert 2GB
When it should be an exact copy of the received file, you could use:

Expand|Select|Wrap|Line Numbers
  1. delete * from tbl_adviseurs
  2. 'and
  3. select into tbl_adviseurs select * from import_adv
  4.  
This will however only work when you haven't defined relations to the NR_adv field.

For adding records you can use the SELECT INTO anyway, as duplicates (those you UPDATE) will not be inserted, just suppress the error messages by running the query in VBA like:
Expand|Select|Wrap|Line Numbers
  1. currentdb.execute ("<your insert query>")
  2.  
Nic;o)
Mar 23 '13 #9
zmbd
5,501 Expert Mod 4TB
Noco5038:
(...) I suspect we have linked fields on [NR_adv] we can't just delete the information(...)
(...)Once these queries are running correctly, you can automate the process by creating either VBA or Macro code to call each query(...)
I already covered your entire post ?!
Mar 23 '13 #10
nico5038
3,080 Expert 2GB
@zmbd:
1) "Suspect" isn't "sure", thus my remark. Pierkes could have programmed relationships, thus making the replace table possible.
When the NR_adv is used as a relation in other tables, additional queries will be needed to remove the deleted values from those tables.
2) The VBA sample shows the syntax how to suppress the warningmessages, by not using a parameter for the errorhandling.

Guess you didn't cover my entire post :-)

Nic;o)
Mar 24 '13 #11

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

Similar topics

4
by: MikeG | last post by:
Hi there, I am using SQL Server 2000 and have users that would like to insert and / or update an existing table using Excel as Excel is a tool they understand. Does anyone have an idea how to do...
2
by: DanLezoche | last post by:
i have a client who's database requires users to enter the beginning date and ending date for contracts. i would like to create a vba function that checks the end date w/ the now() function (from...
1
by: rdeanstew | last post by:
I have created a query and saved it. The Access 2003 allowed me to create a table using the query. To the life of me I can't find out where to do this in A2007. Can someone either answer this or...
4
by: gihope | last post by:
Hi, I have an ASP.NET 2.0 C# issue that has been troubling me for some time, and if someone could shed some light on this I would appreciate it. This seems to be a commonly themed question,...
3
manoj9849967222
by: manoj9849967222 | last post by:
HI All I have two tables One is "master" & the other one is "child". The master Table have filds Productcode -----Number Productname-------Text Child Table
3
by: nico3334 | last post by:
I'm filling in a Report with SQL data using VB code. I'm using LOOP and MoveNext. Before using MoveNext, I would like to be able to check whether the new data is equal to the previous data that was...
1
by: amrit1123 | last post by:
I m working on ms access 2007. and i want to create a table which have a field of datatype attachment through query u .like this (create table tablename (name varchar);). so, how to create a table...
5
by: SagarDoke | last post by:
hi all, i am using yui framework. in that i want to update or set datatable cell of selected row dynamically by different value. can anybody please tell me how to do that?
1
by: BaseballGraphs | last post by:
Hello, I am trying to divide one value from my data table with an associated ID by another value from my data table with a different ID for the same day that the value was added to the data table....
1
by: jan princess | last post by:
please help me i'm trying to update a data user using vb.net2008 and sql server management studio express for my backend. the error says "must declare the scalar variable "@username" here's the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.