MS Access queries display incorrectly - ms-access

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

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

Fields not showing in design view

I'm working on an Access 2003 database, divided into front and back ends. I've attempted to compact and repair the database, which appears to do nothing.
After running various export processes I noticed that some of the columns reported as transferred didn't seem to tally with what was displayed in the design view.
Public Sub Test()
Dim db As Database
Dim td As TableDef
Dim f As field
Set db = CurrentDb
Set td = db.TableDefs("TBL tablename")
For Each f In td.Fields
Debug.Print f.Name
Debug.Print f.Properties("ColumnHidden").value
Next f
Set f = Nothing
Set td = Nothing
Set db = Nothing
End Sub
Further tests ascertained that the table actually had several fields, some of which were replication fields (the database had previously been replicated, but not anymore). More importantly, some of the fields were required data fields, containing data necessary to the project.
I have some questions regarding this:
Is there a setting in the background that allows the field to be hidden in design view?
If not, is this a good indicator of corruption?
Update
I've tested the theory further by writing a query to pull the data back, which also works fine:
...and the table design...
Update (06/06/2016)
I've written a process to extract as much data as possible from the database and drop it into a new one. Though I don't think Microsoft Access is a suitable platform for this application, at present it will have to suffice. In the future, I will attempt to move it to SQL Server to stablise its operation, but in the meantime it will have to serve as is.
I've posted the solution I used to do this here.
I'm going to accept the fact that this database is corrupt. There were too many issues boiling up, including:
Missing fields in tables
Tables not copying to other databases
Inability to compact and repair the database (the C&R would return instantly without doing anything)
Inability to remove the replication applied to the database

MS Access ODBC VBA Refresh Links

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

Open a report with data from a generated RecordSet

I'm fairly new to Access, VBA, and reporting. What I am trying to do is to take the results from a dynamic query that I created and open a report using that data.
My basic setup so far is a table with data that is entered by the user. At the end of the month they're supposed to generate a report. To generate this report I set up a form with VBA behind it to generate a query based on their input. Once that query is run I have it set to open the report.
Currently I have not figured out how to use that RecordSet as the data source for the report.
Could anyone please help me out with this?
The way I tend to handle situations like this is to create a saved query in Access, base the Report on that saved query, and then just update the .SQL property of the QueryDef object before opening the report.
For example, for the report [MyReport] I would create a saved query named [DataForMyReport] with some dummy SQL Statement in it. Then when the time comes to run the report for a given query I would just do
Dim cdb as DAO.Database, qdf as DAO.QueryDef
Set cdb = CurrentDb
Set qdf = cdb.QueryDefs("DataForMyReport")
qdf.SQL = strNewQueryToUse ' replace the SQL statement with the new one
Set qdf = Nothing
DoCmd.OpenReport "MyReport"
You can do 1 of 3 things:
1) Create a saved query and use that query as the report's RecordSource. The data in the query will change, but the query name will not. When the report opens, it will pull in the current results of the saved query. This is definitely the easiest way to do it.
2) When you open the report, set its RecordSource in code. I don't see a need to do this.
3) If you find it absolutely necessary to create a query on the fly and the name of that query might change, you can pass it as an OpenArgs. I wouldn't recommend this, though.

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