Your question includes the following two:
- How to check if db exists and if db.tables contains my table?
- Am I right that this code creates db for ms-access 2007?
For the first part # 1, use the Dir () function.
If Len(Dir("C:\SomeFolder\YourDb.mdb")) > 0 Then Debug.Print "db exists" Else Debug.Print "db not found" End If
For part two # 1, try this feature. pTable is the name of the table you are checking. pDbPath is the full path, including the file name, for the db file you want to examine. The path can be one that starts with a drive letter or it can be a UNC path (\\ Server \ Share \ YourDb.mdb).
Public Function TableExists(ByVal pTable As String, _ Optional ByVal pDbPath As String) As Boolean 'return True if pTable exists as either a native or linked table ' 'pass any error to caller ' Dim blnReturn As Boolean Dim db As DAO.Database Dim tdf As DAO.TableDef If Len(Trim(pDbPath)) > 0 Then Set db = OpenDatabase(pDbPath) Else Set db = CurrentDb End If For Each tdf In db.TableDefs If tdf.Name = pTable Then blnReturn = True Exit For End If Next tdf Set tdf = Nothing If Len(Trim(pDbPath)) > 0 Then db.Close End If Set db = Nothing TableExists = blnReturn End Function
As for your second question, not a single code that you showed us creates a db file for any version of Access. If db_file
is not the path to an existing db file, this code throws an error in conn.Open
. It does not create the missing db file.
However, I doubt that the code will compile as VBA, despite the fact that you included VBA in your header and marked your question as vba. In fact, you should try it first before including it in the stack overflow question.
source share