导入前检查重复项

时间:2019-07-19 18:02:34

标签: vba ms-access

我正在将数据电子邮件从Outlook导入数据库。我想在导入之前检查条件,但不确定如何处理。这是我要导入的文本的示例:


Transmit:  Date: 

*** R O U T I N E         *** Request No.: 123456789

Operators Notified: 


Start Date/Time:    01/01/18   At 00:15  Expiration Date: 01/01/18 

Location Information: 
County:     Municipality: 
Subdivision/Community:  
Street:               0 FAKE ST
Nearest Intersection: FAKE ST
Other Intersection:    
Lat/Lon: 
Type of Work: REPAIR  
Block:                Lot:                Depth: 2FT 

Extent of Work:  BEGINS 53FT W OF C/L OF INTERSECTION AND EXTENDS 785FT
 W.  MARK A 3FT RADIUS OF POLE NUMBERS 000/000, 000/000

Remarks:  
 Working For Contact:  NO ONE

Working For: NO ONE
Address:     123 FAKE ST
City:        SPRINGFIELD
Phone:       555-555-5555   Ext:  

Excavator Information: 
Caller:      NO ONE
Phone:       555-555-5555   Ext:  

Excavator:   NO ONE

Address:     123 FAKE ST
City:        SPRINGFIELD
Phone:       555-555-5555   Ext:          Fax:  
Cellular:     
Email:       EMAIL@EMAIL.COM

End Request

我想在导入之前检查序列号字段,并且仅在该编号不存在的情况下才导入。

下面是我用于导入的代码。任何帮助是极大的赞赏。谢谢!

If InStr(1, abody(j), "Request No.:", 1) Then
   reQuest = Mid(abody(j), InStr(abody(j), "Request No.:") + 13)
   Me.Requestnumber = reQuest```

I have tried the following code:

如果DCount(“ *”,“导入表”,“ [requestnumber] =”&Me![Requestnumber]&“”)> 0然后       MsgBox“请求号已经存在。”

but i receive an error "run-time error '3075'
syntax error (missing operator) in query expression '[requestnumber]='.

any ideas?

1 个答案:

答案 0 :(得分:0)

您可以选择“不同的记录”,诸如此类。

Dim rst As DAO.Recordset
Set rst = Currentdb.OpenRecordset("SELECT DISTINCT DoID, LtID FROM YourStagingTable")
Do Until rst.EOF
  Currentdb.Execute "INSERT INTO YourLiveTable(Col1, Col2, etc) VALUES(" & rst("Col1") & "," & rst("Col2") & ", etc)
  rst.MoveNext
Loop

或者,如果您的表中已经有重复对象,请检查重复对象并删除它们(根据您指定的任何条件)。

DELETE ID
FROM Table1
WHERE Items In 
    (SELECT Items 
     FROM Table1 
     GROUP BY Items 
     HAVING Count(*)>1) 
And ID >
    (SELECT Min(ID) 
     FROM Table1 AS T 
     WHERE T.Items=Table1.Items);