Results 1 to 2 of 2
  1. #1
    ke7khp is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2015
    Posts
    1

    MS Access Transposer problem

    I am transposing multiple queries into different tables at one time using:
    Function transposer(strSource As String, strTarget As String)

    Dim db As Database
    Dim tdfNewDef As TableDef
    Dim fldNewField As Field
    Dim rstSource As Recordset, rstTarget As Recordset
    Dim i As Integer, j As Integer

    On Error GoTo Transposer_Err

    Set db = CurrentDb()
    Set rstSource = db.OpenRecordset(strSource)
    rstSource.MoveLast

    ' Create a new table to hold the transposed data.
    ' Create a field for each record in the original table.
    Set tdfNewDef = db.CreateTableDef(strTarget)
    For i = 0 To rstSource.RecordCount
    Set fldNewField = tdfNewDef.CreateField(CStr(i + 1), dbMemo)
    tdfNewDef.Fields.Append fldNewField
    Next i
    db.TableDefs.Append tdfNewDef

    ' Open the new table and fill the first field with
    ' field names from the original table.
    Set rstTarget = db.OpenRecordset(strTarget)
    For i = 0 To rstSource.Fields.Count - 1
    With rstTarget
    .AddNew
    .Fields(0) = rstSource.Fields(i).Name
    .Update
    End With
    Next i

    rstSource.MoveFirst
    rstTarget.MoveFirst
    ' Fill each column of the new table
    ' with a record from the original table.
    For j = 0 To rstSource.Fields.Count - 1
    ' Begin with the second field, because the first field
    ' already contains the field names.
    For i = 1 To rstTarget.Fields.Count - 1
    With rstTarget


    .Edit
    .Fields(i) = rstSource.Fields(j)
    rstSource.MoveNext
    .Update
    End With

    Next i
    rstSource.MoveFirst
    rstTarget.MoveNext
    Next j

    db.Close

    Exit Function

    Transposer_Err:

    Select Case Err
    Case 3010
    MsgBox "The table " & strTarget & " already exists."
    Case 3078
    MsgBox "The table " & strSource & " doesn't exist."
    Case Else
    MsgBox CStr(Err) & " " & Err.Description
    End Select

    Exit Function

    End Function




    docmd.deleteobject actable, "transposed table"
    docmd.runcommand accmddebugwindow
    debug.print transposer("qrynon-transposed","transposed table")

    docmd.deleteobject actable, "transposed table1"
    docmd.runcommand accmddebugwindow
    debug.print transposer("qrynon-transposed1","transposed table1")

    docmd.deleteobject actable, "transposed table2"
    docmd.runcommand accmddebugwindow
    debug.print transposer("qrynon-transposed2","transposed table2")

    docmd.deleteobject actable, "transposed table3"
    docmd.runcommand accmddebugwindow
    debug.print transposer("qrynon-transposed3","transposed table3")

    docmd.deleteobject actable, "transposed table4"
    docmd.runcommand accmddebugwindow
    debug.print transposer("qrynon-transposed4","transposed table4")

    The problem is that the contents that are in each individual queries are getting into each others tables instead of their own. Example - The contents for transposed table2 sometimes get into transposed table4 and visa versa. The contents will only go into the correct table right after the macro is ran again.

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    very difficult to read your code, please edit your post or repost, this time indenting and use the code tags

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

Similar Threads

  1. Replies: 7
    Last Post: 06-01-2015, 04:04 PM
  2. Replies: 6
    Last Post: 02-18-2012, 07:20 AM
  3. Replies: 2
    Last Post: 06-18-2011, 09:55 AM
  4. Access Problem
    By pverdol in forum Access
    Replies: 5
    Last Post: 03-20-2011, 07:55 PM
  5. Access 2007 to Access 2010 Problem
    By evalmedi in forum Access
    Replies: 1
    Last Post: 10-24-2010, 02:56 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