Solved

Creating tables via ADO in Access 2007

Posted on 2008-06-24
4
1,044 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 119

Accepted Solution

by:
Rey Obrero 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 119

Expert Comment

by:Rey Obrero
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now