Reputation: 67
I created a form that imports a csv file as a new table called "ImportTable". I have queries to look at the data.
Once finished with the data I want to delete the table entirely (as part of the client workflow requirements). I'm stuck on how to apply a delete_table function to an on click event.
I put together the below function to delete the table. How can I apply this to an on click event?
The button name is: cmdDeleteImportTable_Click()
Public Function table_exists(tblName As String) As Boolean
'Returns True if table exists in current DB. Else False
Dim tdf As TableDef
For Each tdf In CurrentDb.TableDefs
If StrComp(tblName, tdf.Name) = 0 Then
table_exists = True
Exit For
End If
Next tdf
End Function
Public Function delete_table(tblName As String) As Boolean
'Returns true if the table is deleted or if the table does not exist
On Error GoTo errHandler
If table_exists(tblName) Then
DoCmd.DeleteObject acTable, tblName
End If
delete_table = True
ExitSuccess:
Exit Function
errHandler:
Debug.Print Err.Number, Err.Description
Resume ExitSuccess
End Function
Sub Test()
Const Names_Table = "ImportTable"
DoCmd.SetWarnings False
Debug.Print delete_table(Names_Table)
DoCmd.SetWarnings True
End Sub
Upvotes: 0
Views: 236
Reputation: 5958
Your code seems correct.
All you need to do now is add it a button. In Access Forms you have to select the button, then in Events tab, you have On Click. Select it and choose code.
This will create an event handler for the click event. Inside this event handler, you can call your code to import data or delete your table.
Further detais Here
Upvotes: 2