Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Posts
    7

    Post Unanswered: Link to subform with cmd button

    Heyo!

    I have a form that contains a continuous subforms (I've attached a picture). The main form contains general details about the project and the subforms will hold data brief details relating to the individual assignments that are part of the project.

    I'd like the "Dtls" button to link to *another* subform. This one would be a subform of the subform and have details about the individual assignment.

    My problem is that I haven't been able to figure out how to use the cmd button to properly link two forms. So far I've been able to get it to either open a specific record (which doesn't work if no record currently exists) OR create a new record. What it *should* do is open the corresponding record for that assignment (based on an AssignmentID key) if it exists, or, if no record exists, create one.

    Thanks in advance!
    Attached Thumbnails Attached Thumbnails formpic.JPG  

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    What do you mean by linking a button to a subform?

    Have a nice day!

  3. #3
    Join Date
    Jul 2009
    Posts
    7
    Well, maybe sub-form isn't the right word. The button will link to a form that has a one to one relationship with the form on which the button is located.

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    I think he means "open" rather than link to?

    Hard so say... since it's impossible to link a command button to a form or to have a form with any kind of relationship.

    Larry, you said you could get the button to either create a new record or go to a new one. If the problem is that you need to do either one or the other depending on a condition, then you would need to use code like:

    Code:
    If YourConditionIsMet Then
       Navigate to the new record
    Else
       Create a new record and then go to that one.
    EndIf
    What code do you have?
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Jul 2009
    Posts
    7
    Here's the code, although as I said it doesn't really...work. But I'll post it anyway so we have something to work from :-) "AssignmentDetailsPresID" is the key that links the two tables.

    Code:
    Private Sub cmdAssignPresDetails_Click()
    On Error GoTo Err_cmdAssignPresDetails_Click
    
        Dim rs As DAO.Recordset
        Dim db As DAO.Database
        Dim stDocName As String
        Dim stLinkCriteria As String
        
        stDocName = "frmESPAssignPresDetails"
        stLinkCriteria = "[AssignmentDetailsPresID]=" & Me![AssignmentDetailsPresID]
        
    '   Start my crappy code to check if the assignment already exists.  
    '   If it does then we're going to go to that record, 
    '   if not then we're going to create a new record.  EXCITING!
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("tblESPAssignPresDetails", dbOpenDynaset)
        
       rs.FindFirst "AssignmentDetailsPresID = " & Me!AssignmentDetailsPresID & ""
       If Me!AssignmentDetailsPresID = rs!AssignmentDetailsPresID Then
         DoCmd.OpenForm stDocName, , , stLinkCriteria
       Else
         DoCmd.OpenForm stDocName, , , , acFormAdd
       End If
    
    Exit_cmdAssignPresDetails_Click:
        Exit Sub
    
    Err_cmdAssignPresDetails_Click:
        MsgBox Err.Description
        Resume Exit_cmdAssignPresDetails_Click
        
    End Sub

  6. #6
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Ok so what part of that doesn't do what you want?

    Modified code (I can't stand seeing Wizard remnants in code):

    Code:
    Private Sub cmdAssignPresDetails_Click()
       On Error GoTo Err_cmdAssignPresDetails_Click
    
       Dim rs As DAO.Recordset
       Dim stDocName As String
       Dim stLinkCriteria As String
        
    '  Start my crappy code to check if the assignment already exists.  
    '  If it does then we're going to go to that record, 
    '  if not then we're going to create a new record.  EXCITING!
    
       Set rs = CurrentDB.OpenRecordset("SELECT AssignmentDetailsPresID FROM tblESPAssignPresDetails WHERE AssignmentDetailsPresID = " & me.AssignmentDetailsPresID, dbOpenDynaset)
       
       If rs.RecordCount > 0 Then
         DoCmd.OpenForm "frmESPAssignPresDetails", , , "[AssignmentDetailsPresID] = " & Me.[AssignmentDetailsPresID]
       Else
         DoCmd.OpenForm "frmESPAssignPresDetails", , , , acFormAdd
       End If
    
    Exit_cmdAssignPresDetails_Click:
       Set rs = Nothing
       Exit Sub
    
    Err_cmdAssignPresDetails_Click:
       MsgBox Err.Description
       Resume Exit_cmdAssignPresDetails_Click
        
    End Sub
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •