设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4584|回复: 10
打印 上一主题 下一主题

[ADO/DAO] 判断重复值

[复制链接]
1#
发表于 2013-11-27 13:29:23 | 显示全部楼层
dim str as string
str="DyelotDate=#" & Me.DyelotDate.Value & "#"
str=str & " and MachineName='" & Me.MachineName.Value & "'"
str=str & " and PlanBath=" & Me.PlanBath.Value
if Dcount("*","Tbl_DyeHistory",str)>0 then
    MsgBox "系统检测到当天" & Me.MachineName.Value & " 机台的这个" & Me.PlanBath.Value & " 已经存在," & "请核对后重新输入新的PlanBath ", vbInformation, "提示重复"
end if


dim ssql as string
Dim rs As New ADODB.Recordset
ssql="SELECT * FROM Tbl_DyeHistory WHERE "
ssql=ssql & "DyelotDate=#" & Me.DyelotDate.Value & "#"
ssql=ssql & " and MachineName='" & Me.MachineName.Value & "'"
ssql=ssql & " and PlanBath=" & Me.PlanBath.Value
rs.Open ssql,CurrentProject.Connection, adOpenStatic, adLockReadOnly
If rs.RecordCount > 0 Then
    MsgBox "系统检测到当天" & Me.MachineName.Value & " 机台的这个" & Me.PlanBath.Value & " 已经存在," & "请核对后重新输入新的PlanBath ", vbInformation, "提示重复"
end if
rs.close
set rs=nothing
2#
发表于 2013-11-29 11:01:07 | 显示全部楼层
本帖最后由 todaynew 于 2013-11-29 11:13 编辑

1、写一个自定义函数
Function btnEnabled(ByVal DyelotDate_Ctrl As Control, ByVal MachineName_Ctrl As Control, ByVal PlanBath_Ctrl As Control) As Boolean
    Dim ssql As String
    Dim rs As New ADODB.Recordset
    Dim b As Boolean
    b = IsDate(DyelotDate_Ctrl.Value)  '数据为日期类型
    b = b And IsNull(MachineName_Ctrl.Value) = False '数据不为null
    b = b And IsNull(PlanBath_Ctrl.Value) = False  '数据不为null
    If b = True Then
        ssql = "SELECT * FROM Tbl_DyeHistory WHERE "
        ssql = ssql & "DyelotDate=#" & DyelotDate_Ctrl.Value & "#"
        ssql = ssql & " and MachineName='" & MachineName_Ctrl.Value & "'"
        ssql = ssql & " and PlanBath=" & PlanBath_Ctrl.Value
        rs.Open ssql, CurrentProject.Connection, adOpenStatic, adLockReadOnly

        b =b and rs.RecordCount = 0 '不存在记录

        rs.Close
    End If
    Set rs = Nothing

    btnEnabled = b
End Function

2、假设有一个用于新增的按钮名曰:btnInsert,在控件DyelotDate、MachineName和PlanBath的更新后事件中均如下代码:
        '调用自定义函数
        me.btnInsert.Enabled=btnEnabled(me.DyelotDate,me.MachineName,me.PlanBath)

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-15 12:46 , Processed in 0.089080 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表