Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

MS Access: dcount duplicate rows?

hildaliu  发表于:2015-03-02 11:19:47  
复制

我的代码是可以运作,但是有一些问题,当我创建一个新的纪录,这无法创建记录到 OrderID2,由于订单Order1已经存在的记录。
我想创建一个基于订单ID记录,但产品ID不能重复
该表将是:
Order ID      Product ID
1                  2
1                  3
1                  4
2                  2
2                  3

2                  4


Private Sub AddToOrder_Click()


On Error GoTo Err_AddToOrder_Click


 
If DCount("[OrderID]", "T_Order_line", "ProductID=" & Me.ProductID) Then
        'Counts and compares the records in "T_Order_line" and ensures that records are not duplicated.
    Me.ProductID.Undo
        'cancels an entry if a duplicate is found
    MsgBox "Warning! ProductID." _
                 & Me.ProductID & vbCr & "has already been entered into this order " _
                 & vbCr & vbCr & "Please check ordered items", _
                 vbInformation, "Duplicate Information"
    MsgBox "Please select another product to add to current order", _
               vbInformation, "Duplicate Information"


Else



    Forms![F_Orders]![F_Order_line_subform].SetFocus
    Me.ProductID.SetFocus
        'navigates between the F_Order_line_subform and the F_PC subform
    DoCmd.GoToRecord , , acNewRec
        'adds a new line for each new record entered
    Me.Parent.F_Order_line_subform.Form!ProductID = Me.ProductID
        'Transfers data from one subform to another.
MsgBox "The order of ProductID." _
                & Me.ProductID & vbCr & "has been transfered, Please enter required product quantity to the recent order entry"

End If


Exit_AddToOrder_Click:
    Exit Sub

Err_AddToOrder_Click:
    Resume Exit_AddToOrder_Click


End Sub

 

Top
煮江品茶 发表于:2015-03-02 14:18:55
用dcount判断一下同一订单中是否存在指定的Product ID即可。

hildaliu 发表于:2015-03-02 21:32:48

I know, I have been already created the dcount 

My code:

If dcount( order id, table , product Id , me.product Id)

You can see the above detailed code 

The function is ok, but the problem is when I created a new record,  I can not enter the same product ID into order2



总记录:2篇  页次:1/1 9 1 :