| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 689 人关注过本帖
标题:excel导入ACCESS时 防止数据重复导入问题
只看楼主 加入收藏
lzxagy
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-8-28
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
excel导入ACCESS时 防止数据重复导入问题
代码是要将EXCEL数据导入到ACCESS中,代码验证成功,但就是将数据重复导入到ACCESS中了。如何防止数据重复导入,应该怎么写代码啊。求高手指点。红色的代码是我写的防止重复导入的,但是不成功。
Private Sub Command3_Click()
Dim excel_app As Object
Dim excel_sheet As Object

If txtExcelFile.Text = "Excel路径" Then
MsgBox "请选择EXCEL表"
  Exit Sub
End If

If txtAccessFile.Text = "Access路径" Then
MsgBox "请选择Access表"
  Exit Sub
End If


Label1.Caption = "正在导入,请稍候..."
 Screen.MousePointer = vbHourglass
 DoEvents

 ' Create the Excel application.
 Set excel_app = CreateObject("Excel.Application")

 ' Uncomment this line to make Excel visible.
 excel_app.Visible = True

 ' Open the Excel spreadsheet.
 excel_app.Workbooks.Open FileName:=txtExcelFile.Text

 ' Check for later versions.
 If Val(excel_app.Application.Version) >= 8 Then
 Set excel_sheet = excel_app.ActiveSheet
 Else
 Set excel_sheet = excel_app
  
 End If
  
Dim u '求EXCEL表中记录的条数,以便控制进度条
u = 1
Do
 If Trim$(excel_sheet.Cells(u, 1)) = "" Then Exit Do
 u = u + 1
 Loop
 bar.Max = u - 1
     
 Dim sql As String
   
sql = "select * from dgjj"
   rs.Open sql, conn.ConnectionString, adOpenStatic, adLockOptimistic '打开记录集


 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 Dim v '导入记录,用了两层循环
 v = 1
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 Do
 If Trim$(excel_sheet.Cells(v, 1)) = "" Then Exit Do '外层,如果EXCEL表中读取到空行,结束

If rs.EOF And rs("gsmc") = Trim$(excel_sheet.Cells(v, 1)) And rs("jyzmc") = Trim$(excel_sheet.Cells(v, 2)) And rs("gh") = Trim$(excel_sheet.Cells(v, 3)) And rs("rq") = Trim$(excel_sheet.Cells(v, 4)) And rs("ch") = Trim$(excel_sheet.Cells(v, 5)) And rs("ckv20") = Trim$(excel_sheet.Cells(v, 7)) And rs("syl") = Trim$(excel_sheet.Cells(v, 21)) Then
 MsgBox "已经包含该条数据!"
 Else

 
 

rs.AddNew

rs("gsmc") = Trim$(excel_sheet.Cells(v, 1))
rs("jyzmc") = Trim$(excel_sheet.Cells(v, 2))
rs("gh") = Trim$(excel_sheet.Cells(v, 3))
rs("rq") = Trim$(excel_sheet.Cells(v, 4))
rs("ch") = Trim$(excel_sheet.Cells(v, 5))
rs("ph") = Trim$(excel_sheet.Cells(v, 6))
rs("ckv20") = Trim$(excel_sheet.Cells(v, 7))
rs("psdv20") = Trim$(excel_sheet.Cells(v, 8))
rs("dzv20") = Trim$(excel_sheet.Cells(v, 9))


rs("xqyg") = Trim$(excel_sheet.Cells(v, 10))
rs("xqv20") = Trim$(excel_sheet.Cells(v, 11))
rs("xqvt") = Trim$(excel_sheet.Cells(v, 12))

rs("xhyg") = Trim$(excel_sheet.Cells(v, 13))
rs("xhv20") = Trim$(excel_sheet.Cells(v, 14))
rs("xhvt") = Trim$(excel_sheet.Cells(v, 15))

rs("xrv20") = Trim$(excel_sheet.Cells(v, 16))
rs("lgv20") = Trim$(excel_sheet.Cells(v, 17))
rs("sy") = Trim$(excel_sheet.Cells(v, 18))
Dim a As Double
a = Trim$(excel_sheet.Cells(v, 19))
a = Round(a, 2)
rs("syl") = a
   
   
  

 bar.Value = v
 v = v + 1
rs.MoveNext

 Loop
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''


 ' Comment the rest of the lines to keep
 ' Excel running so you can see it.

 ' Close the workbook without saving.
 excel_app.ActiveWorkbook.Close False
 ' Close Excel.
 excel_app.Quit
 Set excel_sheet = Nothing
 Set excel_app = Nothing
 Refresh

 rs.Close
Set rs = Nothing


Label1.Caption = "导入完毕"
 Screen.MousePointer = vbDefault
 MsgBox "共导入" & Format$(v - 1) & "条记录"

End Sub

[ 本帖最后由 lzxagy 于 2012-10-18 17:18 编辑 ]
搜索更多相关主题的帖子: 如何 Excel EXCEL 成功 
2012-10-18 17:15
lzxagy
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-8-28
收藏
得分:0 
没有人回复啊   
2012-10-19 15:00
快速回复:excel导入ACCESS时 防止数据重复导入问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014028 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved