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
Related
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
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
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
I'm looking for a way to implement SCM over multiple Access 07 databases. We just need source control for the forms/code.
Essentially we have 100+ databases that are structured the same, use the same code/modules, but each contains data for just one client. When we make a code change we currently have to manually go through each file to make the change.
Has anyone implemented source control for something similar(God help you if so) or have any ideas?
PS - I realize there is lots of DailyWTFery in here, this is a legacy product I've been assigned to do some emergency maintenance on before we rewrite to .NET/MSSQL, but I think there's enough work to warrant putting this in place if it's possible.
You can find out more how to do SCM in this question; it does involve exporting and importing the code and forms using some undocumented (or poorly documented) commands. I also recall issues with checksums or version numbers happening when you used that method.
However, you could solve a lot of this problem by separating the the data and the application sides of the DB into separate files and then adding table links to the data DB from the application DB. Then you would have just one Application DB, and oodles of Client data DBs.
Switching to a different client is as simple as re-linking to the other DB.
You can do that manually or code it with a structure something like this:
Dim myTable As TableDef
Dim db As Database
dim strDBPath as string
strDBPath = ";DATABASE=C:\workbench\MyNewDB.mdb"
Set db = CurrentDb
db.TableDefs.Refresh
For Each myTable In db.TableDefs
If Len(myTable.Connect) > 0 Then
myTable.Connect = strDBPath
myTable.RefreshLink
End If
Next
'' Note: This whips through every table in your database and re-links it to the new
'' DB. ODBC links will have a different value for strDBPath - I'll leave that as
'' and exercise for the reader
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/