Solved

Creating tables via ADO in Access 2007

Posted on 2008-06-24
4
1,065 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

724 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