Results 1 to 9 of 9
  1. #1
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34

    Creating table with template (both in backend) only creates a link rather than table

    I have a template in the backend database that I want to use to create another table in the backend, just with a specific name and it only creates a link in the backend to the template. The link has the new name.

    Code:
    DoCmd.TransferDatabase acExport, "Microsoft Access", BackEnd, acTable, "Template", strNewTableName , True
    Previously, I created the new table in the front end, moved it to the back end with the statement above (except source table name = dest table name) which worked, then created the link. I'm trying to sreamline my code by just creating it in the backend, but it doesn't seem to be working.

    Any ideas?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Here's code from my db that creates table in backend.
    Code:
    Dim tdf As TableDef
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim db As DAO.Database
    
    If IsNull(Me.tbxTestNum) Then
        MsgBox "Must enter test number.", vbCritical, "Error"
    Else
        Set cn = New ADODB.Connection
        'connect to the backend database
        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & gstrBasePath & "Data\LabData.accdb'"
        'create the test table
        cn.Execute "CREATE TABLE " & Me.tbxTestNum & " (LabNum text(12) PRIMARY KEY Not Null, method text(30) Not Null);"
        'set table link
        Set tdf = CurrentDb.CreateTableDef(Me.tbxTestNum)
        tdf.SourceTableName = Me.tbxTestNum
        tdf.Connect = "; DATABASE=" & gstrBasePath & "Data\LabData.accdb"
        CurrentDb.TableDefs.Append tdf
        Set rs = New ADODB.Recordset
        rs.Open "SELECT * FROM BuildTable;", CurrentProject.Connection, adOpenStatic, adLockPessimistic
        'must use DAO to set AllowZeroLength property, I don't allow zero length fields and Access defaults to Yes
        Set db = DBEngine.OpenDatabase(gstrBasePath & "Data\LabData.accdb")
        While Not rs.EOF
            If rs!DataField <> "LabNum" And rs!DataField <> "method" Then
                'create field in new table
                cn.Execute "ALTER TABLE " & Me.tbxTestNum & " ADD COLUMN " & _
                            rs!DataField & " " & IIf(rs!DataType = "Boolean", "Bit", rs!DataType) & _
                            IIf(rs!DataType = "Text", "(" & rs!FieldSize & ")", "") & ";"
            End If
            If rs!DataType = "Text" Then
                'change the AllowZeroLength default Yes to No
                db.TableDefs(Me.tbxTestNum).Fields(rs!DataField).AllowZeroLength = False
            End If
            rs.MoveNext
        Wend
        
        rs.Close
        cn.Close
        db.Close
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    How are you defining backend... is that a string that you are creating elsewhere in your code?
    what about your 'strnewtablename' I assume that's a constructed string to create a unique table name.

    have you tried using debug.print for both of these to see what it's actually got for those two values.

    I used this:


    DoCmd.TransferDatabase acExport, "microsoft access", "C:\test\sample.accdb", acTable, "TestTable", "renamedTestTable", True

    and it created an empty tab;e in the target database (c:\test\sample.accdb) with the same structure as the original table.

  4. #4
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    I have figured out that the source table must be in the front end. If the source table is in the back end, Access creates a LINK to it with the new tables name. If anyone knows of a work around, let me know!

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I am confused by what you want to do.

    My code in post 2 creates a new table in the backend based on source table in the frontend then sets up link to the new table. Isn't that what you want?

    Actually only used the procedure couple times in 6 years.

    Are you having to regularly modify database with new tables? Sounds like a design flaw.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    Thanks for your reply. I have an application where the user can create new "Projects". When the project is created, I use a table as a template to create a table with the new project information. All my tables have been located in the backend, but I've had to move the template into the front end. If the souce is in the back end, Access ceates a link. If in the front end, it works as expected. It was driving me crazy until I figured that out.

    Thanks.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    A table for each project? WHY? Why does a new project require a separate table? Why not one table and just add records?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Seamus59 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    May 2013
    Posts
    34
    That describes my central table, but each project has multiple tables associated with it and many records in each table. To create these tables, I use templates to create the tables associated with a new project.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Still don't understand why need to create new tables. Sounds like a non-normalized structure. I understand a non-normalized structure might be best for a situation (I have that) but I still rarely have to create new table. I have to create a new table if the lab decides to institute a new test procedure. Each test requires its own table for its data. Separate tables to make data entry input and output simple. We do very little statistical analysis, just report the numbers.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. break the link to a backend table
    By markjkubicki in forum Programming
    Replies: 1
    Last Post: 06-28-2013, 10:49 PM
  2. Replies: 5
    Last Post: 11-13-2012, 12:16 PM
  3. Replies: 2
    Last Post: 08-01-2011, 11:35 PM
  4. Form that creates records from a template
    By techaddiction7 in forum Forms
    Replies: 6
    Last Post: 07-20-2011, 05:11 AM
  5. Replies: 5
    Last Post: 10-28-2010, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums