Results 1 to 7 of 7
  1. #1
    eugzl is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    68

    how find and prevent save duplicate

    Hi All.
    I have form with four level cascade combo boxes. And I would like to check duplicate for entered data based on those four combo boxes. I creates code:
    Code:
                stCriteria = Me.cboFacility.Text & " " & Me.cboBuilding.Text & " " & Me.cboWing.Text &  & "-" & Me.txtRoom.Text
    
    
                If DCount("*", "tblLocations", stCriteria) > 0 Then
                    MsgBox "This location already exist." & _
                    vbCrLf & "Please enter a different brand.", vbOKOnly Or VbMsgBoxStyle.vbExclamation, _
                    "Duplicate Found"
    but when I'm running form, entered data and then click Save button I got error message "Run-time error '2185': "You can't reference a property or method for control unless the control has the focus."


    How to fix the problem?
    Thanks.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Unclear about the dash with txtRoom. Code below assumes the combo boxes and the text box are unbound. If they're bound you'll always get a match.
    Your runtime error is corrected via the red code.

    Code:
    Sub cmdSearch_Click()
        'stCriteria = Me.cboFacility.Text & " " & Me.cboBuilding.Text & " " & Me.cboWing.Text &  & "-" & Me.txtRoom.Text
    
    
        If DCount("*", "tblLocations", "Facility = '" & Me.cboFacility & "' And Building='" & Me.cboBuilding & "' AND Wing='" & Me.cboWing & "' And Room ='-" & Me.txtRoom & "'") > 0 Then
            MsgBox "This location already exist." & _
            vbCrLf & "Please enter a different brand.", vbOKOnly + vbExclamation, _
            "Duplicate Found"
        Else
            MsgBox "Not a duplicate"
        End If
    End Sub

  3. #3
    eugzl is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    68
    Hi davegri. Thanks for reply.
    In the Save button event I create such code
    Code:
                stCriteria = "FacilityID = " & !FacilityID & " AND BuildingID = " & !BuildingID & " And WingID=" & !WingID & " And FloorID=" & !FloorID & " And Room='" & !Room & "'"
                If DCount("*", "tblLocations", stCriteria) > 0 Then
                   MsgBox "This location already exist." & _
                   vbCrLf & "Please enter a different location.", vbOKOnly + vbExclamation, _
                   "Duplicate Found"
                Else
                   MsgBox "Not a duplicate"
                End If
    It give correct message:
    Click image for larger version. 

Name:	DuplMsg.png 
Views:	21 
Size:	6.4 KB 
ID:	46986

    But I would like to show in the message box information about location that are text values selected in the combo boxes. For instance, "The location NYU MB A1-2"
    where NYU - Facility; MB - Building; A - Wing; 1 - Floor; 2 - Room are values selected in the combo boxes.
    How to get such text in the message box?

    When I tried code like this:
    Code:
    stCriteria = Me.cboFacility.Text & " " & Me.cboBuilding.Text & " " & Me.cboWing & Me.cboFloor & "-" & Me.txtRoom.Text
    I got error message "Run-time error '2185': "You can't reference a property or method for control unless the control has the focus."
    How to solve the problem?
    Thanks.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    2185 error caused by the .text - Remove that.

    Code:
    Sub cmdSearch_Click()
        Dim stCriteria As String
        stCriteria = Me.cboFacility & " " & Me.cboBuilding & " " & Me.cboWing & "-" & Me.txtRoom
        If DCount("*", "tblLocations", "Facility = '" & Me.cboFacility & "' And Building='" & Me.cboBuilding & "' AND Wing='" & Me.cboWing & "' And Room ='-" & Me.txtRoom & "'") > 0 Then
            MsgBox "This location already exists" & vbCrLf _
            & stCriteria & vbCrLf & "Please enter a different brand.", vbOKOnly + vbExclamation, _
            "Duplicate Found"
        Else
            MsgBox stCriteria & " Is not a duplicate"
        End If
    End Sub
    Last edited by davegri; 01-06-2022 at 06:52 PM. Reason: added stCriteria to ELSE

  5. #5
    eugzl is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    68
    The tblLocations has Integer data type fields FacilityID, BuildingID, WingID, FloorID which are foreign key of tblFacilities, tblBuildings, tblWings, tblFloors tables. And only Room field Short Text data type in the tblLocations table. That why when I try to use your code the system cannot recognize Facility and others such parameter under DCount function. I substitute those parameters by FacilityID, BuildingID, WingID, FloorID that exists in tblLocations table and debug code. The parameters Me.cboFacility, Me.cboBuilding, Me.cboWing show selected ID values of the combo boxes. In my previous post my goal display selected text value in the message box. That result still I cannot get. Will appreciate if you get decision for the problem.
    Thanks

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Lookups-eugzl-davegri-v01.zip

    See if this will do it.

    Click image for larger version. 

Name:	Location.png 
Views:	20 
Size:	25.0 KB 
ID:	46988

  7. #7
    eugzl is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Nov 2021
    Posts
    68
    Thanks a lot is very helpfull. I just modified in DCount function code by added quotes required for string value. I very appreciate for help.

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

Similar Threads

  1. Prevent duplicate value
    By IvanGAa in forum Forms
    Replies: 1
    Last Post: 03-11-2020, 01:18 AM
  2. Replies: 8
    Last Post: 12-16-2018, 06:45 AM
  3. How do you prevent duplicate records?
    By cwjakesteel in forum Database Design
    Replies: 28
    Last Post: 11-28-2016, 09:05 AM
  4. How to prevent duplicate records
    By talktime in forum Programming
    Replies: 7
    Last Post: 05-15-2013, 11:02 PM
  5. Dcount prevent duplicate
    By ayamali in forum Programming
    Replies: 20
    Last Post: 04-16-2013, 09:31 PM

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