Modules

  • Thread starter Thread starter Christopher
  • Start date Start date
C

Christopher

Hi

I have two database, Can I access DB2 From DB1 to be able
to delete a table in DB1


Thanks
 
Christopher said:
Hi

I have two database, Can I access DB2 From DB1 to be able
to delete a table in DB1

I don't understand. If your code is running in DB1, why would you have
to access DB2 in order to delete a table in DB1 -- the database you're
currently *in*?
 
Christopher,

I presume that you mean you want to access db1 FROM db2
to delete a table in db1. This will work:

'This module is defined in database db2.
'
Public Sub test_remote_delete()
Dim db1 As New
Access.Application 'create an access
object variable.
Set db1 = CreateObject
("Access.application") 'instantiate the object
variable
db1.OpenCurrentDatabase "<complete path
name>\db1.mdb" 'point the variable at db1

db1.DoCmd.DeleteObject acTable, "Table1"
End Sub

Dave
 
Dave said:
Christopher,

I presume that you mean you want to access db1 FROM db2
to delete a table in db1. This will work:

'This module is defined in database db2.
'
Public Sub test_remote_delete()
Dim db1 As New
Access.Application 'create an access
object variable.
Set db1 = CreateObject
("Access.application") 'instantiate the object
variable
db1.OpenCurrentDatabase "<complete path
name>\db1.mdb" 'point the variable at db1

db1.DoCmd.DeleteObject acTable, "Table1"
End Sub

Dave

That's a reasonable guess, Dave. Let me suggest that it's not necessary
to open another instance of the Access application in order to delete a
table in another database, nor is it very efficient to do so. You can
open a DAO Database object that represents the external database, and
use that object's Execute to delete the table; e.g.,

'----- start of example code -----
Public Sub DeleteFromOtherDB( _
pstrDBPath As String, _
pstrTable As String)

' pstrDBPath is the fully qualifield path and file name
' of the foreign database, pstrTable is the name of
' the table to be deleted from that database.

' The error-handling for this routine is left up to the
' person who adapts it.

Dim dbOther As DAO.Database

Set dbOther = DBEngine.OpenDatabase(pstrDBPath)

dbOther.Execute "DROP TABLE [" & pstrTable & "]", dbFailOnError

dbOther.Close

Set dbOther = Nothing

End Sub

'----- end of example code -----

Note that this won't work for a secured database that is using a
different workgroup file than the one used for the current database.
Also, of the other database has a database password set, this routine
would have to be modified to supply it when opening the database.
 
Back
Top