Results 1 to 14 of 14

Thread: [RESOLVED] Check table exists in Access

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Resolved [RESOLVED] Check table exists in Access

    Is there any way to check if a table exist aand if so delete it?
    I was thinking this might do it:

    VB Code:
    1. If CurrentDb.TableDefs(TempTable).Name > "" Then
    2.      DoCmd.RunSQL "DROP TABLE TempTable;"
    3. End If

    but that hangs!

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Check table exists in Access

    It does accept the DROP TABLE bit... Thats in my code and executes fine... i just wanted a backup at the start of the code incase the application had been ended midway through the code

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Check table exists in Access

    Nope, you cant do it that way because you will generate a "Item not found in collection" error if it doesnt exist.

    Also, DROP TABLE... is supported.


    VB Code:
    1. 'You can .Delete a table this way...
    2. CurrentDb.TableDefs.Delete ("Table3")
    3.  
    4. 'You can iterate through the TableDefs collection looking for a match
    5. Dim i As Integer
    6. For i = 1 To CurrentDb.TableDefs.Count
    7.     If CurrentDb.TableDefs(i).Name = "Table3" Then
    8.         MsgBox "Table3 Exists!"
    9.         Exit For
    10.     End If
    11. Next
    And finally you can just error trap for the existance of the table in the collection.
    VB Code:
    1. On Error GoTo MyError
    2. If CurrentDb.TableDefs(TempTable).Name = "Table3" Then
    3.     MsgBox "Table Exists"
    4. End If
    5. Exit Sub
    6. MyError:
    7.     Msgbox "Table Does Not Exist"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Check table exists in Access

    Cheers Rob,

    ive now got:

    VB Code:
    1. For i = 1 To CurrentDb.TableDefs.Count
    2.         On Error GoTo Start
    3.         If CurrentDb.TableDefs(i).Name = "TempTable" Then
    4.             CurrentDb.TableDefs.Delete ("TempTable")
    5.             Exit For
    6.         End If
    7.     Next
    8. Start:

    which works beautifully!

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Check table exists in Access

    Thanks but you really dont need the "On Error GoTo Start" since we are iterating through the collection using the Index position instead of its Name.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [RESOLVED] Check table exists in Access

    Once again you are right, although for some reson the first time i did that it screwed up... it works anyhow.

    Thanks again!

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [RESOLVED] Check table exists in Access

    Sorry.... it's now still failing without the on error statement.... it runs through the loop but stops at the end of the loop and doesnt execute any more code!...

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Check table exists in Access

    Post what your code looks like.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [RESOLVED] Check table exists in Access

    VB Code:
    1. Public Function exportData(pPath As String, pDataSource As Integer)
    2.  
    3. On Error GoTo Err_exportData
    4.  
    5.     DoCmd.SetWarnings False
    6.    
    7.     Dim oApp As Excel.Application
    8.     Dim oWB As Excel.Workbook
    9.     Dim oWS As Excel.Worksheet
    10.     Dim i, j, tDataSource As Integer
    11.     Dim tPath, tTemp, tSQL As String
    12.     Dim tResult As VbMsgBoxResult
    13.    
    14.     For i = 1 To CurrentDb.TableDefs.Count
    15.         On Error GoTo Start
    16.         If CurrentDb.TableDefs(i).Name = "TempTable" Then
    17.             CurrentDb.TableDefs.Delete ("TempTable")
    18.             Exit For
    19.         End If
    20.     Next
    21.  
    22. Start:
    23.     If Not Len(Dir(pPath, vbDirectory)) > 0 Then
    24.         MkDir pPath
    25.     End If
    26.    
    27.     If Not pDataSource = -1 Then
    28.         tDataSource = pDataSource
    29.     Else
    30.         tDataSource = 5
    31.     End If
    32.    
    33.     tSQL = Switch(tDataSource = 0, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=7)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    34.                 tDataSource = 1, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=8)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    35.                 tDataSource = 2, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=9)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    36.                 tDataSource = 3, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=10)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    37.                 tDataSource = 4, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID WHERE (((pupils.Year)=11)) ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;", _
    38.                 tDataSource = 5, "SELECT pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction, behaviour.Reason, behaviour.ExclusionLength, teacher.TeacherName, faculty.FacultyName, period.Period INTO TempTable FROM teacher INNER JOIN (Sanctions INNER JOIN (pupils INNER JOIN (period INNER JOIN (faculty INNER JOIN behaviour ON faculty.FacultyID = behaviour.FacutyID) ON period.PeriodID = behaviour.PeriodID) ON pupils.PupilID = behaviour.PupilID) ON Sanctions.SanctionID = behaviour.SanctionID) ON teacher.TeacherID = behaviour.TeacherID ORDER BY pupils.Surname, pupils.Forename, behaviour.Date, Sanctions.Sanction;")
    39.    
    40.     tPath = pPath
    41.     tTemp = Right(tPath, 1)
    42.    
    43.     If Not tTemp = "\" Then
    44.         If Not tTemp = "/" Then
    45.             tPath = tPath & "\"
    46.         End If
    47.     End If
    48.    
    49.     tTemp = Switch(tDataSource = 0, "Year_7_" & Date, _
    50.                 tDataSource = 1, "Year_8_" & Date, _
    51.                 tDataSource = 2, "Year_9_" & Date, _
    52.                 tDataSource = 3, "Year_10_" & Date, _
    53.                 tDataSource = 4, "Year_11_" & Date, _
    54.                 tDataSource = 5, "Whole_School_" & Date)
    55.                
    56.     tPath = tPath & Replace(tTemp, "/", "_")
    57.    
    58.     If Len(Dir(tPath & ".xls")) > 0 Then
    59.         '   We know that the file exists.
    60.         tResult = MsgBox("File Already Exists, would you like to overwrite the existing file?", vbYesNo, "PK:Sanctions")
    61.         If tResult = vbNo Then
    62.             MsgBox "No file created", vbOKOnly
    63.             Exit Function
    64.         Else
    65.             Dim tDeleteFile As String
    66.             tDeleteFile = tPath & ".xls"
    67.             SetAttr tDeleteFile, vbNormal
    68.             Kill tDeleteFile
    69.         End If
    70.     End If
    71.  
    72.     Set oApp = New Excel.Application
    73.     Set oWB = oApp.Workbooks.Add
    74.    
    75.     DoCmd.RunSQL ("CREATE TABLE TempTable (Surname Text(50), Forename Text(50), [Date] DateTime, Sanction Text(50), Reason Text(255), ExclusionLength Long, TeacherName Text(50), FacultyName Text(50), Period Text(50));")
    76.     DoCmd.RunSQL (tSQL)
    77.        
    78.     oWB.Sheets("Sheet1").Cells(1, 1) = ""
    79.     oWB.Close SaveChanges:=True, Filename:=tPath
    80.        
    81.     Set oWB = Nothing
    82.     oApp.Quit
    83.     Set oApp = Nothing
    84.     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TempTable", tPath & ".xls"
    85.     DoCmd.RunSQL ("DROP TABLE TempTable;")
    86.        
    87.     Set oApp = New Excel.Application
    88.     Set oWB = oApp.Workbooks.Open(tPath & ".xls")
    89.    
    90.     For i = oWB.Sheets.Count To 1 Step -1
    91.         If Not oWB.Sheets(i).Name = "TempTable" Then
    92.             oWB.Sheets(i).Delete
    93.         End If
    94.     Next
    95.    
    96.     tTemp = Switch(tDataSource = 0, "Year_7_" & Date, _
    97.                    tDataSource = 1, "Year_8_" & Date, _
    98.                    tDataSource = 2, "Year_9_" & Date, _
    99.                    tDataSource = 3, "Year_10_" & Date, _
    100.                    tDataSource = 4, "Year_11_" & Date, _
    101.                    tDataSource = 5, "Whole_School_" & Date)
    102.    
    103.     tTemp = Replace(tTemp, "/", "_")
    104.     oWB.Sheets(1).Name = tTemp
    105.    
    106.     Set oWS = oWB.Worksheets(tTemp)
    107.    
    108.     oWS.Range("A1:I1").Select
    109.     oWS.Range("A1:I1").Font.Bold = True
    110.     With oWS.Range("A1:I1").Interior
    111.         .ColorIndex = 16
    112.         .Pattern = xlSolid
    113.     End With
    114.        
    115.     If Not oWS.Cells(3, 9).Value = "" Then
    116.         i = 3
    117.         Do While Not oWS.Cells(i, 9).Value = ""
    118.             With oWS.Range("A" & i & ":i" & i).Interior
    119.                 .ColorIndex = 15
    120.                 .Pattern = xlSolid
    121.             End With
    122.             i = i + 2
    123.         Loop
    124.     End If
    125.     i = 1
    126.     If Not oWS.Cells(2, 9).Value = "" Then
    127.         i = 1
    128.         Do While Not oWS.Cells(i, 9).Value = ""
    129.             i = i + 1
    130.         Loop
    131.     End If
    132.    
    133.     If i > 2 Then
    134.         i = i - 1
    135.     End If
    136.    
    137.     oWS.Range("A1:I" & i).Select
    138.     With oWS.Range("A1:I" & i).Borders(xlEdgeLeft)
    139.         .LineStyle = xlContinuous
    140.         .Weight = xlMedium
    141.         .ColorIndex = xlAutomatic
    142.     End With
    143.     With oWS.Range("A1:I" & i).Borders(xlEdgeTop)
    144.         .LineStyle = xlContinuous
    145.         .Weight = xlMedium
    146.         .ColorIndex = xlAutomatic
    147.     End With
    148.     With oWS.Range("A1:I" & i).Borders(xlEdgeBottom)
    149.         .LineStyle = xlContinuous
    150.         .Weight = xlMedium
    151.         .ColorIndex = xlAutomatic
    152.     End With
    153.     With oWS.Range("A1:I" & i).Borders(xlEdgeRight)
    154.         .LineStyle = xlContinuous
    155.         .Weight = xlMedium
    156.         .ColorIndex = xlAutomatic
    157.     End With
    158.     With oWS.Range("A1:I" & i).Borders(xlInsideVertical)
    159.         .LineStyle = xlContinuous
    160.         .Weight = xlThin
    161.         .ColorIndex = xlAutomatic
    162.     End With
    163.     If i > 1 Then
    164.         With oWS.Range("A1:I" & i).Borders(xlInsideHorizontal)
    165.             .LineStyle = xlContinuous
    166.             .Weight = xlThin
    167.             .ColorIndex = xlAutomatic
    168.         End With
    169.     End If
    170.     oWS.Columns("A").Insert (1)
    171.     oWS.Cells(1, 1).Select
    172.     oWS.Columns("A").ColumnWidth = 2.25
    173.     oWS.Rows(1).Insert (1)
    174.        
    175.     oWS.Cells(1, 1).Select
    176.     oWS.Columns.AutoFit
    177.        
    178.     oWB.Close SaveChanges:=True
    179.     Set oWB = Nothing
    180.     oApp.Quit
    181.     Set oApp = Nothing
    182.    
    183.     MsgBox "File """ & tPath & ".xls"" created successfully", vbOKOnly, "PK:Sanctions"
    184.        
    185. Exit_exportData:
    186.     DoCmd.SetWarnings True
    187.     Exit Function
    188.  
    189. Err_exportData:
    190.     If Err.Description = "Permission Denied" Then
    191.         MsgBox "The file """ & tPath & ".xls"" is currently running." & vbCrLf & "Click 'Ok' to stop the export, then close the file and restart the export.", vbExclamation
    192.     End If
    193.     Resume Exit_exportData
    194.    
    195.     MsgBox Err.Description
    196.     Resume Exit_exportData
    197.  
    198. End Function

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Check table exists in Access

    ? Is it working now? I see that you dont reset your error handler after the loop?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [RESOLVED] Check table exists in Access

    Yes, it works now... should i reset the error handler... i assumed that it would be local to the loop (as in java)

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] Check table exists in Access

    You need to set it back because there is no scope in the procedure for errors. Its procedural. So after the end if the test for table existance loop place your "On Error GoTo Err_exportData" code to tell vb that from this point on use this label for handling any errors in this procedure.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [RESOLVED] Check table exists in Access

    Oh, ok... thanks for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width