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

Insert or Update aggregates from another table

Hi,

I'm using Access 2007.
I have imported a table called 'January' and from this I need to update an existing table called 'TablesInfo' which needs to hold some details about the imported tables.

Fields needed are:
[NumRecords] total number of records in the imported table
[DateFrom] earliest date in the Arrival Time field of the imported table
[DateTo] latest date in the Arrival Time field of the imported table

I'm using VB from a command button at the moment to do this but I'm not sure what to use e.g. INSERT INTO or UPDATE or maybe I have to try a join?

Ok I have tested hard wiring in values and this works fine e.g.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "INSERT INTO TablesInfo ([NumRecords], [DateFrom], [DateTo]) VALUES ('100', '01/01/2008', '31/01/2008');"
  2.  
But i need to calculate them which brings up errors i.e.
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "INSERT INTO TablesInfo ([NumRecords], [DateFrom], [DateTo]) SELECT (COUNT (January.[Arrival Time]) AS Count, MIN (January.[Arrival Time]) AS Min, MAX (January.[Arrival Time]) AS Max) FROM January;"
  2.  
Any help would be appreciated
Thanks,

Chris
Feb 25 '08 #1
4 2426
JKing
1,206 Expert 1GB
Hi Chris,

I believe the error is coming from too many brackets in your select portion of the statement.

I made a mock-up of your scenario and the following code worked for me:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command4_Click()
  2.  
  3. Dim strSQL As String
  4.  
  5. strSQL = "INSERT INTO TableInfo ( NumRecords, DateFrom, DateTo ) "
  6. strSQL = strSQL & "SELECT Count(January.[Arrival Time]) AS [CountOfArrival Time], Max(January.[Arrival Time]) AS [MaxOfArrival Time], Min(January.[Arrival Time]) AS [MinOfArrival Time]"
  7. strSQL = strSQL & "FROM January;"
  8.  
  9.  
  10. DoCmd.RunSQL strSQL
  11.  
  12. End Sub
  13.  
Let me know how this works out for you.

Jking
Feb 26 '08 #2
Hi Chris,

I believe the error is coming from too many brackets in your select portion of the statement.

I made a mock-up of your scenario and the following code worked for me:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command4_Click()
  2.  
  3. Dim strSQL As String
  4.  
  5. strSQL = "INSERT INTO TableInfo ( NumRecords, DateFrom, DateTo ) "
  6. strSQL = strSQL & "SELECT Count(January.[Arrival Time]) AS [CountOfArrival Time], Max(January.[Arrival Time]) AS [MaxOfArrival Time], Min(January.[Arrival Time]) AS [MinOfArrival Time]"
  7. strSQL = strSQL & "FROM January;"
  8.  
  9.  
  10. DoCmd.RunSQL strSQL
  11.  
  12. End Sub
  13.  
Let me know how this works out for you.

Jking

Thank you that worked well.

As an extension to this i would like to have another filed called [tablename] this will be captured from the user when tables are imported so 'January' would be a value under tablename with the corresponding count and dates info.

I can't seem to add a variable to the code you suggested. Furthermore i'd like the tablename to be the key.

something like (which returns syntax error)
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO TableInfo ( tablename, NumRecords, DateFrom, DateTo ) "
  2. strSQL = strSQL & variable & ", SELECT Count(January.[Arrival Time]) AS [CountOfArrival Time], Max(January.[Arrival Time]) AS [MaxOfArrival Time], Min(January.[Arrival Time]) AS [MinOfArrival Time]"
  3. strSQL = strSQL & "FROM January;"
or would it be better to insert the table name variable then run an update query version of what you suggested. I tried this but i know its wrong
Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE TablesInfo SET ( NumRecords, DateFrom, DateTo ) = "
  2. strSQL = strSQL & "SELECT Count(January.[Arrival Time]) AS [CountOfArrival Time], Max(January.[Arrival Time]) AS [MaxOfArrival Time], Min(January.[Arrival Time]) AS [MinOfArrival Time]"
  3. strSQL = strSQL & "FROM January WHERE [TablesInfo.tableName] = '" & variable & "';"
Thanks,
Chris
Feb 26 '08 #3
...and obviously the other places where the table name 'January' is would be replaced with the variable....
Feb 26 '08 #4
NeoPa
32,556 Expert Mod 16PB
Please remember to use the [ CODE ] tags provided (See the # button when posting).
Feb 26 '08 #5

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

Similar topics

1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
10
by: Ivan | last post by:
Hi to all !!! I have one stored procedure that update and delete rows in differents tables, but when I try of delete of the main table this show problems with primary and foreign key. I DROP...
1
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp....
1
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
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...

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.