ACCESS的VBA中如何在导入文本数据至数据库时,以流水号和日期剔除重复数据,DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, "" MsgBox "导入完成", , "完成提醒"
时间: 2023-07-06 13:33:34 浏览: 211
在导入文本数据至数据库时,可以使用以下代码以流水号和日期剔除重复数据:
```
DoCmd.RunSQL "DELETE * FROM 接触清单 WHERE 流水号 & 日期 IN (SELECT 流水号 & 日期 FROM [Text;HDR=YES;DATABASE=" & Forms!数据管理窗!导入路径 & "].[接触清单导入规格])"
DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, ""
MsgBox "导入完成", , "完成提醒"
```
这个代码将首先使用DELETE语句从目标表中删除与文本文件中重复的记录。然后,使用DoCmd.TransferText方法导入文本文件中的数据。最后,显示一个消息框,提示导入完成。
相关问题
请优化以下代码,让ACCESS的VBA中在导入文本数据至数据库时,同时判断数据库有相同流水和日期时,则不存入:DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, "" MsgBox "导入完成", , "完成提醒"
以下是对代码的优化:
```
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("接触清单")
DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, ""
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
Do Until rs.EOF
If rs!流水号 = Forms!数据管理窗!接触清单!流水号 And rs!日期 = Forms!数据管理窗!接触清单!日期 Then
MsgBox "已存在相同流水和日期的数据,不进行存入操作!", , "提示"
rs.Close
Set rs = Nothing
Exit Sub
End If
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
MsgBox "导入完成", , "完成提醒"
```
此优化代码的思路如下:
1. 先打开接触清单表,准备进行数据比对;
2. 使用DoCmd.TransferText导入数据;
3. 循环比对接触清单表中的每条记录,判断是否有相同流水号和日期的记录,如果有,则弹出提示框并退出存入操作;
4. 如果没有相同的记录,则关闭接触清单表并继续执行存入操作。
请优化以下代码,让ACCESS的VBA中在导入文本数据至数据库时,同时判断数据库有相同流水和日期时,则不存入,同时提示“已有重复数据,不作导入”:DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, "" MsgBox "导入完成", , "完成提醒"
以下是对代码的优化:
```
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("接触清单")
DoCmd.TransferText acImportDelim, "接触清单导入规格", "接触清单", Forms!数据管理窗!导入路径, True, ""
If Not rs.BOF And Not rs.EOF Then
rs.MoveFirst
Do Until rs.EOF
If rs!流水号 = Forms!数据管理窗!接触清单!流水号 And rs!日期 = Forms!数据管理窗!接触清单!日期 Then
MsgBox "已有重复数据,不作导入", vbCritical, "错误"
rs.Close
Set rs = Nothing
Exit Sub
End If
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
MsgBox "导入完成", vbInformation, "完成提醒"
```
此优化代码的思路如下:
1. 先打开接触清单表,准备进行数据比对;
2. 使用DoCmd.TransferText导入数据;
3. 循环比对接触清单表中的每条记录,判断是否有相同流水号和日期的记录,如果有,则弹出提示框并退出存入操作;
4. 如果没有相同的记录,则关闭接触清单表并继续执行存入操作。
5. 导入完成后,弹出提示框提示导入完成,提醒用户操作成功。
阅读全文
相关推荐
















