Peter Chan
asked on
Access issue
Hi,
In VBA, how to check if one table is existing or not in Access DB? And how to also duplicate existing table to another in Access DB?
In VBA, how to check if one table is existing or not in Access DB? And how to also duplicate existing table to another in Access DB?
ASKER
Hi,
Thanks a lot. How to run "select * into otherTab from ..." against Access DB?
Thanks a lot. How to run "select * into otherTab from ..." against Access DB?
Gustav answered this question:
Solved: Issue to code | Experts Exchange (experts-exchange.com)
Solved: Issue to code | Experts Exchange (experts-exchange.com)
ASKER
Sorry, I do not see the way to duplicate one table, from that.
Can you also advise to code below?
Can you also advise to code below?
It is just a matter of running a query:
SELECT *
INTO Table2
FROM [C:\Test\Test.accdb].Table1;
Function for testing table existance:
You can also use docmd for object copy
Public Function TblExists(ByVal strTableName As String) As Boolean
Dim tblN As Object
TblExists = False
For Each tblN In CurrentDb.TableDefs
If tblN.Name = strTableName Then
TblExists = True
Exit For
End If
Next tblN
End Function
You can also use docmd for object copy
Sub tst()
If TblExists("Table2") Then
' Delete Table2
DoCmd.DeleteObject acTable, "Table2"
End If
' Copy Table1 toTable 2 in CurrentDB
DoCmd.CopyObject , "Table2", acTable, "Table1"
End Sub
ASKER
ASKER
Als,
I've got attached error, like1g.png
due to For Each line below.
I've got attached error, like1g.png
due to For Each line below.
Public Function TblExists(ByVal strTableName As String) As Boolean
Dim tblN As Object
TblExists = False
For Each tblN In Adb.TableDefs
If tblN.Name = strTableName Then
TblExists = True
Exit For
End If
Next tblN
End Function
Dim Adb As Object: Set Adb = CreateObject("Access.Application")
Call Adb.OpenCurrentDatabase(F1)
Adb.Visible = False
Try other version of function (without objects):
Public Function TblExists(ByVal strTableName As String) As Boolean
TblExists = False
If Not IsNull(DLookup("Name", "MSysObjects", "Name = " & Chr(34) & strTableName & Chr(34) & " AND Type = 1")) Then TblExists = True
End Function
ASKER
I got
Sub or Function not defined
to DLookup in your code.
Sub or Function not defined
to DLookup in your code.
ASKER
Yes, I checked that OLE Automation was already selected in the reference.
Here's my article on object existence:
https://www.experts-exchange.com/articles/6670/Object-Existence.html
https://www.experts-exchange.com/articles/6670/Object-Existence.html
You had to have Microsoft Access XX Object library checked
ASKER
Sorry, which is the option per attached file?1k.png
Look for "Microsoft Access 16.0 Object Library"
Try attached sample
Database2.accdb
Database2.accdb
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks.
When writing to Access DB, is details of current active worksheet going to the table having same name of current work sheet?
When writing to Access DB, is details of current active worksheet going to the table having same name of current work sheet?
Nope, you never mentionned such detail.
It goes to T1 table.
The function I wrote can write data to an,y table. Adjust to fit your needs.
It goes to T1 table.
The function I wrote can write data to an,y table. Adjust to fit your needs.
Are you trying to copy table from Excel VBA?
Tables are represented by the TableDefs collection who's table name is the key.
So to check if the key is present, we can just attempt to retrieve it, if it fail, the table do not exist, and an error will be raised. We intercept it with an error handler:
Open in new window
To use it, just pass the TableDefs collection and table name arguments to the function:
Open in new window