How to end Windows lock on file in ODBC connection? - ms-access

I am using Access to import data from a series of SQLite 3 databases that are all structured the same. I have a system/user DSN for "Import.db". My Access DB has linked tables to those in Import.db.
My goal is to import the data via the linked tables into Access tables, then delete Import.db, then copy the next SQLite3 DB to the same location and call it Import.db, and then keep repeating the process till all are imported.
I took this approach because I don't know how to create DSNs on the fly and link tables for SQLite3 dbs. SQL Server, yes, but not SQLite3. So I thought, just use the same DSN but change the actual file.
The trouble is, after opening my Access DB and opening the linked tables, Access creates a Windows file lock on Import.db. So I can't delete and replace it. Instead, I can import one, then close Access, reopen Access, and repeat. Not so hot.
Suggestions?

You can get the connection string of a linked table with:
Debug.Print CurrentDb.TableDefs("MyLinkedTable").Connect
You now have the information you need to create a query that imports without a linked table.
You might get something like this:
ODBC;DSN=SQLite3;Database=Z:\Docs\Import.db;StepAPI=0;SyncPragma=NORMAL;
NoTXN=0;Timeout=;ShortNames=0;LongNames=0;NoCreat=0;NoWCHAR=0;
FKSupport=0;JournalMode=;OEMCP=0;LoadExt=;BigInt=0;
But you probably won't need most of it, so:
sODBC = "[ODBC;DSN=SQLite3;Database=Z:\Docs\Import.db;]"
''Create table query, but append and update are also easy enough
sSQL = "SELECT * INTO SQLite_Import FROM " & sODBC & ".SQLiteTableNameHere"
CurrentDb.Execute sSQL, dbFailOnError

Related

Is it safe to frequently Compact and Repair Access DB?

I have an Access Database which I populate using a pass through query.
I get specific data from an SQL database and dumps it on Access.
This Access DB is updated weekly but I cannot simply append new data from the previous week because all other past weeks also updates.
What I did is truncate the Access DB and then load everything again.
When I do that, I need to Compact and Repair the DB so that the size doesn't bloat.
My question is, is it ok to do that? Currently I am using the logic posted in this answer.
I have not encountered any problems yet but I just want to make sure and get our access guru's thought about it. Also I'm planning on doing a scheduled run on our server to do the task.
Just need to make sure that it will not get corrupted easily (what is the chance of corrupting the file in the first place?).
If you'll ask, why do I need to do this? Users of data have no access on SQL server.So I have to pull data for them so they can just connect to Access DB instead.
Just in case you need the code:
Dim sqlDelete As String
Dim sqlAppend As String
sqlDelete = "DELETE * FROM dbo_Table;"
sqlAppend = "INSERT INTO dbo_Table (Col1,Col2) SELECT Col1,Col2 FROM passThrough;"
With DoCmd
.SetWarnings False
.RunSQL sqlDelete
.RunSQL sqlAppend
.SetWarnings True
End With
Application.SetOption "Auto Compact", True
If you need to truncate the data and load again I would recommend to move all tables, which should be truncated and all temporary tables to separate database.
After separating it will be possible to replace this database with unnecessary data by empty database file when the application starts. Just save a copy of empty database and copy this file over existing database with old data. This should be done before opening any form/recordset based on tables from temp database. It will work faster than C&R each time and more reliable, because sometimes C&R may damage the file.
Template database file also can be stored in main database in Memo field
I will refer this forum post: http://www.utteraccess.com/forum/index.php?showtopic=1561733

calling oracle stored procedure in ms access without saving password in pass through query

I am working on MS ACCESS 2003, i need to call oracle stored procedure through ms access. I am using pass through query to call the procedure. I have created user DSN with Microsoft driver for ODBC and successfully able to call the procedure.
ODBC;DSN=DSN_NAME;UID=USER_NAME;PWD=*******;DBQ=MY_SERVER
But my worry is to save the password in pass through query, some times password changes then i have to change the connection string for that pass through query every time.
Is there any other way to calling the procedure via pass through query without changing the connection string when password gets change or is there any code in vba to doing the same. I am looking for dynamic way where calling procedure would be easy without changing password to each and every pass through query by going to its properties.
Thanks !!
Yes this is possible however you need to know or in other words be able to retrieve the password somehow within your application. In a production line application you would use the user(login) password to create a connection string to access the back-end tables.
The structure would look similar to this:
Create a module to host connection related public functions.
Create new function GET_CONNECTION_STRING() as String: Which will return your connection-string including logged in user's password.
loop through the table definitions/ query definitions in your database and update the .connect property.
In most cases you would change the .connect property and use the .RefreshLink to refresh/connect manually.
You need to add a new reference to use DAO object. (Microsoft dao objets, or activex data obects or ado objects whichever type you want to use)
some startup code:
dim db as dao.database
set db = currentdb
dim tdf as dao.tabledef
For Each tdf In db.TableDefs
If tdf.connect <> vbNullString Then
tdf.connect = GET_CONNECTION_STRING & ";TABLE=" & tdf.name
'if you want to manually refresh uncomment below line
'tdf.refreshlink
End If
End If
Next tdf
adding above function to a startup macro will ensure all the linked tables (queries you need to perform extra) are updated with the newest connectionstring.
try and post your code when you are stuck.
Any linked table(s) and that of including “saved” pass-though quires in Access does NOT require the password to be included in that string. If you leave out the password, then a SINGLE logon to the Oracle database will THEN allow ALL linked tables and pass though quires to run and do so without a password.
The first step is to setup your tables as DSN less, and DON’T include the password (you likely best to delete existing linked tables).
To link using above but NOT include pass-word means you FIRST will have to execute a logon into the database. Once done, then you can link your tables and pass-though quires. So a one time link of tables as DSN less and you are off to the races.
Once above is done, then any and all connections (including pass-though query) will all work and work without having to include the user name.
The above thus means you don’t have to re-link for different users logging into the database.
To run a pass-through query, you can then use this one line of VBA code:
CurrentDb.Execute.QueryDefs("MyPassQuery").execute
How to link with DNS-less is outlined here:
http://www.accessmvp.com/djsteele/DSNLessLinks.html
How to “cache” the user logon, and NOT require user + logon in the linked connections or ones used for pass-though is outlined here:
Power Tip: Improve the security of database connections
http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx
So you can on startup execute the logon one time, or prompt the user for a logon, and from that point on your application and pass-=though queries will run without required a password. And this ALSO means you can have different users logon and NOT have to re-linked the existing tables (and pass-though quires).

Migrating Table Macros to VBA with a now split Database

What i have is the Issues template from microsoft which is all i really need for its purpose. I have converted it so that is now no longer a web database by exporting objects as client objects.
I want to split this database so that not only multiple users can utilise it at the same time but also so it performs better.
When i split the database the macros linked to the tables go to the backend so when the front end needs to use them it errors.
Below is and example of the add comments macro that gets called when the add comment button is pressed on the front end.
And this is the macro embedded to the button
Can these be converted to VBA and interact with the backend the way it is meant to and if so where would i start. I have looked for an answer but all i find is people saying "it is fine now i have gone vba route" or similar but not actually showing it working.
Below is the converted data macro to VBA. It isn't 100% there yet as I have hard-coded the userID but this will be fixed later today, but I hope it gives a good understanding of how to convert data macros to VBA because this was a learning experience for me.
Private Sub cmdAddaComment_Click()
Dim db As dao.Database, theComments As Recordset
Set db = DBEngine.Workspaces(0).Databases(0)
Set theComments = db.OpenRecordset("Comments")
theComments.MoveLast
theComments.AddNew
theComments!IssueID = Me.ID
theComments!CommentDate = Now
theComments!Comment = Me.txtAddComment
theComments!UserID = 2
theComments.Update
Me.txtAddComment = ""
DoCmd.RepaintObject acForm, "IssueDetail"
End Sub
Before doing this, make a backup of your database. You can do that by closing down the database, then locate the .accdb file on your computer, and press Ctrl+C and Ctrl+V
You can do your own split like this:
Create a new, blank database.
Import everything EXCEPT THE TABLES from the old database
In the new database, create links to the old database tables. To do that, click on External Data - Import & Link, and then click on Access. Select the Link option, then locate the old database, and select all the tables you want to link. Note that if you see any tables that are named like "MSYS", do NOT import those. Those are system tables, and Access will handle those internally.
Now delete everything EXCEPT THE TABLES from the old database.
Your new database is now "linked" to the old database's tables, but all Forms, Reports, etc are in the new database.
If you want to split an Access DB, you have to separate TABLES from all the rest.
Queries, Macros, Forms, Reports and VBA code have to stay on the frontend, and only tables on the backend.
What you are missing are the LINKS from the frontend to the backend tables.
To do that you need to create a copy of your DB, renaming it "BE.mdb" (or accdb), delete all your objects except tables from BE.mdb.
Now on the original mdb, you need to delete every table and add a link for every deleted table to the BE.mdb corresponding table. This can be done from the Import menu, choosing "link table" instead of "Import table".

Access: notify when linked table source changes structure?

I have imported a table in Access 2007 through Oracle ODBC. My problem is that sometimes the tables change structure in Oracle (eg new columns are added) and when that happens Access doesn't automatically pick up the changes in its linked table.
Instead it keeps using the old structure and even worse some rows simply won't show up in the Access queries (I don't know why?).
The other problem is that I don't have any control over the Oracle DB so the changes can happen any time. Manually updating the linked tables all the time is too much of a hassle. Is it possible to somehow set Access up to notify me of the changes? I mean, somehow Access must be able to tell that something has changed - the question is; can it tell me?
Regards,
John
Run this function - you can either link it to a form Open event, or just run it when you need to refresh the data
Sub relinkTables()
Dim tdf As DAO.TableDef
For Each tdf In CurrentDb.TableDefs
' check if table is a linked table
If Len(tdf.Connect) > 0 Then
tdf.Connect = "your odbc connection string to the DSN or database"
tdf.RefreshLink
End If
Next
End Sub

Connect to ODBC from Access (Newbie)

I am spreading my wings into ODBC but need a kick-start. I have the SQL for the query and have ODBC installed (it works in Excel; that's how I got the SQL for the query). I need to know how to connect to the database from Access and pull the data into my table. Can someone please help with that?
Thank you,
Dave
Do you wish to use Excel data in Access? If so, simply link the spreadsheet (External Data).
EDIT
You can also link tables from other Access databases to easily create queries in the Query Design Window.
DAO is native for Access, here are a few notes:
Dim db As Database
Dim rs As DAO.Recordset
Set db = OpenCurrentDatabase("c:\docs\MyDb.mdb")
Set rs= db.OpenRecordset("Select Field From Table")
db.Execute "Update Table Set Field=2", dbFailOnError
I don't have MSAccess installed on this machine, so I might have the menu names incorrect.
If I understand correctly you're just trying to get MSAccess to connect to an ODBC database.
Go to the Main menu. Select "File"/"External Data"/"Link" ...
A wizard will step you through the connection set-up and show you the tables available in the database you're connecting to.
Once you've selected all the tables you want to work with, Access imports the definitions and they behave mostly like the local tables.
also see
http://www.aspfree.com/c/a/Microsoft-Access/Importing-Data-into-MS-Access-with-ODBC/1/