Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2046|回复: 5

[已解决]找到对应的用户删除

[复制链接]
发表于 2013-4-24 09:31 | 显示全部楼层 |阅读模式
如何删除数据库里不存在的记录 删除用户名.zip (89.82 KB, 下载次数: 10)
发表于 2013-4-24 09:58 | 显示全部楼层
能把数据库数据导出到EXCEL里吗,那样插入一列输入函数=VLOOKUP(B9,yftab.xlsx!$C$2:$C$5,1,0),#N/A删除,你要VBA我不会[em04]
回复

使用道具 举报

 楼主| 发表于 2013-4-24 10:02 | 显示全部楼层
整个程序全部用代码编程,所以导出到excel的话,就没意义,因为系统登陆是要有一个登陆窗口的,要根据数据库里的用户名来判断
回复

使用道具 举报

发表于 2013-4-24 20:12 | 显示全部楼层
  1. Option Explicit
  2. Const adUseClient = 3
  3. Const adModeShareDenyWrite = 8
  4. Const adModeReadWrite = 3

  5. Dim StrConn$, strSQL$
  6. Dim AccessFile$, DataSource$
  7. Dim strError$
  8. Dim AdoConn As Object
  9. Dim AdoRst As Object


  10. Function ConnectDatabase() As Boolean
  11.     AccessFile = "mg.mdb"
  12.     DataSource = ThisWorkbook.Path & Application.PathSeparator & AccessFile

  13.     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  14.               "Data Source=" & DataSource & ";"""

  15.     If AdoConn Is Nothing Then
  16.         Set AdoConn = CreateObject("ADODB.Connection")
  17.         On Error GoTo ErrCheck
  18.         With AdoConn
  19.             .CommandTimeout = 5
  20.             .ConnectionTimeout = 5
  21.             .CursorLocation = adUseClient
  22.             .Mode = adModeReadWrite    'adModeShareDenyWrite
  23.             .ConnectionString = StrConn
  24.             .Open
  25.         End With
  26.         ConnectDatabase = True
  27.     Else
  28.         ConnectDatabase = True
  29.     End If
  30.     Exit Function

  31. End1:
  32.     Set AdoConn = Nothing
  33.     Exit Function

  34. ErrCheck:

  35.     strError = Err.Number & vbCrLf & _
  36.                Err.Description
  37.     Resume End1
  38. End Function

  39. Sub 用户更新()

  40.     If Not ConnectDatabase Then
  41.         MsgBox strError, vbCritical
  42.         Exit Sub
  43.     End If

  44.     strSQL = "delete from yftab"
  45.     AdoConn.Execute strSQL

  46.     Set AdoRst = CreateObject("ADODB.Recordset")
  47.     AdoRst.Open "yftab", AdoConn, 2, 3    'adOpenDynamic, adLockOptimistic

  48.     Dim lLastRow&, i&, j&
  49.     Dim arr

  50.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  51.     If lLastRow < 3 Then Exit Sub
  52.     arr = Range("a2:j" & lLastRow)

  53.     arr(1, 1) = "gh"
  54.     arr(1, 2) = "name"
  55.     arr(1, 3) = "password"
  56.     arr(1, 8) = "生产计划"

  57.     Randomize (2013)

  58.     With AdoRst
  59.         For i = LBound(arr) + 1 To UBound(arr)
  60.             .AddNew
  61.             For j = 1 To UBound(arr, 2)
  62.                 .Fields(arr(1, j)).Value = arr(i, j)
  63.             Next
  64.             .Fields("id") = (Rnd(2013) * 65536 + 1) \ 1
  65.             .Update
  66.         Next
  67.     End With
  68.     Set AdoRst = Nothing
  69.     MsgBox "用户更新完成", vbInformation + vbOKOnly

  70. End1:
  71.     Exit Sub

  72. ErrCheck:
  73.     MsgBox Err.Number & vbCrLf & _
  74.            Err.Description, vbCritical
  75.     Resume End1
  76. End Sub

  77. Sub 读取用户()

  78.     If Not ConnectDatabase Then
  79.         MsgBox strError, vbCritical
  80.         Exit Sub
  81.     End If

  82.     On Error GoTo ErrCheck


  83.     Dim lLastRow&
  84.     Dim arr, strFields$

  85.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  86.     If lLastRow < 2 Then Exit Sub

  87.     arr = Range("a2:j" & lLastRow)
  88.     If llatrow > 2 Then Range("a3:j" & lLastRow).Value = ""
  89.     arr(1, 1) = "gh"
  90.     arr(1, 2) = "name"
  91.     arr(1, 3) = "password"
  92.     arr(1, 8) = "生产计划"

  93.     arr = WorksheetFunction.Index(arr, 1, 0)
  94.     strFields = Join(arr, ",")

  95.     strSQL = "select "
  96.     Range("a3").CopyFromRecordset AdoConn.Execute(strSQL & strFields & " from yftab")
  97.     MsgBox "用户导入完成", vbInformation + vbOKOnly

  98. End1:
  99.     Set AdoRst = Nothing
  100.     Exit Sub

  101. ErrCheck:
  102.     MsgBox Err.Number & vbCrLf & _
  103.            Err.Description, vbCritical
  104.     Resume End1

  105. End Sub
复制代码
回复

使用道具 举报

发表于 2013-4-24 20:14 | 显示全部楼层
楼主,提个建议啊。
你的EXCEL表字段名与ACCESS表的设计最好是相同的。
另外在ACCESS的YFTAB中,列标题最好统一,不要又是中文又是英文的。
回复

使用道具 举报

发表于 2013-4-24 20:16 | 显示全部楼层    本楼为最佳答案   
上面有点小错误,变量名有个写错了。
  1. Option Explicit
  2. Const adUseClient = 3
  3. Const adModeShareDenyWrite = 8
  4. Const adModeReadWrite = 3

  5. Dim StrConn$, strSQL$
  6. Dim AccessFile$, DataSource$
  7. Dim strError$
  8. Dim AdoConn As Object
  9. Dim AdoRst As Object


  10. Function ConnectDatabase() As Boolean
  11.     AccessFile = "mg.mdb"
  12.     DataSource = ThisWorkbook.Path & Application.PathSeparator & AccessFile

  13.     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  14.               "Data Source=" & DataSource & ";"""

  15.     If AdoConn Is Nothing Then
  16.         Set AdoConn = CreateObject("ADODB.Connection")
  17.         On Error GoTo ErrCheck
  18.         With AdoConn
  19.             .CommandTimeout = 5
  20.             .ConnectionTimeout = 5
  21.             .CursorLocation = adUseClient
  22.             .Mode = adModeReadWrite    'adModeShareDenyWrite
  23.             .ConnectionString = StrConn
  24.             .Open
  25.         End With
  26.         ConnectDatabase = True
  27.     Else
  28.         ConnectDatabase = True
  29.     End If
  30.     Exit Function

  31. End1:
  32.     Set AdoConn = Nothing
  33.     Exit Function

  34. ErrCheck:

  35.     strError = Err.Number & vbCrLf & _
  36.                Err.Description
  37.     Resume End1
  38. End Function

  39. Sub 用户更新()

  40.     If Not ConnectDatabase Then
  41.         MsgBox strError, vbCritical
  42.         Exit Sub
  43.     End If

  44.     strSQL = "delete from yftab"
  45.     AdoConn.Execute strSQL

  46.     Set AdoRst = CreateObject("ADODB.Recordset")
  47.     AdoRst.Open "yftab", AdoConn, 2, 3    'adOpenDynamic, adLockOptimistic

  48.     Dim lLastRow&, i&, j&
  49.     Dim arr

  50.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  51.     If lLastRow < 3 Then Exit Sub
  52.     arr = Range("a2:j" & lLastRow)

  53.     arr(1, 1) = "gh"
  54.     arr(1, 2) = "name"
  55.     arr(1, 3) = "password"
  56.     arr(1, 8) = "生产计划"

  57.     Randomize (2013)

  58.     With AdoRst
  59.         For i = LBound(arr) + 1 To UBound(arr)
  60.             .AddNew
  61.             For j = 1 To UBound(arr, 2)
  62.                 .Fields(arr(1, j)).Value = arr(i, j)
  63.             Next
  64.             .Fields("id") = (Rnd(2013) * 65536 + 1) \ 1
  65.             .Update
  66.         Next
  67.     End With
  68.     Set AdoRst = Nothing
  69.     MsgBox "用户更新完成", vbInformation + vbOKOnly

  70. End1:
  71.     Exit Sub

  72. ErrCheck:
  73.     MsgBox Err.Number & vbCrLf & _
  74.            Err.Description, vbCritical
  75.     Resume End1
  76. End Sub

  77. Sub 读取用户()

  78.     If Not ConnectDatabase Then
  79.         MsgBox strError, vbCritical
  80.         Exit Sub
  81.     End If

  82.     On Error GoTo ErrCheck


  83.     Dim lLastRow&
  84.     Dim arr, strFields$

  85.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  86.     If lLastRow < 2 Then Exit Sub

  87.     arr = Range("a2:j" & lLastRow)
  88.     If lLastRow > 2 Then Range("a3:j" & lLastRow).Value = ""
  89.     arr(1, 1) = "gh"
  90.     arr(1, 2) = "name"
  91.     arr(1, 3) = "password"
  92.     arr(1, 8) = "生产计划"

  93.     arr = WorksheetFunction.Index(arr, 1, 0)
  94.     strFields = Join(arr, ",")

  95.     strSQL = "select "
  96.     Range("a3").CopyFromRecordset AdoConn.Execute(strSQL & strFields & " from yftab")
  97.     MsgBox "用户导入完成", vbInformation + vbOKOnly

  98. End1:
  99.     Set AdoRst = Nothing
  100.     Exit Sub

  101. ErrCheck:
  102.     MsgBox Err.Number & vbCrLf & _
  103.            Err.Description, vbCritical
  104.     Resume End1

  105. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-11 11:16 , Processed in 0.243239 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表