Using one-to-one relationship

Anonymous
2011-02-07T01:32:55+00:00

For various reasons, I need to break up a large table into smaller tables.  I've set up the smaller tables with one-to-one relationships.

If I assign a form's record source using just one of the smaller tables (and its fields), the form opens correctly showing the first record.  And, I can move through all the records using the Access record controls.

If I add one or more of the smaller tables (which participate in the one-to-one relationship with the original table), the form opens but no records are available - i.e., it opens as if I asked it to display a new record, and it doesn't recognize that there are other records available.

The one-to-one relationships are correctly set, with each table's primary key being a single key.

I have form controls that need to be bound to several of the one-to-one tables, so several tables need to be the form's record source.

As an example:  Table A has an autonumber primary key field, AID, and another field, Aname.  Table B has a primary key field BID (numeric long integer type) joined to AID in a one-to-one relationship (with referential integrity), and another field, Baddress.

If Table A is a recordsource for a form, the value of field Aname appears on the form and I can move through all Table A records.  As soon as I add Table B (along with Table A) as a recordsource, the form opens with no records available (i.e., a blank form as if I told it to go to a new reocrd).

I realize I can resolve this through a single large table, but that's what I'm trying to avoid. 

Has anyone worked with one-to-one relationships enough to have run into this problem?  And come up with a solution?

Thanks for any help.

Mike

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
ScottGem 68,840 Reputation points Volunteer Moderator
2011-02-10T19:13:03+00:00

Mike,

I'm going to agree with Roger yet again. Helping you has been a pleasure and we will be here to support you as your project continues.

The main difference between bound and unbound forms is that, with a bound form, Access handles all the I/O between the form and the table. With an unbound form, the user has to write code (VBA), to handle the I/O. The complexity of that code can vary widely depending on the usage and purpose of a form. For example, a form solely for entering new records is fairly easy to code. But a form for retrieving data and populating the unbound controls, can be very complex. The coding involved depends on the table structure and form layout. Personally, I'm lazy and tend to use bound forms wherever I can.

There is only ONE circumstance in Access where a child record can be created with an automatically populated FK. And that's when using a subform. When you correctly link a subform to the main (parent) form, then any records created in the subform have the FK automatically populated. This is one of the reasons I said that subforms are a very powerful feature. But this only works with bound forms. So, in every other circumstance, when you add a record to a child table, you have to make sure the FK is populated correctly.


Hope this helps, Scott<> P.S. Please post a response to let us know whether our answer helped or not. Microsoft Access MVP 2010 Blog: http://scottgem.wordpress.com Author: Microsoft Office Access 2007 VBA Technical Editor for: Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports and Queries

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments
Answer accepted by question author
Anonymous
2011-02-10T18:15:24+00:00

Mike,

It's posts like this that makes the whole process of answering questions here worth while.  I think we've seen a lightbulb go off above your head <grin>

As you no doubt realize, there are two distinct knowledge sets necessary for database development: design and implementation.  The Design set is universal and can be applied to any DBMS implementation.  The Implementation set is specific to the DBMS.

For Design knowledge, I recommend Michael Hernandez' "Database Design for Mere Mortals".  I taught 12 years of college courses in db design from that book.  It was a revelation to me.

For Access Implementation knowledge, I recommend "Access Cookbook" by Ken Getz, Paul Litwin, and Andy Baron.  I learn best from examples and this book is a set of common problems and solutions in Access development.  It's not really a book that you read through.  It's best used to find how to solve a specific problem.

I created the samples on my website (www.rogersaccesslibrary.com) with the same idea in mind. 

Good luck!


-- Roger Carlson

MS Access MVP 2006-2010

www.rogersaccesslibrary.com

If you want a detailed answer, ask a detailed question!

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

44 additional answers

Sort by: Most helpful
  1. ScottGem 68,840 Reputation points Volunteer Moderator
    2011-02-15T13:08:32+00:00

    Mike,

    If you are bound on using unbound forms (pun intended) then you need to do a lot more research on how to do them. If you just want to display data, you could use DLookups (as Ken said you can't use a query as the Controlsource of a control). You could use a temp table and other methods. If you want interactivity (to be able to enter and edit data, Unbound forms become even more complex to do. You need to have a fuller understanding of VBA and database strucutre before you do unbound forms.


    Hope this helps, Scott<> P.S. Please post a response to let us know whether our answer helped or not. Microsoft Access MVP 2010 Blog: http://scottgem.wordpress.com Author: Microsoft Office Access 2007 VBA Technical Editor for: Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports and Queries

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-02-15T01:17:47+00:00

    I agree that my query makes little sense.  In my fumbling way I'm looking for a way to pick out a single record.

    I wish I could insert a screen shot in here of the form I need.  It's a tab control.  On the first tab, there are three required dates in different locations, a number of other pieces of necessary information, and a number of "evaluation criteria" (which I wanted to treat in the same way as the milestones).  There are four other tabs with similar interleaving of milestones, criteria and other information.  The form models the chronological business process in that the user won't enter any milestones residing on later tabs (or evaluation criteria) until the earlier tabs are complete.  For the user's benefit, I'd like to preserve the chronological sequence of the process.  And, all this is just for Phase One of the process.  There are six phases total with similar interleaving of milestones/criteria/other info.

    So, yes, I'd like to learn how to reinvent the wheel rather than require the user to only view milestones in one subForm, criteria in another subForm, action items in a third subForm, etc.  Unless they see a milestone in the context of (for instance) a "site investigation tab" (which is a subset of Phase One) along with related site investigation information, it will likely be a confusing subform without extensive descriptions attached to each milestone/evaluation critera/etc.

    To the extent I follow your code, it looks analogous to what you suggest I do for milestones.  But, PM tasks are repetitive and only need, at most, a start/finish date.  Each project I'm dealing with spans a year or more with absolutely no repetition within a single project, has about 70 different dates spread out through different phases, and even within a phase are broken up into subphases.  Some of the dates are inapplicable depending on what evaluation criteria are selected.  There may only be a total of 3-4 projects in a year.  I've been simplifying this all along.  There are six phases in each project.  In addition to about 70 milestone dates, there are about 65 different evaluation criteria, each of which have a unique list to choose only acceptable entries for the criterion (they're all different), and a notes field.  Interspersed with all this are internal personnel choices and assignments, external firms (lawyers, surveyors, environmental assessors, etc.) to be retained and assigned tasks, and many other pieces of information necessary to retain.   It just doesn't make sense to simply present the user with continuous subforms dealing with only milestones, or evaluation criteria, even if they are  divided up into phases.  I suppose I could get elaborate and divide each phase into subphases into subsubphases to the point where only one record is chosen on the subForm (or subphases tied to only the milestones/criteria/etc on a single tab) but that's getting ridiculous and defeats the purpose of being able to easily add a new milestone without extensive modification.

    The project also requires printing numnerous forms/reports in a prescribed format that intersperses milestones/criteria/etc. at different locations in the reports.  I don't see how I can accomplish that if I can't directly access a particular milestone record.

    I guess my frustration is showing.

    Ken - I understand how to do what you've suggested.  I've created a main and continuous subform that does exactly what you prescribe and it works fine.  I just don't think that approach lends itself to a long, linear business process as opposed to a repetitive task.

    Probably at this point y'all are getting weary of my questions and resistance to using continuous subforms.  I am willing to learn, and if given appropriate references, I can figure it out.

    So, why don't we leave it at this?  Could someone provide me with the references I need to learn how to deal with unbound forms so I can connect a single control with a single milestone/criteria?

    I very much appreciate all the information you, Scott and Roger have given me.  It's been very helpful in understanding why I can't do something and/or what constraints exist, but other than using continuous subForms to enter a single category of data, I don't have the positive side of that.  You know the old saying:  it's better to teach a man how to fish (give him a reference source) than to give him a fish (a piece of code).  At this point, I need to learn how to fish, else my only choice is to go back to one large project table, which I can make work as kludgy as it may be.

    Please tell me where to look to understand what I need to do. 

    Thanks

    Mike

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-02-14T23:44:26+00:00

    Your query makes little sense as you are repeating the join criterion in the JOIN and WHERE clause.  More to the point queries are used as the RecordSource of a form not the ControlSource of a Control.

    Just use the built in functionality of a bound subform in the way I described.  No point in trying to reinvent the wheel. 

    To give you an idea of how this sort of thing works here is the code behind a button on a form in a database which I once developed for scheduling preventative maintenance tasks (PMs) on engineering structures.  It inserts rows into a table and then requeries a bound subform to show the new rows.  The details of how the SQL statement is built are not really relevant, but in essence all it does is execute an SQL statement to insert rows into a StructurePMJobs table, which is analogous to your SQL stement to insert rows into the tblProjectMilestones table, and then requery the  sfcStructurePMJobs subform control within the parent form.  It also uses ADO rather than DAO as in your case, but that's also beside then point.  The subform in this case is in continuous forms view and based on the following query:

    SELECT StructurePMJobs.* FROM StructurePMJobs ORDER BY StructurePMJobs.DateDue;

    and is linked to the parent form on two pairs of fields, StructureNumber and PMCode, so it shows the schedule for jobs to be undertaken on the current   structure and PM task in the parent form.  In your case the link would be on ProjectID to show the Phase 1 milestones for the current project.

    Private Sub cmdConfirm_Click()

        On Error GoTo Err_Handler

        Const MESSAGETEXT = _

            "A structure, PM and number of jobs to be added must be selected."

        Dim strCriteria As String

        Dim dtmNewdate As Date

        Dim varLastdate As Variant

        Dim strSQL As String

        Dim n As Integer

        Dim cmd As ADODB.Command

        ' first confirm all necessary controls have values

        If Not IsNull(Me.cboStructure) _

            And Not IsNull(Me.cboPM) _

            And Not IsNull(Me.txtNumberOfJobs) Then

            ' loop number of times entered in the form

            ' and insert a row at each itereation of the loop

            For n = 1 To Me.txtNumberOfJobs

                ' build criteria for finding last due date

                ' for selected structure/PM

                strCriteria = "StructureNumber = """ & _

                    Me.cboStructure & """ And PMCode = """ & _

                    Me.cboPM & """"

                ' get last due date

                varLastdate = DMax("DateDue", "StructurePMJobs", strCriteria)

                ' call NextDueDate function to get next

                ' due date for selected PM

                dtmNewdate = NextDueDate(Me.cboPM, varLastdate)

                Set cmd = New ADODB.Command

                cmd.ActiveConnection = CurrentProject.Connection

                cmd.CommandType = adCmdText

                ' build and execute SQL statement to insert row into table

                strSQL = "INSERT INTO StructurePMJobs" & _

                    "(StructureNumber,PMCode,DateDue) " & _

                    "VALUES(""" & Me.cboStructure & """,""" & _

                    Me.cboPM & """,#" & Format(dtmNewdate, "yyyy-mm-dd") & "#)"

                cmd.CommandText = strSQL

                cmd.Execute

                ' requery subform to show new rows

                Me.sfcStructurePMJobs.Requery

            Next n

        Else

            MsgBox MESSAGETEXT, vbExclamation, "Invalid Operation"

        End If

    Exit_Here:

        Exit Sub

    Err_Handler:

        MsgBox Err.Description

        Resume Exit_Here

    End Sub


    Ken Sheridan, Stafford, England

    Was this answer helpful?

    0 comments No comments