我正在尝试使用Form创建一个唯一数字表,该Form有一个文本框,用于输入要生成的请求的唯一数字量,以及一个用于运行程序的按钮。到目前为止我使用的是VBA和SQL的组合(实质上,VBA代码在while循环中运行SQL查询)。 VBA代码如下:
Option Compare Database
Private Sub Command10_Click()
'Get number of iterations
Dim i As Integer
'Ensure that Form is open
DoCmd.OpenForm "Interface"
'Set i as value inputed into the Form
i = Forms![Interface]![Text6]
'Clear t3
DoCmd.RunSQL "DELETE * FROM t3"
'Generate as many control numbers as wanted
Do While i > 0
'Generate Control number
DoCmd.OpenQuery "Control", acViewNormal, acEdit
'Clean up duplicates
DoCmd.OpenQuery "Dup_Cont", acViewNormal, acEdit
'Populate t3 with only new control numbers
DoCmd.OpenQuery "New Numbers", acViewNormal, acEdit
'reduce i by 1
i = i - 1
Loop
'Open t3 as a ReadOnly table
DoCmd.OpenTable "t3", acViewNormal, acReadOnly
End Sub
使用的SQL查询如下:
“控制查询”
INSERT INTO t1 ( DATE, DATE_NUM, CONTROL_NUMBER, UNIQUE_NUM )
SELECT DISTINCT
'C77A' & Year(Date()) & Format(Month(Date()),'00') & Format(Day(Date()),'00') AS DATE_A,
IIf([DATE] Like [DATE_A],Count(*)+1,1) AS DATE_NUM,
[DATE_A] & Format([DATE_NUM],'000') AS CONTROL_NUM,
Year(Date()) & Format(Month(Date()),'00') & Format(Day(Date()),'00') & Format(IIf([DATE] Like [DATE_A],Count(*)+1,1),'000') AS UNIQUE_NUM
FROM t2
GROUP BY t2.DATE;
“Dup_Cont查询”
SELECT DISTINCT
t1.DATE,
t1.DATE_NUM,
t1.CONTROL_NUMBER,
t1.UNIQUE_NUM INTO t2
FROM t1;
“新号码查询”
INSERT INTO t3 ( CONTROL_NUMBER )
SELECT Max(t2.CONTROL_NUMBER)
FROM t2;
此代码目前的作用是根据使用[C77A] [日期] [顺序号]格式运行查询的日期创建一个唯一的控制号,并打开一个最新控制号的表,但是因为某些运行控制查询的原因会创建数字的重复项我添加了Dup_Control查询,只提取没有重复项的表的唯一数字。
我希望有人能告诉我如何压缩这个或者至少知道为什么我会在“控制查询”中获得重复。
样本t1
DATE | DATE_NUM| CONTROL_NUM | UNIQUE_NUM
-------------------------------------------------------
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 2 |C77A20140829002|20140829002
C77A20140829 | 3 |C77A20140829003|20140829003
样本t2
DATE | DATE_NUM| CONTROL_NUM | UNIQUE_NUM
-------------------------------------------------------
C77A20140829 | 1 |C77A20140829001|20140829001
C77A20140829 | 2 |C77A20140829002|20140829002
C77A20140829 | 3 |C77A20140829003|20140829003
答案 0 :(得分:0)
除非我弄错了,[DATE]永远不会等于[DATE_A]。您对运算符“LIKE”的使用没有任何通配符(*)意味着两个必须相等才能进行增量。因此,您的唯一编号字段将始终以“001”结尾。
首先需要通过在公式中添加通配符来更改它。如果这不起作用,那么尝试使用此方法递增字段: