Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IfObjectExists - Need help please

Status
Not open for further replies.
May 5, 2000
168
0
0
US
I am having a problem getting this to work for me.&nbsp;&nbsp;I want use the IfObjectExists to verify if a table exists, and if it does, delete it, otherwise go on to the next table.&nbsp;&nbsp;I want it to be in the click event of a procedure.&nbsp;&nbsp;I found the function in FAQ, but it doesn't seem to work for what I want.&nbsp;&nbsp;I have pasted my code below.&nbsp;&nbsp;Could anyone be kind enough to fix my code so that it works.&nbsp;&nbsp;I'd really appreciate it.<br><br>Private Sub RunUpdate_Click()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;If ObjectExists(acTable, &quot;PROJECT&quot;) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.DeleteObject acTable, &quot;PROJECT&quot; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;If ObjectExists(acTable, &quot;VENDOR&quot;) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.DeleteObject acTable, &quot;VENDOR&quot; else<br><br>&nbsp;&nbsp;&nbsp;&nbsp;If ObjectExists(acTable, &quot;EMPLOYEE&quot;) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.DeleteObject acTable, &quot;EMPLOYEE&quot; else <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;If ObjectExists(acTable, &quot;Exprate&quot;) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.DeleteObject acTable, &quot;Exprate&quot; else<br>&nbsp;&nbsp;&nbsp;&nbsp;EndIf<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro _<br>&nbsp;&nbsp;&nbsp;&nbsp;2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;,&nbsp;&nbsp;_<br>&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;project.dbf&quot;, &quot;PROJECT&quot;, False<br><br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro _<br>&nbsp;&nbsp;&nbsp;&nbsp;2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;EMPLOYEE.dbf&quot;, &quot;EMPLOYEE&quot;, False<br><br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro _<br>&nbsp;&nbsp;&nbsp;&nbsp;2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;time.dbf&quot;, &quot;VENDOR&quot;, False<br><br>&nbsp;&nbsp;&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro _<br>&nbsp;&nbsp;&nbsp;&nbsp;2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;timearc.dbf&quot;, &quot;EXPRATE&quot;, False<br><br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;'This statement checks to see if there isn't already a file with<br>&nbsp;&nbsp;&nbsp;'the name of the compacted database.<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;If Dir(&quot;Budget.mdb&quot;) &lt;&gt; &quot;&quot; Then _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kill &quot;Budget.mdb&quot;<br>&nbsp;&nbsp;&nbsp;EndIf <br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;'This statement creates a compact version of the Budget database.<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;DBEngine.CompactDatabase &quot;Budget.mdb&quot;, &quot;Budget.mdb&quot;<br>&nbsp;&nbsp;&nbsp;<br>End Sub&nbsp;&nbsp;&nbsp;<br>
 
Try this:<br><br><br>Private Sub RunUpdate_Click()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.<br>&nbsp;&nbsp;Docmd.Hourglass True<br><br>&nbsp;&nbsp;If ObjectExists(acTable, &quot;PROJECT&quot;) Then DoCmd.DeleteObject acTable, &quot;PROJECT&quot;<br>&nbsp;&nbsp;If ObjectExists(acTable, &quot;VENDOR&quot;) Then DoCmd.DeleteObject acTable, &quot;VENDOR&quot;<br>&nbsp;&nbsp;If ObjectExists(acTable, &quot;EMPLOYEE&quot;) Then DoCmd.DeleteObject acTable, &quot;EMPLOYEE&quot;<br>&nbsp;&nbsp;If ObjectExists(acTable, &quot;Exprate&quot;) Then DoCmd.DeleteObject acTable, &quot;Exprate&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro 2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;project.dbf&quot;, &quot;PROJECT&quot;, False<br>&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro 2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;EMPLOYEE.dbf&quot;, &quot;EMPLOYEE&quot;, False<br>&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro 2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;time.dbf&quot;, &quot;VENDOR&quot;, False<br>&nbsp;&nbsp;DoCmd.TransferDatabase acImport, &quot;FoxPro 2.6&quot;, &quot;\\gensrvcs\sys\S480\data&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acTable, &quot;timearc.dbf&quot;, &quot;EXPRATE&quot;, False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;If Dir(&quot;Budget.mdb&quot;) &lt;&gt; &quot;&quot; Then Kill &quot;Budget.mdb&quot;<br>&nbsp;&nbsp;DBEngine.CompactDatabase &quot;Budget.mdb&quot;, &quot;Budget.mdb&quot;<br><br>&nbsp;&nbsp;Docmd.Hourglass False<br>&nbsp;&nbsp;MsgBox &quot;Process Complete&quot;<br>End Sub&nbsp;&nbsp;&nbsp; <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Thank you so much.&nbsp;&nbsp;It works like a charm.&nbsp;&nbsp;I used the function below that was suggested in FAQ's for compacting the database, it works great too.&nbsp;&nbsp;I don't know what I'd do without this forum, I've learned so much just from reading and printing all the good posts.<br><br>Function compact_database()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;SendKeys (&quot;%(W1)&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;SendKeys (&quot;%(TDC)&quot;)<br><br>End Function
 
I believe that's what the creators of these forums had in mind. They (Dave & Doug) should be commended for the diligence on making this such a great forum. They really do all the work, all we do is respond. Glad to help. <p>Jim Lunde<br><a href=mailto:compugeeks@hotmail.com>compugeeks@hotmail.com</a><br><a href= Application Development
 
Jane,<br><br>About the compact_database function, I hope you realize that it will only work in the English version of Access. That code will break in any other language.<br><br>Why not use instead the CompactDatabase method:<br><br>DBEngine.CompactDatabase &quot;OldName.mdb&quot;, &quot;NewName.mdb&quot;<br><br>Julio<br>
 
May I have a copy of the ObjectExists() function. It must be a handy one.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top