Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, must concatenate variables into the sql. Reference to form control is a variable.
    Code:
    Set rstSource = db.OpenRecordset("SELECT SequenceNo, SamplePrefix, SampleID, SampleSuffix, ResultDate, Result " _
                                        & "FROM tblTestTypes " _
                                        & "INNER JOIN tblSampleLogIn INNER JOIN tblSampleData " _
                                        & "ON tblSampleLogIn.[AccessionNo] = tblSampleData.[AccessionNoFK] " _
                                        & "INNER JOIN tblResultsData " _
                                        & "ON tblSampleData.[SampleDataID] = tblResultsData.[SampleDataFK] " _
                                        & "ON tblTestTypes.[TestTypesID] = tblResultsData.[TestTypeFK] " _
                                        & "WHERE tblTestTypes.TestType= '" & [Forms]![Main].[NavigationSubform].[Form].[cboTestType] & "'" _
                                        & "AND tblSampleData.AccessionNoFK=" & [Forms]![Main].[NavigationSubform].[Form].[cboLongLabID] & ";", dbOpenSnapshot)
    Assumes TestType is text (see apostrophe delimiters) and AccessionNoFK is number (no delimiters). Date type uses # delimiter.

    Why searching on TestType and not TestTypesID? Is cboTestType a multi-column combobox with columns for TestTypeID and TestType fields?

    This code is executed by the main form?



    Doesn't look like need the strSource argument of the function. Remove it so VBA won't expect it.
    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.

  2. #17
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Thanks June7,

    I think that I'll have to spend more time with SQL. With the SQL you provided, I now get "3131 Syntax error in FROM clause." I tried a few things to fix this, but none were successful. Here is the current code now:

    Code:
    Function Transposer(strTarget As String)
    
       Dim db As DAO.Database
       Dim tdfNewDef As DAO.TableDef
       Dim fldNewField As DAO.Field
       Dim rstSource As DAO.Recordset, rstTarget As DAO.Recordset
       Dim i As Integer, j As Integer
    
       On Error GoTo Transposer_Err
       
       Set db = CurrentDb()
       Set rstSource = db.OpenRecordset("SELECT SequenceNo, SamplePrefix, SampleID, SampleSuffix, ResultDate, Result " _
                                        & "FROM tblTestTypes " _
                                        & "INNER JOIN tblSampleLogIn INNER JOIN tblSampleData " _
                                        & "ON tblSampleLogIn.[AccessionNo] = tblSampleData.[AccessionNoFK] " _
                                        & "INNER JOIN tblResultsData " _
                                        & "ON tblSampleData.[SampleDataID] = tblResultsData.[SampleDataFK] " _
                                        & "ON tblTestTypes.[TestTypesID] = tblResultsData.[TestTypeFK] " _
                                        & "WHERE tblTestTypes.TestType= '" & [Forms]![Main].[NavigationSubform].[Form].[cboTestType] & "'" _
                                        & "AND tblSampleData.AccessionNoFK=" & [Forms]![Main].[NavigationSubform].[Form].[cboLongLabID] & ";", dbOpenSnapshot)
       
       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), dbText)
          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 Else
             MsgBox CStr(Err) & " " & Err.Description
       End Select
    
       Exit Function
    
    End Function
    TestType = Text
    AccessionNoFK = Number

    Why searching on TestType and not TestTypesID? Is cboTestType a multi-column combobox with columns for TestTypeID and TestType fields?
    cboTestType is a single-column combobox with only one column for TestType field. I set it up to search on TestType, as opposed to TestTypeID, as it will be simpler for the user to search by the text [TestType], rather than searching by just a number in [TestTypeID]. Is there a more logical approach to this?

    This code is executed by the main form?
    Not yet. I am hoping to build this Transposer function first, then call the function from the main form (and most likely from reports later on).

  3. #18
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    How long are the TestType values? Is the TestType field indexed? Could have user make selection by the TestType value they see but code searches on the TestTypeID. If the TestType is a very short value, could just make it the pk and actually save it as fk instead of the autonumber.

    Sorry, looks like I removed essential parens:
    & "INNER JOIN ((tblSampleLogIn INNER JOIN tblSampleData " _
    & "ON tblSampleLogIn.[AccessionNo] = tblSampleData.[AccessionNoFK]) " _
    & "INNER JOIN tblResultsData " _
    & "ON tblSampleData.[SampleDataID] = tblResultsData.[SampleDataFK]) " _
    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.

  4. #19
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Thanks again June7,

    The SQL worked, and a new table is transposed. Now I'll start adjusting the code to get what I need (The first 4 columns to be static, the remaining columns transposed).

    How long are the TestType values?
    Right now I'm using dummy data as the values, but they can be anywhere from 3-15 characters (maybe more in the future).

    Is the TestType field indexed?
    No, should it be?

    If the TestType is a very short value, could just make it the pk and actually save it as fk instead of the autonumber.
    I see, but what would be the advantage of this? Would it make it easier to follow when coding? I don't think it's necessary to change this right now.

    Time to start playing with the Transposer()!!!

  5. #20
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Re indexing: http://office.microsoft.com/en-us/ac...010210347.aspx

    By making the TestType the pk instead of TestTypeID would not need join to the the TestType table to 'lookup' the related TestType. I avoid 'lookup' whenever I can. Review http://access.mvps.org/access/lookupfields.htm

    Another reason not identified in the link is use of dependent (cascading) comboboxes in Continuous or Datasheet forms. https://www.accessforums.net/access/...orm-17150.html
    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. #21
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    June7,

    I thank you for your time, help, and insight on this thread. However, I believe that we have wonder astray from the original problem of the thread: Normalized Design. Through your help and other online resources, I have arrived at a more normalized table/relationship design for the db. I would like to close this thread, and revisit the 'transpose' idea in another thread.

    Below is a pic of the more normalized db structure between the tblSampleLogIn, tblTestTypes, tblSampleData, and tblResultsData. To make the db more normalized in this respect, another many-to-many intermediate table was needed between tblTestTypes and tblSampleData. This is because one sample can have many Tests, and one test can have many Samples.
    Click image for larger version. 

Name:	Nml.png 
Views:	5 
Size:	53.1 KB 
ID:	8391

    Thanks again June7!

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help with design
    By Ray67 in forum Database Design
    Replies: 21
    Last Post: 05-01-2012, 01:06 PM
  2. Help with Design
    By anartey in forum Queries
    Replies: 1
    Last Post: 12-18-2011, 08:37 PM
  3. Design Help
    By jbevans in forum Database Design
    Replies: 3
    Last Post: 11-29-2011, 08:01 AM
  4. Help with Design
    By rbiggs in forum Database Design
    Replies: 8
    Last Post: 07-10-2011, 07:13 PM
  5. Design help
    By jacko311 in forum Database Design
    Replies: 0
    Last Post: 11-12-2009, 05:57 AM

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