我有一个通过VBA / Excel填充的数据库。
我的主要字段是TRADE_ID
,设置为无重复项。
我有一些交易每个月重置一次......这个例子是3个月
所以我的字段TRADE_ID看起来像
ABCDEFGH-1 'for 1st month
ABCDEFGH-2 'for 2nd month
ABCDEFGH-3 'for 3rd month
当我的交易被关闭时...我想禁止ABCDEFGH
作为TRADE_ID
我不知道怎么可能
以下是我用来生成ID的代码
For Each cl In Range("C2", Range("C2").End(xlDown))
If cl.Offset(0, -2).Value = "NEW" or cl.Offset(0, -2).Value = "RESET"Then
If cl.Value <> currentvalue Then
currentinteger = 1
currentvalue = cl.Value
Else
currentinteger = currentinteger + 1
End If
cl.Offset(0, -1).Value = "OPT-" & Year(Date) & Format(Month(Date), "00") & Format(Day(Date), "00") & "-" & Format(cl.Value, "0000") & "-" & Chr(64 + currentinteger) & cl.Offset(0, 1).Value
End If
Next cl
基本上如果If cl.Offset(0, -2).Value = "CLOSE"
我想禁止"OPT-" & Year(Date) & Format(Month(Date), "00") & Format(Day(Date), "00") & "-" & Format(cl.Value, "0000") & "-" & Chr(64 + currentinteger
作为数据库中ID的开头
答案 0 :(得分:0)
将密钥拆分为2个字段 - “ABCDEFGH”作为一个表中的主键,第二个事务表具有2个关键字段,一个为“ABCDEFGH”,第二个字段为“1”,“2” ,“3”等。第一个表将保存ABCDEFGH的所有事务的共同数据,第二个表将具有每个事务的唯一数据。
当您尝试添加下一笔交易时,第一个表中的唯一键会阻止重复使用ABCDEFGH作为关键字。例如,在第二个表格中使用两个键可以防止重复使用ABCDEFGH-2。
我通常不会建立关系并强制执行引用完整性,但根据您的需要,您可以查看这两个主题以强制实现两个表之间的一致性。