Solved

Creating tables via ADO in Access 2007

Posted on 2008-06-24
4
1,052 Views
Last Modified: 2013-11-29
I'm trying to create a table using the attached VBA code.  However, when I try to execute it I get the following error

run-time error
-2147217859 (80040e3d)

The error occurs at line 30 "cat.Tables.Append tbl"  I've used the locals window and it states that the catalog object is no longer valid
Public Sub CreateInvoiceTable()
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
   
    'Create and connect a catalog object
    Set cat = New ADOX.Catalog
    cat.ActiveConnection = CurrentProject.Connection
     
    On Error Resume Next
    
    'If the table already exists, delete it
    cat.Tables.Delete "tblInvoice"
    On Error GoTo 0
    
    'Create the table definition in memory
    Set tbl = New ADOX.Table
    tbl.Name = "tblInvoice"
    
    'Create the new columns
    tbl.Columns.Append "InvoiceNo", adVarChar, 10
    
   
    'Create the remaining columns
    tbl.Columns.Append "InvoiceDate", adDBDate
    tbl.Columns.Append "CustomerID", adInteger
    tbl.Columns.Append "Comments", adVarChar, 50
    Debug.Print tbl.Name
    'Append the new table to the collection
 
    cat.Tables.Append tbl   'Error occurs here
    
  
    
    cat.Tables.Refresh
    Application.RefreshDatabaseWindow
    
    'Clean up
    cat.ActiveConnection = Nothing
    Set tbl = Nothing
    Set cat = Nothing
    
 
End Sub

Open in new window

0
Comment
Question by:chtullu135
  • 2
  • 2
4 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 21861186
try this

Public Sub CreateInvoiceTable()
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
   
    'Create and connect a catalog object
    Set cat = New ADOX.Catalog
    cat.ActiveConnection = CurrentProject.Connection
     
    On Error Resume Next
   
    'If the table already exists, delete it
    cat.Tables.Delete "tblInvoice"
    On Error GoTo 0
   
    'Create the table definition in memory
    Set tbl = New ADOX.Table
    tbl.Name = "tblInvoice"
   
    'Create the new columns
    tbl.Columns.Append "InvoiceNo", adVarWChar, 10
   
   
    'Create the remaining columns
    tbl.Columns.Append "InvoiceDate", adDate
    tbl.Columns.Append "CustomerID", adInteger
    tbl.Columns.Append "Comments", adVarWChar, 50
    Debug.Print tbl.Name
    'Append the new table to the collection
 
    cat.Tables.Append tbl   'Error occurs here
   
 
   
    cat.Tables.Refresh
    Application.RefreshDatabaseWindow
   
    'Clean up
    cat.ActiveConnection = Nothing
    Set tbl = Nothing
    Set cat = Nothing
   
 
End Sub
0
 

Author Comment

by:chtullu135
ID: 21861328
I ran the code you sent and it works.  I did a line by line comparison with my code and could find no difference.  I must be missing something.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 21861375
the lines with '<<<<<  were changed

   tbl.Columns.Append "InvoiceNo", adVarChar, 10      '<<<<<<
   
   
    'Create the remaining columns
    tbl.Columns.Append "InvoiceDate", adDBDate         '<<<<<<
    tbl.Columns.Append "CustomerID", adInteger
    tbl.Columns.Append "Comments", adVarChar, 50    '<<<<<<
0
 

Author Comment

by:chtullu135
ID: 21861544
Thanks for the info.  To be honest I've never run across adVarWChar before.  I'll research it.  Thanks again
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question