Results 1 to 3 of 3
  1. #1
    shubhamgandhi is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Jul 2011
    Posts
    22

    New tables created in a loop causing error

    I'm grabbing field row from one table and creating a new table for each row. The new tables will have names equal to the row they correspond to.

    Here is my code:

    Code:
    Option Compare Database
    
    Public Function createTables()
    
        Dim db As DAO.Database
        Dim tdf As DAO.TableDef
        Dim rst As DAO.Recordset
        Dim fld As DAO.Field
        Dim strSQL As String
    
        strSQL = "Select SKUS from SKUS"
        Set db = CurrentDb()
        Set rst = db.OpenRecordset(strSQL)
        Set fld = rst.Fields("SKUS")
    
        'MsgBox fld.Value
    
        rst.MoveFirst
        Do While Not rst.EOF
            Set tdf = db.CreateTableDef(fld.Value)
    
            Set fld = tdf.CreateField("SKUS", dbText, 30)
            tdf.Fields.Append fld
    
            Set fld = tdf.CreateField("Count", dbInteger)
            tdf.Fields.Append fld
    
            db.TableDefs.Append tdf
    
            rst.MoveNext
        Loop
    
    End Function
    The problem is that after the first iteration of the code (the first table is created), it gives me an error "Invalid operation" pointing to the line

    Code:
    ...
    Set tdf = db.CreateTableDef(fld.Value)
    ...
    Why do you think this is? I have a feeling its because I need to re set fld or rst, but I'm not sure.



    Can anyone help me out with this?
    Thanks!

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Dim db As Database
    Dim tdf As TableDef
    Dim rst As Recordset
    Dim fld As Field
    Dim strSQL As String
    Dim sTableName

    strSQL = "Select SKUS from SKUS"
    Set db = CurrentDb()
    Set rst = db.OpenRecordset(strSQL)
    rst.MoveFirst



    Do While rst.EOF <> True
    sTableName = rst.Fields("SKUS")
    Set tdf = db.CreateTableDef(sTableName)

    With tdf
    .Fields.Append .CreateField("FirstName", dbText, 30)
    .Fields.Append .CreateField("LastName", dbInteger)
    db.TableDefs.Append tdf
    End With

    rst.MoveNext
    Loop

  3. #3
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Why are you creating a table for each SKU? That is definitely NOT normalized.

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

Similar Threads

  1. Query Criteria Causing Data Mismatch Error
    By jrubenol in forum Queries
    Replies: 2
    Last Post: 09-14-2011, 09:34 AM
  2. Replies: 8
    Last Post: 05-16-2011, 06:01 PM
  3. Error: Loop without Do
    By eric.opperman1@gmail.com in forum Programming
    Replies: 4
    Last Post: 01-25-2011, 02:37 PM
  4. Replies: 9
    Last Post: 04-28-2010, 11:20 AM
  5. Replies: 1
    Last Post: 04-07-2010, 08:49 AM

Tags for this Thread

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