Thank you VERY much Mr. Steele!
the following code works perfectly to update all my tables.
________________________________________________________
Private Sub btnUpdateAll_Click()
On Error GoTo Err_btnUpdateAll_Click
Dim stDocName1 As String
Dim stDocName2 As String
' The following code deletes all the data in tables on the front end
' Code provided by Doug Steele - thank you.
Dim dbfrontend As DAO.Database
Set dbfrontend =
OpenDatabase("\\servername\sharename\foldername\Mydatabase.mdb")
dbfrontend.Execute "DELETE * FROM tblTable1", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable2", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable3", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable4", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable5", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable6", dbFailOnError
dbfrontend.Execute "DELETE * FROM tblTable7", dbFailOnError
dbfrontend.Close
Set dbfrontend = Nothing
' This runs a macro for all DELETE and INSERT queries on back end
stDocName1 = "UpdateTableData"
DoCmd.RunMacro stDocName1
' This runs a macro for all Append queries on front end
stDocName2 = "AppendFrontEndTables"
DoCmd.RunMacro stDocName2
Exit_btnUpdateAll_Click:
Exit Sub
Err_btnUpdateAll_Click:
MsgBox Err.Description
Resume Exit_btnUpdateAll_Click
End Sub
------------------------------------------------------
I also have individual buttons that update specific tables instead of doing
all of them.
A shorter version of the code is used.
In order to reduce the repetitive code for the following on each button:
Dim dbfrontend As DAO.Database
Set dbfrontend =
OpenDatabase("\\servername\sharename\foldername\Mydatabase.mdb")
Can it be done ONCE at the beginning of the form's module? Set the path ONCE
then
reference the path for each button?
Douglas J. Steele said:
My preference is to use
CurrentDb.Execute "DELETE FROM [F:\Folder\File.mdb].Table1", dbFailOnError
CurrentDb.Execute "DELETE FROM [F:\Folder\File.mdb].Table2", dbFailOnError
CurrentDb.Execute "DELETE FROM [F:\Folder\File.mdb].Table3", dbFailOnError
CurrentDb.Execute "DELETE FROM [F:\Folder\File.mdb].Table4", dbFailOnError
CurrentDb.Execute "DELETE FROM [F:\Folder\File.mdb].Table5", dbFailOnError
I prefer the Execute method for two reasons:
1) You don't get the annoying "You're about to delete n rows of data..."
prompts.
2) Because of the dbFailOnError parameter, a trappable error will be
raised if something goes wrong.
You could also use
Dim dbOther As DAO.Database
Set dbOther = OpenDatabase("F:\Folder\File.mdb")
dbOther.Execute "DELETE * FROM Table1", dbFailOnError
dbOther.Execute "DELETE * FROM Table2", dbFailOnError
dbOther.Execute "DELETE * FROM Table3", dbFailOnError
dbOther.Execute "DELETE * FROM Table4", dbFailOnError
dbOther.Execute "DELETE * FROM Table5", dbFailOnError
dbOther.Close
Set dbOther = Nothing
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
Presto said:
Will something like this work?
Dim dbOther As DAO.Database
Set dbOther = OpenDatabase("F:\Folder\File.mdb")
dbOther.DoCmd.SetWarnings False
dbOther.DoCmd.RunSQL "DELETE * FROM Table1"
dbOther.DoCmd.RunSQL "DELETE * FROM Table2"
dbOther.DoCmd.RunSQL "DELETE * FROM Table3"
dbOther.DoCmd.RunSQL "DELETE * FROM Table4"
dbOther.DoCmd.RunSQL "DELETE * FROM Table5"
dbOther.DoCmd.SetWarnings True
dbOther.Close
Set dbOther = Nothing