MS Access ODBC VBA Refresh Links - ms-access

I have an MS Access database with six (6) ODBC tables from Intuit QuickBooks Enterprise. I created the links manually, and they worked fine right off the bat. However, the links need to be refreshed daily. I attempted to write a VBA code to automate this process, however using tdf.RefreshLinks always failed. I always have to manually refresh the links. I discovered that the database name changes daily. Once it's refreshed, it's refreshed the rest of the day no matter if the PC is restarted or not. However, the next day the name is always different. This must be something that QuickBooks does. I think I need to be able to "grab" this database name in order to make my code work successfully. Has anyone ever heard of this? Does anyone know how to overcome this so that I can script the refreshing of the links, instead of manually refreshing them?
Since I have no reputation on this site, I am unable to post any images. But I can explain that my MSysObjects query shows the QuickBooks linked tables database name to be 33c292ce6be44bdca0cc8dd6c68594a0 and when I go to create a new link manually, the Connect to SQL Anywhere dialog box reveals on the Database tab the new day's database name: 4b2444e2c7ff4c37aa53d12a648f2fa0. I know once I refresh the links, this will be the database name that shows up in my MSysObjects query for the rest of the day. How do I get this so I can incorporate into VBA code? If this is truly the answer to automating the refreshing of the links?

RefreshLink should work fine:
Dim db As DAO.Database, tdf As DAO.TableDef
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection ' cnn as in connection
On Error Resume Next
cnn.Open YOURCONNECTIONSTRINGHERE
Set db = CurrentDb
For each tdf in db.TableDefs
tdf.Connect = YOURCONNECTIONSTRINGHERE
tdf.RefreshLink
Next tdf
db.Close
Set db = Nothing

Related

Changes in MSAccess VBA coding

I have been coding in MSAccess since the Win95 days. Over the years, these apps have been upgraded to the latest versions of MSAccess. I am guessing that the change occurred sometime after Access 2003.
I am sure that this is a trivial question, but I can't seem to find the answer in the documentation online.
In the old days, my Modules would look like this:
Option Compare Database
Private Sub PrintReports_Click()
Dim db As Database
Dim rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Classes", dbOpenDynaset)
...
Where Classes is usually a table or query in my database. This continues to work in older databases that have been converted to the latest version.
When I try to create a new database using the above code, I get error 13, type mismatch error, with no explanation of what is wrong and how to fix it.
So 2 questions:
Why does it work on old databases but not newly created ones, and what is the correct way to open and refer to my tables in my VBA code?
Thanks for your help,
Dan
Try to check "References", is it missing anything
Also Declare db as follows
Dim db As DAO.Database

MS Access queries display incorrectly

I have an access database with a form that runs 6 queries based on inputted values. A coworker went in to edit a query and instead of displaying the full query, Access is displaying the following
SELECT * FROM table WHERE 1 <> 1
I’ve tried opening a backup copy on a different computer as well as running Compact & Repair to no avail. The form is still running correctly, however.
Running Access 2016 and files displayed fine yesterday afternoon.
It's hard to say what happened without more details, but some queries can't be represented in design view. Editing such a query in design view trashes it.
Anyway, the query is lost. If you don't have a backup, you're out of luck. (Very rarely a temporary query still exists, you can iterate through the querydefs collection to view the SQL of all queries including temporary ones).
This doesn't solve the problem (sorry, not sure what's happened) but should let you retrieve the SQL if the query still runs as expected.
Erm... basically what #ErikvonAsmuth just suggested. :)
Public Sub Test()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("MyQueryName")
Debug.Print qdf.SQL
End Sub

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/

Is it possible to programmatically detect corrupt Access 2007 database tables?

Is it possible via code to programmatically (from .NET for example via SQL query) to ask an Access database if it is corrupt or have tables with corrupt rows in it?
//Andy
None of the application/database level objects have such an "isCorrupted" property.
Furthermore, corrupted databases do not have a standard behaviour. Depending on the situation, database might not open at all (file is not recognized as a valid mdb file). If it opens, error might occur immediately or when using/opening a specific object (table, form, or VBA code).
In these conditions, I do not think there is a positive answer to your question.
Note: In addition to the standard compact/repair option of Access, exporting object to other databases (or importing them from the corrupted database) as well as the non-documented .saveAsText command can be of great help.
Here is some VBA that can be used to check for an error in previous releases of Access. It may also suit 2007.
Sub CheckForErr(tablename)
Dim rs As dao.Recordset
Dim db As Database
Set db = CurrentDb
Set rs = db.OpenRecordset(tname)
With rs
Do While Not .EOF
For Each fld In rs.Fields
If IsError(rs(fld.Name)) Then
Debug.Print "Error"
End If
Next
.MoveNext
Loop
End With
rs.Close
Set rs = Nothing
End Sub
We had network issues causing corruption in an access database on our shared drive and I spent a lot of time cleaning up corrupted rows from a couple specific tables.
The memo fields in particular were a good indication of corruption because they are not stored inline like the rest of the data but are kept in a separate table. I could open up the corrupted table in datasheet mode and try to determine which rows were corrupted by putting the focus on a corrupted memo cell--if there was corruption then I would immediately get an error.
Corrupted Int and Date columns would have odd values (3/18/1890, -11100910, etc.) but would not actually throw any errors when I read their values.
This is why cycling through all records and fields in the database as shown in the VBA from a previous answer makes some sense but will perform most reliably only if you have memo field corruption.