我在VB中用SQL语句删除access表记录,可记录看不了文件大小还是没有改变这何解??

解决方案 »

  1.   

    这个时候需要压缩数据库,以释放这部分空间,以下是偶写的函数:
    首先“工程”->“引用”,选中“microsoft jet and replication objects 2.* library”'压缩数据库
    Public Function CompDatabase() As Boolean
        On Error GoTo ErrMsg
        Dim JRO As New JRO.JetEngine
        Dim tempDBPath As String
        Dim conStr1 As String, conStr2 As String
        Dim i As Integer
        
        If MsgBox("你确定要压缩当前数据库吗?", vbQuestion + vbOKCancel + vbDefaultButton2, "小心!") = vbCancel Then
           Exit Function
        End If
        
        Screen.MousePointer = 11
        
        For i = Len(MdbSourcePath) To 1 Step -1
            If Mid(MdbSourcePath, i, 1) = "\" Then
                tempDBPath = Left(MdbSourcePath, i) & "tempDocument.mdb"
                Exit For
            End If
        Next i
        
        If Dir(tempDBPath) <> "" Then
            Kill tempDBPath
        End If
        
        conStr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MdbSourcePath & ";jet oledb:database password=790319"
        conStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tempDBPath & ";jet oledb:database password=790319"
        
        con.Close '先关闭全局连接
        JRO.CompactDatabase conStr1, conStr2
        
        Kill MdbSourcePath
        Name tempDBPath As MdbSourcePath
        
        ConnectDatabase con '再开启全局连接
        
        Screen.MousePointer = 0
        
        MsgBox "数据库压缩成功", vbInformation + vbOKOnly, "祝贺"
        CompDatabase = True
        Exit Function
    ErrMsg:
        Screen.MousePointer = 0
        CompDatabase = False
        MsgBox "请确保其它应用程序没有使用当前数据库!" & vbCrLf & Err.Description & "然后关闭其它所有子窗体后再恢复!", vbInformation + vbOKOnly, "提示"
        CheckConnection con
    End Function
      

  2.   

    应楼主的短信要求,补充如下:
    “microsoft jet and replication objects 2.* library”这个库具有压缩数据库的功能。
    JRO.CompactDatabase conStr1, conStr2的作用是把数据库压缩到一个临时的数据库中。
    Kill MdbSourcePath 和 Name tempDBPath As MdbSourcePath:把原来的数据库删掉,再重命名临时数据库为原来的数据库。
    关于数据库的恢复,我在另外一个帖子里贴了代码:http://expert.csdn.net/Expert/topic/2203/2203194.xml?temp=.4708979如果你的access数据库没有密码,请把连接字符串改为:
    conStr1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MdbSourcePath
    conStr2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tempDBPath