Results 1 to 7 of 7
  1. #1
    jfn15 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    26

    Code not working!!

    I have 2 M:M relationships, as follows:



    tblApplication, - AppID, AppDate etc.
    tblAnimalDetails - AnimalID, Gender etc.
    tblAppSpecies - AppID, AnimalID, SpeciesStatus etc.
    tblContacts - ContactID, etc
    tblContactApp - AppID, ContactID

    I have an application form (frmApplication) that is based on a query (using tblApplication, tblContacts, tblContactApp)
    I have a second form (frmAppSpecies), based on a query using tblAppSpecies & tblAnimalDetails. This is a form with a subform.
    I've placed a command button on frmApplication, to open frmAppSpecies, with the AppID already added, so that relevant animal information for that particular application can be added.

    The code I've put in the button's click event is as follows:


    DoCmd.OpenForm "frmAppSpecies", acNormal, , "AppID =" & Me.AppID, acFormEdit, acDialog

    I have no coding experience at all, so have cobbled this together from a book, but I'm obviously missing something - can anyone assist?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    It would be better for readers if you could post a jpg of your tables and relationships.
    If you really have M:M relationships, you may find this video tutorial useful

    https://www.youtube.com/watch?v=7XstSSyG8fw

    Here is another tutorial (pdf based) to help with Tables, Normalization and Relationships.
    http://www.rogersaccesslibrary.com/T...lationship.zip

  3. #3
    Dunro is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Feb 2011
    Location
    Toronto, Canada
    Posts
    39
    Quote Originally Posted by jfn15 View Post

    The code I've put in the button's click event is as follows:

    DoCmd.OpenForm "frmAppSpecies", acNormal, , "AppID =" & Me.AppID, acFormEdit, acDialog
    I'm assuming that the underlying query for frmApplication only includes 1 field called AppID? (even though it's used to join both tblApplication and tblContactApp). Also, if you have a control called AppID, is it bound to the correct field?

  4. #4
    JrMontgom is offline Competent Performer
    Windows Vista Access 2010 32bit
    Join Date
    Sep 2012
    Location
    Vero Beach, FL USA
    Posts
    124
    You may need to be specific as to which AppID you mean i.e. precede the AppID field name with the table name for example tbl!.Appid to prevent confusion.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by jfn15 View Post

    ...I've placed a command button on frmApplication, to open frmAppSpecies, with the AppID already added, so that relevant animal information for that particular application can be added...
    Your code assumes that a Record for the given AppID already exists in frmAppSpecies, and that you want to go to that Record when that Form opens; is that the case?

    Or do you need to go to frmAppSpecies and start a New Record with that AppID entered in the appropriate Control?

    To me your post sounds as if it's the latter case, but you can use this code to do either.

    In frmApplication:

    Code:
    Private Sub Go2AppSpeciesForm_Click()
    
    If Not IsNull(Me.AppID) Then
      DoCmd.OpenForm "frmAppSpecies", , , , , , Me.AppID
     Else
      MsgBox "An AppID Must Be Entered First!"
     End If
    
    End Sub


    In frmAppSpecies:

    Code:
    Private Sub Form_Load()
    
    Dim rst As Recordset
    
    If Not IsNull(Me.OpenArgs) Then
     Set rst = Me.RecordsetClone
     
     rst.FindFirst "[AppID] = " & Me.OpenArgs 
     
       If Not rst.NoMatch Then
          Me.Bookmark = rst.Bookmark
       Else
        DoCmd.GoToRecord , , acNewRec
        Me.AppID = Me.OpenArgs
      End If
    
    rst.Close
    Set rst = Nothing
    
    End If
    
    End Sub


    The above code assumes that AppID is Numeric. If it is actually defined Text, replace the line

    rst.FindFirst "[AppID] = " & Me.OpenArgs

    with

    rst.FindFirst "[AppID] = '" & Me.OpenArgs & "'"

    You'll also need to replace Go2AppSpeciesForm with the actual name of your Command Button.

    Linq ;0)>

  6. #6
    jfn15 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    26
    Yes, the ID field is numeric, and your code works perfectly! Thank you!

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>

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

Similar Threads

  1. Code not working
    By cheyanne in forum Forms
    Replies: 9
    Last Post: 06-10-2012, 03:22 AM
  2. Code Not Working
    By Kirsti in forum Programming
    Replies: 3
    Last Post: 03-26-2012, 02:48 PM
  3. VBA Code not working how it Should
    By Juan4412 in forum Programming
    Replies: 7
    Last Post: 12-07-2010, 01:59 PM
  4. VBA Code Not working
    By jo15765 in forum Programming
    Replies: 12
    Last Post: 12-03-2010, 04:01 PM
  5. VBA Code for working with IE
    By smikkelsen in forum Programming
    Replies: 3
    Last Post: 04-15-2010, 01:05 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