MS Access - VBA - Cloning a Table - sql

I wanted to basically copy the entire content of one table to another.
Context:
Table source is SharePoint list and triggers an email per record being queried. No way to turn it off on my end as it's being utilised by another team.
When I run my queries on a local table, it's fine.
I need to just copy the data directly. So far, the only code I found is
DoCmd.TransferDatabase but I can't seem to configure it correctly.

Simplest method is probably to run a make-table query to (re)create the local table:
Dim Sql As String
Sql = "SELECT * INTO LocalTable FROM SharePointTable;"
CurrentDb.Execute Sql
That will pop a warning, though. If that is too much, create the local table, then run two queries - the first to delete all records from the local table, the second to append all records from the SharePoint table to the local table.

Related

Access Macro Creating a Duplicate Table that then Breaks the Macro

I inherited an Access based dashboard which uses a series of SQL queries and Make Table actions to write the results of those queries into tables located in two other Access files.
There is a macro that runs all of the make table commands which in turn run the sql queries. All of them are working when I run this on my machine, but when I run it from our VM which handles scheduled refreshes, it creates a duplicate table for one of the queries.
If I delete that table from the Tables1 database, the Queries database will run successfully and create both the correct table, and the duplicate in Tables1. However, each subsequent time, the macro will fail with an error saying that the duplicate table already exists.
This is the make table sql:
SELECT [SQLQueryName].*
INTO [TableName]
IN 'filepath\Tables1.accdb'
FROM SQLQueryName;
This is the same structure that all the other make table queries are using and they are not having this issue. I'm not sure if this matters or will tell someone something, but the duplicate table is the same name with a 1 added to the end of it. We also recently had to get a new VM setup and there have been a lot of weird issues with things not working as they did in the previous VM where this had been running without issue for quite a long time.
So far I've tried compacting both the Query and Table1 Database files. I've tried deleting the make table query and making a new version. I've tried deleting the table and duplicate from Tables1. I've tried rolling back the database files to versions several weeks old. We also made sure that the version of Access is the same as on my PC.
I am currently attempting to change it to a delete row/append row method rather than a make table, but even if that works I would still love to know why this is happening.
Edit:
Actual Code from make table that is failing, removed filepath.
SELECT [012_HHtoERorOBS].*
INTO [HHtoER-Obs]
IN '\\filepath\MiscTablesB.accdb'
FROM 012_HHtoERorOBS;
Here is code from 2 other make table queries that are working. Each make table query follows an identical format of select * from the sql query to get the data into the table in the destination accdbs.
SELECT [010_ScheduledVisitsQuery].*
INTO ScheduledVisits
IN '\\filepath\MiscTablesB.accdb'
FROM 010_ScheduledVisitsQuery;
SELECT [020_HH_HO].*
INTO HH_Referred_to_HO
IN '\\filepath\MiscTablesB.accdb'
FROM 020_HH_HO;
All of these tables exist in the destination accdbs when the make table queries are run. The macro does not include any commands to delete tables. Here is a screenshot of the top of the macro, it repeats all the make table queries then ends with a command to quit access.

How to sync/update a database connection from MS Access to SQL Server

Problem:
I need to get data sets from CSV files into SQL Server Express (SSMS v17.6) as efficiently as possible. The data sets update daily into the same CSV files on my local hard drive. Currently using MS Access 2010 (v14.0) as a middleman to aggregate the CSV files into linked tables.
Using the solutions below, the data transfers perfectly into SQL Server and does exactly what I want. But I cannot figure out how to refresh/update/sync the data at the end of each day with the newly added CSV data without having to re-import the entire data set each time.
Solutions:
Upsizing Wizard in MS Access - This works best in transferring all the tables perfectly to SQL Server databases. I cannot figure out how to update the tables though without deleting and repeating the same steps each day. None of the solutions or links that I have tried have panned out.
SQL Server Import/Export Wizard - This works fine also in getting the data over to SSMS one time. But I also cannot figure out how to update/sync this data with the new tables. Another issue is that choosing Microsoft Access as the data source through this method requires a .mdb file. The latest MS Access file formats are .accdb files so I have to save the database in an older .mdb version in order to export it to SQL Server.
Constraints:
I have no loyalty towards MS Access. I really am just looking for the most efficient way to get these CSV files consistently into a format where I can perform SQL queries on them. From all I have read, MS Access seems like the best way to do that.
I also have limited coding knowledge so more advanced VBA/C++ solutions will probably go over my head.
TLDR:
Trying to get several different daily updating local CSV files into a program where I can run SQL queries on them without having to do a full delete and re-import each day. Currently using MS Access 2010 to SQL Server Express (SSMS v17.6) which fulfills my needs, but does not update daily with the new data without re-importing everything.
Thank you!
You can use a staging table strategy to solve this problem.
When it's time to perform the daily update, import all of the data into one or more staging tables. Execute SQL statement to insert rows that exist in the imported data but not in the base data into the base data; similarly, delete rows from the base data that don't exist in the imported data; similarly, update base data rows that have changed values in the imported data.
Use your data dependencies to determine in which order tables should be modified.
I would run all deletes first, then inserts, and finally all updates.
This should be a fun challenge!
EDIT
You said:
I need to get data sets from CSV files into SQL Server Express (SSMS
v17.6) as efficiently as possible.
The most efficient way to put data into SQL Server tables is using SQL Bulk Copy. This can be implemented from the command line, an SSIS job, or through ADO.Net via any .Net language.
You state:
But I cannot figure out how to refresh/update/sync the data at the end
of each day with the newly added CSV data without having to re-import
the entire data set each time.
It seems you have two choices:
Toss the old data and replace it with the new data
Modify the old data so that it comes into alignment with the new data
In order to do number 1 above, you'd simply replace all the existing data with the new data, which you've already said you don't want to do, or at least you don't think you can do this efficiently. In order to do number 2 above, you have to compare the old data with the new data. In order to compare two sets of data, both sets of data have to be accessible wherever the comparison is to take place. So, you could perform the comparison in SQL Server, but the new data will need to be loaded into the database for comparison purposes. You can then purge the staging table after the process completes.
In thinking further about your issue, it seems the underlying issue is:
I really am just looking for the most efficient way to get these CSV
files consistently into a format where I can perform SQL queries on
them.
There exist applications built specifically to allow you to query this type of data.
You may want to have a look at Log Parser Lizard or Splunk. These are great tools for querying and digging into data hidden inside flat data files.
An Append Query is able to incrementally add additional new records to an existing table. However the question is whether your starting point data set (CSV) is just new records or whether that data set includes records already in the table.
This is a classic dilemma that needs to be managed in the Append Query set up.
If the CSV includes prior records - then you have to establish the 'new records' data sub set inside the CSV and append just those. For instance if you have a sequencing field then you can use a > logic from the existing table max. If that is not there then one would need to do a NOT compare of the table data with the csv data to identify which csv records are not already in the table.
You state you seek something 'more efficient' - but in truth there is nothing more efficient than a wholesale delete of all records and write of all records. Most of the time one can't do that - but if you can I would just stick with it.

Update duplicate table in network location from local copy

I'm trying to create an Update Query in Access 2010 to update a duplicate table on our shared drive from the user's local copy.
The Access program itself uses the usual split front end / back end.
Due to frequent drops over VPN, we came up with a method for:
downloading the latest version of the front end and a copy of the back end to the user's local drive
then running off of the local front end / back end (with the two linked)
and then using VBA to update individual records on both the local and network locations, unless the network drive is unavailable, where it then dumps the updated data into an array to be attempted again at program close.
I have two identical tables (one on the local and one on the network) that I need to create an Update Query to update any changes made in the local table to the one on the network so that it can be stored on the network database for the next user on their machine.
UPDATE HiringMgrData As NetworkHiringMgrData IN '\\ServerName\FilePath\HREmails_be.accdb'
SET NetworkHiringMgrData.UserName = HiringMgrData.UserName,
NetworkHiringMgrData.UserPhone = HiringMgrData.UserPhone,
NetworkHiringMgrData.UserEmail = HiringMgrData.UserEmail
WHERE NetworkHiringMgrData.ID IN (SELECT ID FROM HiringMgrData)
This gives me an error when it gets to the SET statements, and clicking through simply blanks the fields in the network table.
I'm trying to "trick" Access into treating the table in the network database as NetworkHiringMgrData, while keeping the name of table in the the local database HiringMgrData, in hopes that Access will be able to distinguish between the two.
In reality, both the local and network databases have a table named HiringMgrData with field names of ID, UserName, UserPhone, and UserEmail.
I was able to get the Append Query to work using:
INSERT INTO HiringMgrData IN '\\ServerName\FilePath\HREmails_be.accdb'
SELECT HiringMgrData.*
FROM HiringMgrData;
which simply adds any new records from the HiringMgrData table in the local database to the HiringMgrData table in the network database, but I cannot update the existing records.
Try the below. I was attempting to do something similar on my MS Access Database and for some reason this worked for me instead of using IN 'network path'
UPDATE [\\ServerName\FilePath\HREmails_be.accdb].HiringMgrData As NetworkHiringMgrData
inner join HiringMgrData as LocalHiringMgrData on NetworkHiringMgrData.ID = LocalHiringMgrData.ID
SET NetworkHiringMgrData.UserName = LocalHiringMgrData.UserName,
NetworkHiringMgrData.UserPhone = LocalHiringMgrData.UserPhone,
NetworkHiringMgrData.UserEmail = LocalHiringMgrData.UserEmail;
I think your WHERE clause is wrong and should be
WHERE NetworkHiringMgrData.ID = HiringMgrData.ID;
Note that this may generate lots of network traffic as all records are updated. Maybe your application can manage an isChanged flag and update only those records.

copy tables with data to another database in SQL Server 2008

I have to copy the tables with data from one database to another using Query. I know how to copy tables with data within a database. But I was not sure about how to do the same for copying between two databases.
I have to copy huge number of tables, so I need any fast method using query...
Anybody please help out...Thanks in advance...
You can use the same way to copy the tables within one database, the SELECT INTO but use a fully qualified tables names database.schema.object_name instead like so:
USE TheOtherDB;
SELECT *
INTO NewTable
FROM TheFirstDB.Schemaname.OldTable
This will create a new table Newtable in the database TheOtherDB from the table OldTable whih belongs to the databaseTheFirstDB
Right click on the database, select tasks and click on Generate Scripts.
In the resultant pop-up, choose options as required (click advanced), to drop and create table, drop if exists, etc.
Scroll down and choose "Schema and Data" or "Data Only" or "Types of data to script (2008 R2)"as required.
Save to file and execute on the destination DB.
Advantages -
Can be executed against the destination DB, even if it is on another server / instance
Quickly script multiple tables, with data as needed
Warning - Might take quite a while to script, if the tables contain a large amount of data.
Rajan
INSERT INTO DB2.dbo.MyOtherTable (Col0, Col1)
SELECT Col0, Col1 FROM DB1.dbo.MyTable
Both table column's must have same data types..
Below SQL Query will copy SQL Server table schema & data from one database to another database. You can always table name (SampleTable) in your destination database.
SELECT * INTO DestinationDB.dbo.SampleTable FROM SourceDB.dbo.SampleTable

How to write a query get all infomation from one table to another one

I am building access database that will get data from a outside source and place it in a table that is link to the data source. As we all know that you are not allowed to recinfigure that linked table.
What I want to do is take that data from that that linked table and make another table that I will be able to add additional new fields and snyc the out that gets put into the linked table.
Please Help
You need to read up on append queries:
http://www.databasedev.co.uk/append_query.html
In other SQL systems, this would use some variant on "INSERT INTO" or "SELECT INTO"
The other alternative would be to use DoCmd.TransferDatabase to import the table. This can be done from any data source that Access can use via a linked table. It might be a little tricky figuring out the exact format for the connect string if it's not an Access table, though.