MS Access Copy Table Structure Only - ms-access

I have a number of Linked (ODBC) tables in my MSAccess DB.
I what I want to do is create a copy of each table (preferably the Structure Only).
ive tried,
DoCmd.CopyObject , "NewTableName", acTable, "SourceTableName"
DoCmd.TransferDatabase acImport, "Microsoft Access", _
SrcDatabase, acTable, SrcTable, DstTable, StructureOnly
but these just seem to make a copy of the linked table.

think ive got it sorted, but ill leave it open incase someone has a better solution,
DoCmd.RunSQL ("Drop Table [LocalTable]")
DoCmd.RunSQL ("SELECT * INTO [LocalTable] FROM [ODBCTable];")

Had the same problem. Found out that the "DoCmd.Transferdatabase" worked, but had to use "1" instead of the keyword "True". And if the source table is a linked table the destination table appears as linked as well (with the arrow in the table list), although it really is in the local database.

Related

VBA Access OutputTo without the table formatting

I'm having a lot of trouble trying to export a table (actually a query, but I made it create this table just to see if something changed) to a .csv file, I was using this line to make it:
DoCmd.TransferSpreadsheet acExport, , "TABLE", "TEST.csv", False
But it gives me error 3027 (something about read-only database or object), but I created this table, and as far as I'm concerned, it is possible to write on it!
Then I tried using this line here:
DoCmd.OutputTo acOutputTable, "TABLE", acFormatTXT, "test.csv"
And it works just fine. But When I came to see the results, they came with a table drawn around my data, and it's just horrible!
Anyways, it doesn't matter really which command I want, I'd appreciate any help as long as it works. Thanks!
TransferSpreadsheet is for spreadsheet files. Try:
DoCmd.TransferText acExportDelim, , "TABLE", "d:\path\TEST.csv", False

How can I refresh a linked table in VBA when it contains a multi-valued field?

I have some VBA that programmatically updates the location of linked tables so that the front end of my database can be easily pointed at a different back end, without needing to click around in the Linked Table Manager. The technique is explained in this Microsoft blog post. This should be as simple as running the following code for each linked table in CurrentDb.TableDefs:
tableDef.Connect = ";DATABASE=" & newBackEndPath
tableDef.RefreshLink
However, when the table contains a multi-valued field, Access provides the following unhelpful and misleading error message:
Run-time error '3125':
'[table name]' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
The same error appears when manually updating the links from the Linked Table Manager, although sometimes after a few attempts, the update will work without an error message. To work around the problem, I'm trying to refresh the link by creating a new TableDef and then renaming it over the old one:
Dim tdf As TableDef
Set tdf = CurrentDb.CreateTableDef(originalTableName & "_Copy")
tdf.Connect = ";DATABASE=" & newBackEndPath
tdf.SourceTableName = tableName
With CurrentDb.TableDefs
.Append tdf
.Refresh ' Required?
End With
RefreshDatabaseWindow ' Required?
DoEvents ' Required?
DoCmd.Rename originalTableNameName, acTable, originalTableNameName & "_Copy"
This usually works, but sometimes DoCmd.Rename throws the following run-time error:
Run-time error '7874':
Microsoft Access can't find the object '[table name]_Copy'.
If I enter the debugger, this usually triggers some kind of refresh and the _Copy table appears in the navigation pane. If I press F5 at this point, DoCmd.Rename executes with no problems. Clearly, there is some delay after the TableDef is appended before it can actually be found and renamed. I've added the TableDefs.Refresh, RefreshDatabaseWindow and DoEvents lines in an attempt to force the new table to appear, but they don't seem to help. Even forcing a delay of a few seconds doesn't seem to work, but somehow, opening the debugger does.
Is there any reliable way to refresh a linked table that contains a multi-valued field?
(I ignored the advice to avoid using multi-valued fields when I started this project, because I wanted users to be able to filter on multi-valued fields using the Quick Filters on a split form datasheet. This function is very helpful, but I'm beginning to wish I just built my own filtering UI after encountering this and numerous other bugs in the implementation of multi-valued fields.)
You could try a different approach altogether, by using DoCmd.TransferDatabase:
DoCmd.TransferDatabase acLink, "Microsoft Access", newBackEndPath, acTable, tableName, tableName
DoCmd.TransferDatabase is similar to using the GUI to link the table. It handles steps like refreshing the database pane for you. I usually recommend using TableDefs instead, but this situation might be an exception.
Of course, I support Sergey's advice to not use a multi-valued field. I'm a fan of the user experience of multi-valued fields and the associated comboboxes, but it has far-going consequences to use a MVF, like not being able to migrate your database to SQL server in the future

Export & Link a Local Table to a Backend DB?

I have a Local table (Table_A) that I want to periodically export/copy to a backend database as (Table_A_Snap). Each subsequent snapshot will overwrite prior snapshots.
The following snippet produces the following result:
Table_A_Snap is created in the Local DB
Table_A_Snap also created in the Backend DB
The desired LINK to the Backend table is not produced
Note: As the code runs I can actually momentarily see Table_A_Snap appear as a LINKED tale, but then it converts to a Local Table (no link)
TempVars!my_path = full path to backend DB
Snippet:
DoCmd.TransferDatabase acExport, "Microsoft Access", TempVars!my_path, acTable, "Table_A", "Table_A_Snap", -1
DoCmd.TransferDatabase acLink, "Microsoft Access", TempVars!my_path, acTable, "Table_A_Snap", "Table_A_Snap"
I am guessing this is a simple syntax issue. Any suggestions to remedy?
Resolved - Bonehead issue - I copied over the newly created LINKED table further down into code - Apologies! :)

MS Access Auto De-link and Re-link an Excel file?

I have a link-table from an Excel file, but sometimes it "breaks" for reasons unknown to me (other people manipulate this file externally and for whatever reason, the link in the database corrupts even though the filename is the same, etc).
The only way I can fix this issue is by delinking the file and then re-linking it. Is there a way to do this automatically when someone opens the database? (I know about autoexec macros and VBA and everything, but I was unable to find VBA code to delete the old link and re-link the file again).
if the file path does not change you can simply relink the source. You can do it to connect all the linked tables to be connected or you can specifically tell one linked table to be reconnected.
pseudo would be:
Go through your table collection
check if the table is a linked table / or any condition
use the .Connect property to set/renew the connectionstring/path for linked tables.
use the .RefreshLink method to reconnect the table
in code it would be something like this:
For Each tdf In db.TableDefs
If tdf.connect <> vbNullString Then
'you can renew the connectionstring if you want by
'tdf.connect = Your_connectionString & ";TABLE=" & tdf.name
'and to reconnect
tdf.RefreshLink
End If
Next tdf

VB does not see Table after DoCmd.TransferDatabase using OpenRecordset

I am using Do.CMD.TransferDatabase to copy in two tables from a second access database. The TransferDatabase command runs, and I see the table in the Access list of tables, but the VB code doesn't see it.
I suspect I've messed up the internal list of tables because I see the tables (
myTable
myTable1
myTable2)
If I leave a table there on purpose, Access seems to "see" the table enough to not over-write it and Access writes a copy, but then the same code does not see the existing table to openit.
Code snippets (sanitized):
{
Dim ws As Workspace
Dim myDB As Database
Set myDB = DBEngine.Workspaces(0).Databases(0)
DoCmd.TransferDatabase acImport, "Microsoft Access", filename, acTable,"myTable", "myTable", False
Dim OriginalInformation As Recordset
Set OriginalInformation = myDB.TableDefs("myTable").OpenRecordset
The code stops on the last line and give the error that the item is not found in the collection.
How do I copy in a table and then look at the contents within the same VB function?
The answer is to use myDB.TableDerrs.Refresh between the import and using the
myDB.TableDefs("myTable").OpenRecordset