Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69

    Lab DB Design

    I started creating a db, then found out that my db was not normalized. I am wanting to resolve this before going further.



    Right now, I have 5 main tables (there's more in the db, but I'm mostly concerned with these 5 tables for now).

    Click image for larger version. 

Name:	LabDBRelationships.png 
Views:	24 
Size:	24.1 KB 
ID:	7924

    With the data and the relationships:

    Each record in [tblSampleLogIn] can have many test types, and each test type can be associated with many sample logins (from [tblSampleLogIn]). Thus, I created a many-to-many relationship. This works fine.

    Where I am having trouble is with the sample data and the results. All of the samples in a batch needs to be related to the [tblSampleLogIn] somehow, while also being related to the test type (either [mmTestTypes] or [tblTestTypes]). Thus, I tried to create a query that would display the [tblSampleLogIn.LongLabID], [tblTestTypes], and [tbl.SampleData.SampleID]. However, the query didn't return any results. I think that there is probably a better way to relate the tables, but I just can't wrap my head around it. Any suggestions?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Show the query sql statement and/or provide file for analysis. Follow instructions at bottom of my post.

    Not understanding why the results need to be in a table separate from SampleData. Won't each SampleData record have a single result?

    Is each SampleData record for a single test type?
    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
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Thanks June7,

    I think I figured it out with the same table/relationship structure as pictured above. The problem was with the query I created. I'm still building a few things to test it out.

    To answer your questions:

    Not understanding why the results need to be in a table separate from SampleData. Won't each SampleData record have a single result?
    No, each sample can have multiple tests, and each test has at least one result (sometimes multiple results for the one test).

    Is each SampleData record for a single test type?
    No, I hope that the design shown above prevents this. If each SampleData record was for a single test type, then if a sample that was submitted requested multiple tests, then the sampleID would have to be entered for each test. What I hope to achieve with the design above is that the sampleID will only have to be entered once, regardless of the amount of tests performed. Do you think I'm headed in the right track? I'll post an update after I try testing it again.

    Thanks again June!

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I am confused by the SampleData table. SampleDataID is the primary key. What is SampleID for?
    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.

  5. #5
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    In [tblSampleData], SampleDataID is the pk, and SampleID is for the sample number (the identifying number of the sample). Some, but not all, of the samples will have a prefix and suffix. I decided to separate these from the SampleID to aid in sorting and searching later on. But, now I just realized that if not all of the samples contain a prefix and suffix, then there will be some null values in the records in the table [tblSampleData]. I'll address that later.

    Right now, I think that I am back at square one, with trying to get the table [tblResultsData] to query with all of the other tables shown above. Then, I plan on making a form based upon that query for entering the results.

    This first pic is of a query that I can get to work:
    Click image for larger version. 

Name:	qryDesign1.png 
Views:	6 
Size:	25.4 KB 
ID:	7946
    Click image for larger version. 

Name:	qryResult1.png 
Views:	7 
Size:	14.6 KB 
ID:	7947

    But then when I add the table [tblResultsData], I can't get the query to return anything.
    Click image for larger version. 

Name:	qryDesign2.png 
Views:	7 
Size:	33.1 KB 
ID:	7948
    Click image for larger version. 

Name:	qryResult2.png 
Views:	6 
Size:	10.0 KB 
ID:	7949
    Again, I would like to create a query such as this to make a form for entering in the results data. Maybe I still don't have the tables structured correctly? Any suggestions?

  6. #6
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    June, please wait for my next post. I have an update. I will post soon.

  7. #7
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    Ok, I found out why my query in post #5 above wasn't working: I didn't have any data in the table [tblResultsData]. Once I put in data in this table, then the query worked, but not exactly how I need.
    Click image for larger version. 

Name:	qryDesign3.png 
Views:	7 
Size:	31.6 KB 
ID:	7953
    The results of the query will return the same records from the table [tblSampleData] regardless of the TestType. How can I get it to where each sample will have it's own results based upon the test type?
    Click image for larger version. 

Name:	qryResult3.png 
Views:	8 
Size:	44.1 KB 
ID:	7954
    Below is a pic of the table [tblResultsData]
    Click image for larger version. 

Name:	tblResultsData.png 
Views:	7 
Size:	15.6 KB 
ID:	7956

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    How are they the same records - the ResultDate is different?

    Consider form/subform arrangement.

    Hard for me to evaluate data manipulation from pictures. I need to work with the data directly. If you want to provide, follow instructions at bottom of my post.
    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.

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

    I have scaled back the DB to make navigation a little simpler (I removed erroneous and irrelevant tables and forms, but not queries). I put the two major forms in a navigation form called [Main-June7]. From there, you should be able to see the two forms: one for sample login, and another for inserting results.

    What I can't figure out is how to insert results for the different tests. If I insert results for one test, then the results will show up for all tests.

    I left the subform tab control empty at the bottom of the Insert Results form. I wasn't able to get anything to work correctly, and I might need to use a different control type. Please have a look at the attached db below.

    Thank you!

    LabDBTest06062012-June7.zip

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You need to associate Resuts with SampleData. But because these related forms are subforms at same level, that gets tricky. Review http://www.fmsinc.com/MicrosoftAcces...edSubforms.asp

    However, yours is trickier because the forms are sub/subforms. If the above can be adapted to this arrangement, I've never seen it. It's hard enough when the forms are children of the same parent. Yours are grandchildren.

    Might make this work if main form bound to SampleLogIn, then a subform for SampleData and a subform (2 subforms?) for ResultsData.
    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.

  11. #11
    Jester0001 is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2012
    Location
    Indiana
    Posts
    69
    June,

    I think I know how to get what I need, but I don't think it will be easy.

    To get the proper data from a query, I created two combo boxes on the frmInsertResults that are parameters for qrySampleResults. Now, these combo boxes will successfully filter the query to return the data of interest. However, the data in the query isn't structured the way it should be. The data needs to be transposed.

    I tried to transpose the data using a Crosstab Query, but then figured out that this isn't what I need, as a Crosstab Query will calculate data. I don't need any data calculated in this scenario.

    Then, I read here, here, and here about using VBA to transpose data into another table, and UNION queries. However, I'm not exactly sure which one I need (VBA transpose or UNION) or should use. If I use the VBA approach, will a new table be created every time the code is run? Or will the table be overwritten every time the code is run?

    Below is a pic of the form [frmInsertResults] on the [Main] navigation form, along with qrySampleResults that shows the combo boxes used as a filter (not sure if that is correct terminology).
    Click image for larger version. 

Name:	frmqry1.jpg 
Views:	11 
Size:	91.4 KB 
ID:	8138

    Below is a pic of the query, along with the transposition I created manually in Excel.
    Click image for larger version. 

Name:	Transpose1.png 
Views:	7 
Size:	66.6 KB 
ID:	8139

    Ultimately, I want to have a subform (technically a sub/subform) in the [frmInsertResults] with this transposed data (in the bottom of the second pic) to enter the results for the different days.

    Would it be practical to use VBA or a UNION query to achieve this? If so, which one? And how can I write the code/SQL for it?

    Here is a copy of the updated db:
    LabDBTest06182012-June7-A.zip

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I use 'temp' tables only for output (reporting) processes. I have never set up a form with 'transposed' dataset. What would be the purpose - entry/edit would not feed into the original tables without more coding.

    A UNION query is not an editable dataset.

    Whether or not a new table is created or records are appended or records are deleted/replaced depends on what you want. Any of those options is possible and can be code by VBA.

    As for coding to 'transpose' data into 'temp' table, each situation is unique. Examples are provided in the links you reference. Make attempt and post code for analysis when you encounter issue.
    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.

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

    I copied and pasted the code below to transpose a table:

    Code:
    Function Transposer(strSource As String, 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(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), 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 3078
             MsgBox "The table " & strSource & " doesn't exist."
          Case Else
             MsgBox CStr(Err) & " " & Err.Description
       End Select
    
       Exit Function
    
    End Function
    I tested the function by typing this into the immediate window:

    Code:
    ?Transposer("tblSampleLogIn", "tblTempResults")
    Where [tblSampleLogIn] is a current table, and [tblTempResults] is a new table that is created and transposed from the first table. (it works)

    However, most of the data needed is contained in multiple tables. So, I tried to use a query in the argument, like so:

    Code:
    ?Transposer("qrySampleResults", "tblTempResults")
    When this is run in the immediate window, the error "3061 Too few parameters. Expected 2". I suspect that this is because the query has two parameters that use criteria from the form (see the first pic in my last post for the query and form). I even tried running the transposer with the query as the first argument with the form opened, and the query source criteria controls filled with data. The same error occurs.

    Before I start tweaking the VBA Transposer code more, I would like to try to get the proper data in the recordset first. How can I get it to make the recordset based upon the query results?

    Thanks again

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Seems to me that should work with the form open and criteria in the comboboxes.

    However, an alternative to opening a recordset of the query is to construct the query with VBA.

    Set rstSource = db.OpenRecordset(sql statement here)

    i.e.

    Set rstSource = db.OpenRecordset("SELECT * FROM [tablename] WHERE fieldname1='" & Forms!formname!controlname1 & "' AND fieldname2='" & Forms!formname!controlname2 & "';", dbOpenSnapshot)

    Assumes both criteria are text type. Not showing example with JOIN clause. Copy/paste the sql statement from the Access query and modify as needed for VBA.
    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.

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

    I'm not too good with SQL yet, but I basically just copied and pasted the SQL code from the SQL View in the query.

    Here is my code now:

    Code:
    Function Transposer(strSource As String, 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 tblSampleData.SequenceNo, tblSampleData.SamplePrefix, tblSampleData.SampleID, tblSampleData.SampleSuffix, tblResultsData.ResultDate, tblResultsData.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 3078
             MsgBox "The table " & strSource & " doesn't exist."
          Case Else
             MsgBox CStr(Err) & " " & Err.Description
       End Select
    
       Exit Function
    
    End Function
    The same error "3061 Too few parameters. Expected 2" came up. I tested the parameter fields in the immediate window, and they are both returning the correct values. Does the SQL need to be edited to work in VBA?

    Also, should I really use dbOpenSnapshot? I anticipate on editing/updating these records once they are transposed. In that case, wouldn't I use dbOpenDynaset instead?

Page 1 of 2 12 LastLast
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