Results 1 to 13 of 13
  1. #1
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28

    Question Form opened with acFormAdd change to edit if productID entered already exists


    Here's my current dilemma:

    I have a button on a form that when clicked opens another form in data entry mode via DoCmd.OpenForm and the acFormAdd mode. However what I would like to happen after update of the productID field of the newly opened data entry form is that if a productID entered already exists I would like that same form to change data entry mode to false and display the record of the existing productID and allow it to be edited or saved.

    Currently when try to simply set the data entry mode to false and use OpenRecordset to display the record associated with the productID entered I get 3022 duplicate entry errors.

    Here is the AfterUpdate code for the productID field, its not finished so I'm sure there are probably syntax issues etc.:

    Code:
    Dim product As StringDim crit As String
    Dim msg As String
    Dim db As Database
    Dim rst As Recordset
    Dim SQLstr As String
    
    
    Set db = CurrentDb
    
    
    
    
    product = Me.prodID.Value
    crit = "[prodID] = '" & product & "'"
    
    
    If Me.prodID = DLookup("[prodID]", "tblProducts", crit) Then
        msg = MsgBox("This record already exists, would you like to view it?", vbYesNo)
        
        If msg = vbYes Then
    
    
        SQLstr = "SELECT tblProducts.prodID, tblProducts.prodTypeID, tblProducts.prodDescription FROM tblProducts " & _
                 "WHERE (((tblProducts.prodID)= '" & product & "'));"
                 
        Set rst = db.OpenRecordset(SQLstr, dbOpenDynaset)
        rst.MoveFirst
        
        Me.prodID = rst.Fields("prodID").Value
        Me.prodDescription = rst.Fields("prodDescription").Value
        Me.prodType = rst.Fields("prodTypeID").Value
        Me.prodID.SetFocus
        
        Else
        
        Resume Next
        
        End If
        
    End If
    
    
    Set rst = Nothing
    Set db = Nothing
    
    
    End Sub
    Last edited by illicited; 08-20-2014 at 02:57 PM. Reason: solved

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Determine if the productID already exists before opening the form, then open the form in appropriate mode.
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    My thoughts........

    Dim product As String
    product = Me.prodID.Value
    Is "prodID" as String or a Long?

    I don't think your code would even execute if the prodID exists because the 3022 error would occur first.

    The AfterUpdate event is the wrong event to use (IMO). You should be using the BeforeUpdate event to check if the prodID exists. If the prodID exists, you can cancel the update - the BeforeUpdate event can be canceled, the AfterUpdate event cannot be canceled.


    opens another form in dataentry mode via DoCmd.OpenForm
    It looks like the form unbound?

  4. #4
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28
    prodID is a string

    I should give more context of what I have going on. I have a form displaying a list of all the products and a button for adding a new one. The button being clicked opens a blank form for the product specifics, but in effort to make things simple for the user if the user inputs a product ID that already exists in the blank form (product ID's are alphanumeric with several different schemes involved in their choosing) then I would like the form to load that record. There may be a better way of doing this that I haven't considered.

  5. #5
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28
    Steve,

    Thank you, you got me on the right track. My data entry form wasn't unbound which was what was causing duplication issue. I removed all bound references and its working as intended now. Thanks again.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    So if user clicks the 'New Product' button, form opens to new record and user enters productID. But if they goof by entering an existing productID, you want to retrieve that record instead of just telling them to try a different input?
    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.

  7. #7
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28
    Yes exactly

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I guess you have satisfactory solution. I've never use UNBOUND forms for data entry/edit.
    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
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28
    What would you suggest in a situation like this? It's opening the record now, but doesn't appear to be saving any changes.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Would need code to write data back to the record. That's why I don't use UNBOUND forms.

    Could open bound form, move to New Record row, code in BeforeUpdate event validates user input and if ProdID exists, cancel the entry and move to existing record.
    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
    illicited is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    28
    I will look at doing that instead. I am wary of the unbound because of all the coding. Why not let Access do what it already has built-in procedures for doing.

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    My sentiments.

    For a start, consider code in the New Product button:

    DoCmd.OpenForm "Products"
    DoCmd.GoToRecord acDataForm, "Products", acNewRec
    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
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Then you could use code in the BEFORE UPDATE event to check for duplicate entry.

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

Similar Threads

  1. Replies: 2
    Last Post: 02-24-2014, 09:26 AM
  2. Replies: 3
    Last Post: 10-07-2013, 12:27 PM
  3. Replies: 9
    Last Post: 02-26-2013, 10:48 AM
  4. Change form to edit mode using VBA
    By Richie27 in forum Programming
    Replies: 1
    Last Post: 06-05-2012, 04:39 AM
  5. Trouble with check if Exists before Edit or Add Record
    By mrfixit1170 in forum Programming
    Replies: 4
    Last Post: 02-10-2012, 10:38 AM

Tags for this Thread

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