Solved

VBA - Controlling Access from Word:  Create/copy table including lookup details

Posted on 2008-06-24
3
399 Views
Last Modified: 2012-06-21
I need to be able to create or copy an Access table without opening the database.  Thanks to the helpful response to a question I posted yesterday, I can now create the table.  However, I need one of the fields to refer to another table (in the same database) as a lookup field.  Is it possible to achieve this either as part of the initial creation process, by modifying it afterwards or by copying a master copy of the table?  I've tried the DoCmd.CopyObject but this seems to require the database to be physically open which is not an option.

As always, many thanks in advance for any help.
0
Comment
Question by:MonteDelMar
  • 2
3 Comments
 
LVL 30

Assisted Solution

by:hnasr
hnasr earned 500 total points
ID: 21872039
Try this model, code in document tblTemp.doc Open event. Open the document, then look in drive C to see the .rtf output.
The code should list a table and Tables/queries behind Lookup fields.

Private Sub Document_Open()

    Dim db As DAO.Database

    Dim qd As QueryDef

    Dim td As TableDef

    Dim tdo As Object

    Dim rs As DAO.Recordset

    Dim Path As String

    Dim x As String

    Dim i As Integer

    Dim qn As String

    Dim objAccess As Object

    Set objAccess = CreateObject("Access.Application")

    Path = "E:\_desktopAlternative\_disaster\Database2.accdb"

   

   objAccess.opencurrentdatabase Path

   Set db = currentdb

   Set td = db.TableDefs("1")

   

   objAccess.Visible = False

   qn = "temp" & Format(Date, "yyyymmdd") & Format(Time, "hhmm")

   objAccess.DoCmd.OutputTo acOutputTable, "1", acFormatRTF, "c:\" & qn & "Table.rtf"

   

   On Error Resume Next

   Set rs = db.OpenRecordset(td.Name)

   For i = 0 To rs.RecordCount - 1

        x = td.Fields(i).Properties("RowSource")

        If Len(x) > 0 Then

            Set qd = objAccess.currentdb.CreateQueryDef(qn & i, x)

            objAccess.DoCmd.OutputTo acoutputquery, qd.Name, acFormatRTF, "C:\" & qn & i & "LookUP.rtf"

        End If

    rs.MoveNext

    Next i

    

    DoCmd.CloseDatabase
 

    DoCmd.Quit acQuitPrompt

End Sub

Open in new window

tblTemp.doc
0
 
LVL 30

Expert Comment

by:hnasr
ID: 21872046
Modify:
line 13 for database name, here it is "Datbase2.accdb"
line 21 for table name, here it is "1"
Good Luck!
0
 

Accepted Solution

by:
MonteDelMar earned 0 total points
ID: 21872817
Thank you very much for responding and for the code.

I'm sorry but I don't understand how I can adapt this to a) work without Access being opened (the reason I am trying to avoid this is so that Access doesn't need to be installed on the user's machine) and b) how to create another table within the database - this code exports the information to a .rtf file.  

Also the source table is empty, although I can add some dummy information if necessary - I noticed that this code requires at least one record.  

What I want to achieve is a copy of the source table within the same database and with the complete structure of the source table, including the lookup field and to do so, ideally, without physically opening Access.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

867 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

12 Experts available now in Live!

Get 1:1 Help Now