设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1519|回复: 5
打印 上一主题 下一主题

[Access本身] 循环备份碰到问题求教代码修改

[复制链接]
跳转到指定楼层
1#
发表于 2008-1-28 12:13:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
从数据库“备份”表里读出源文件与目标文件夹是,然后在目标文件夹里存20个备份文件,循环备份。请高手指教下面的代码错误,如何修改。

Dim conn As ADODB.Connection
        Set conn = CurrentProject.Connection
        Dim rst As New ADODB.Recordset
        Dim strsql As String
      Dim TemPath As String
   Dim StrFileName As String
  Dim strDBFileName     As String
        strsql = "SELECT  top 1 源文件, 备份文件 FROM 备份"
        rst.Open strsql, conn, 1, 1
If Not IsNull(rst("源文件")) And Not IsNull(rst("备份文件")) Then

strDBFileName = Format(Year(Now), yyyy) & Format(Month(Now), mm) & Format(Day(Now), dd) & Format(Hour(Now), hh) & Format(Minute(Now), mm) & ".mdb"
Dim fs
   
   Set fs = Application.FileSearch
   TemPath = rst("备份文件") & "\"

       fs.LookIn = rst("备份文件")
       fs.Filename = "*.mdb"
                     
        If fs.foundfiles.Count > 20 Then
        
        StrFileName = DMin(Left(fs.Filename, 12))
        
          Kill TemPath & StrFileName & ".mdb"
        End If
         
        FileCopy rst("源文件"), TemPath & strDBFileName

      
      End If
      
  rst.Close
        Set rst = Nothing
       Set conn = Nothing
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2008-1-28 15:34:41 | 只看该作者
要写出什么错误?能更有效地解决问题
3#
 楼主| 发表于 2008-1-28 15:45:18 | 只看该作者
找出最先的文件名是否可以用Dmin函数
StrFileName = DMin(Left(fs.Filename, 12))
还有这些代码调试中提示好几处错误
4#
发表于 2008-1-28 16:14:49 | 只看该作者
Sub BackUp()

    Dim conn As ADODB.Connection
    Set conn = CurrentProject.Connection
    Dim rst As New ADODB.Recordset
    Dim strsql As String
    Dim TemPath As String
    Dim StrFileName As String
    Dim strDBFileName As String
    strsql = "SELECT  top 1 源文件, 备份文件 FROM 备份"
    rst.Open strsql, conn, 1, 1
    If Not IsNull(rst("源文件")) And Not IsNull(rst("备份文件")) Then

        strDBFileName = Format(Year(Now), yyyy) & Format(Month(Now), mm) & Format(Day(Now), dd) & Format(Hour(Now), hh) & Format(Minute(Now), mm) & ".mdb"
        Dim fs

        Set fs = Application.FileSearch
        TemPath = rst("备份文件") & "\"

        fs.LookIn = rst("备份文件")
        fs.FileName = "*.mdb"
        Dim I As Integer
        I = fs.Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) - 20
        While fs.FoundFiles.Count > 0
            Kill TemPath & fs.FoundFiles(I)
            I = I - 1
        Wend
        FileCopy rst("源文件"), TemPath & strDBFileName
    End If

    rst.Close
    Set rst = Nothing
    Set conn = Nothing
End Sub
5#
发表于 2008-1-28 16:17:10 | 只看该作者
While I> 0
6#
 楼主| 发表于 2008-1-28 18:16:22 | 只看该作者
谢谢楼上指点,出现运行错误“9”,下标越界,
Set fs = Application.FileSearch
        TemPath = rst("备份文件") & "\"
        fs.LookIn = rst("备份文件")
        fs.FileName = "*.mdb"
        Dim I As Integer
        I = fs.Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) - 20
        While fs.FoundFiles.Count > 0
            Kill TemPath & fs.FoundFiles(I)
            I = I - 1
        Wend
        FileCopy rst("源文件"), TemPath & strDBFileName

[ 本帖最后由 小铁匠 于 2008-1-29 12:04 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-13 16:13 , Processed in 0.089730 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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