-
Xoá các table theo điều kiện trong Access
Noname > 15-08-10, 01:10 PM
Hỏi: Tromg file MDB của em có nhiều table để lưu dữ liệu của các năm khác nhau. EM chỉ muốn lưu 3 năm gần nhất thôi. Ví dụ: năm nay là 2009, em chỉ cần lưu dữ liệu của 2007, 2008, 2009; năm tới 2010: chỉ lưu 2008, 2009, 2010.
Em muốn tự khi lưu dữ liệu ngày đầu tiên của năm mới, thì table từ 3 năm về trước sẽ tự động bị xoá đi. Cụ thể: ngày 01/01/2010, em lưu dữ liệu thì table lưu dữ liệu của 2007 bị xoá đi.
Đồng thời, em muốn xoá table với điều kiện nó tồn tại: Cụ thể: câu lệnh để thực hiện:
Nếu tableA có trong MDB thì xoá tableA
Các bác cho em xin đoạn code nhé
Đáp:
Mình gửi bạn 1 đoạn code xóa table:
Code:
Mã:Sub DelTable(T As String)
DoCmd.DeleteObject acTable, T
End Sub
Bây giờ bạn có thể tạo 1 table với thông tin: tblTableInfo(tableName,TableDate)
Ở đây bạn khai báo table nào thuộc năm nào.
sau đó có thể gọi 1 sub xóa table cũ hơn 3 năm:
Code:
Mã:sub DelTable3yearOld()
Dim Db As Database, Rs As Recordset, sql1 as string
SQL1="select TableName from tblTableInfo where (year(date())-year(TableDate))>=3
Set Db = CurrentDb()
Set Rs = Db.OpenRecordset(SQL)
Do Until rs.EOF
Deltable rs(0)
rs.MoveNext
Loop
Rs.Close
set DB = Nothing
End Sub
Nhớ lưu trước khi thử nghiệm nhé -
RE: Xoá các table theo điều kiện trong Access
dannynguyen1980 > 10-11-11, 11:57 AM
Bác cho hỏi luôn tôi có viết đoạn code sau để xóa bảng với điều khi bảng tồn tại trên database SQL Server 2000 (nối với Access 2003), khi gọi hàm DeleteTable không báo lỗi gì nhưng bảng cũng không xóa luôn. Code như sau:
Option Compare Database
Option Explicit
Function DeleteTable(ByVal TableName As String) As Boolean
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
'Set references by Clicking Tools and Then References in the Code View window
' Deletes a Table with a given TableName that exists in a database
' Accepts
' TableName: Name of table
' Returns True on success, false otherwise
'USAGE: DeleteTable "TABLENAME"
On Error GoTo errhandler
Dim Db As ADODB.Connection
Dim strSql As String
Set Db = CurrentProject.Connection
'Create the Delte SQL Code from our Three Strings
strSql = "DROP TABLE " & TableName
If ifTableExists(TableName) = True Then
'Print the SQL so we can paste into the query builder if there are errors
Debug.Print strSql
'delete table if found
Db.Execute strSql
End If
'If no errors return true
DeleteTable = True
ExitHere:
Set Db = Nothing
'Notify the user the process is complete.
MsgBox "Delete Table Completed"
Exit Function
errhandler:
'There is an error return false
DeleteTable = False
With Err
MsgBox "Error " & .Number & vbCrLf & .Description, _
vbOKOnly Or vbCritical, "DeleteTable"
End With
Resume ExitHere
End Function
Function ifTableExists(TableName As String) As Boolean
Dim rs As Recordset, Db As ADODB.Connection
' DAO Vars
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
'Set references by Clicking Tools and Then References in the Code View window
'Checks if Table exists.
'USAGE: ifTableExists "TABLENAME"
On Error GoTo NoTable 'If there is no table capture the error.
Set Db = CurrentProject.Connection
'If Table is there open it
Set rs = Db.OpenRecordset("Select * from " & TableName & ";")
ifTableExists = True
rs.Close
Db.Close
Exit Function
NoTable:
'If table is not there close out and set function to false
Set rs = Nothing
Db.Close
Set Db = Nothing
ifTableExists = False
Exit Function
End Function
Mong bác chỉ giúp.
Trân trọng cảm ơn! -
RE: Xoá các table theo điều kiện trong Access
Noname > 10-11-11, 12:09 PM
Bạn có thể nói rõ ADODB.Connection ...là trỏ tới đâu không? Bạn đã có connect lấy dữ liệu ra..hoặc làm gì được tương tự với SQL server chưa? -
RE: Xoá các table theo điều kiện trong Access
dannynguyen1980 > 10-11-11, 02:24 PM
(10-11-11, 12:09 PM)Noname Đã viết: Bạn có thể nói rõ ADODB.Connection ...là trỏ tới đâu không? Bạn đã có connect lấy dữ liệu ra..hoặc làm gì được tương tự với SQL server chưa?
Cảm ơn Noname đã quan tâm đến thắc mắc của tôi.
Theo tôi hiểu ADODB.Connection ... trỏ tới CurrentProject. Còn vấn đề connect để lấy dữ liệu hoặc làm gì khác với SQL Server thì tôi chưa thử!
Tôi chỉ mới biết nếu thay:
Dim Db As ADODB.Connection
.
.
.
Set Db = CurrentProject.Connection
bằng
Dim Db as DAO.Database
.
.
.
Set Db = CurrentDb()
trong đoạn code trên và dùng 1 nút lệnh để Call Funtion DeleteTable thì chạy rất ngon lành cành đào trên database của Access.
demo ở đây: http://www.mediafire.com/?j2nnvhogz9m9u5u
Trân trọng./.
-
RE: Xoá các table theo điều kiện trong Access
Noname > 10-11-11, 03:15 PM
(10-11-11, 02:24 PM)dannynguyen1980 Đã viết:
Mình hỏi vậy để chắc rằng kết nối của bạn không có vấn đề gì!(10-11-11, 12:09 PM)Noname Đã viết: Bạn có thể nói rõ ADODB.Connection ...là trỏ tới đâu không? Bạn đã có connect lấy dữ liệu ra..hoặc làm gì được tương tự với SQL server chưa?
Cảm ơn Noname đã quan tâm đến thắc mắc của tôi.
Theo tôi hiểu ADODB.Connection ... trỏ tới CurrentProject. Còn vấn đề connect để lấy dữ liệu hoặc làm gì khác với SQL Server thì tôi chưa thử!
Tôi chỉ mới biết nếu thay:
Dim Db As ADODB.Connection
.
.
.
Set Db = CurrentProject.Connection
bằng
Dim Db as DAO.Database
.
.
.
Set Db = CurrentDb()
trong đoạn code trên và dùng 1 nút lệnh để Call Funtion DeleteTable thì chạy rất ngon lành cành đào trên database của Access.
demo ở đây: http://www.mediafire.com/?j2nnvhogz9m9u5u
Trân trọng./.
Nếu là current project, tức là nội bộ của file Access thì không vấn đề. Nhưng yêu cầu của bạn là xóa table trên SQL server. Vì vậy, còn tùy thuộc vào nhiều yếu tố như permission... nếu bạn thử được các tác vụ thêm sửa xóa thì mới tính tiếp.
-
RE: Xoá các table theo điều kiện trong Access
dannynguyen1980 > 11-11-11, 07:24 PM
Mình đã thử rồi vẫn thêm, sửa, xóa table trên SQL Server từ Access bằng cách khác (del, copy & paste, rename) bình thường.
Tức cái là chạy không báo lỗi gì mà table thì không xóa thôi. -
RE: Xoá các table theo điều kiện trong Access
Noname > 11-11-11, 08:09 PM
Cho hỏi có phải bạn dùng Access Project không? Vì như Code của bạn thì không có liên quan gì với SQL server cả! Không thể thao tác gì được với SQL server bằng code đó trừ khi bạn đang dùng MS Access Project (vốn đã có sẵn connection) -
RE: Xoá các table theo điều kiện trong Access
Noname > 11-11-11, 08:19 PM
Nếu bạn đang dùng Access Project hoặc dùng 1 kết nối với SQL server qua ADO thì thử dùng code này xem
Mã:Function DeleteTable(strTableName As String) As Boolean
Dim cmd As ADODB.Command
Dim strCommand As String
Dim Conn As ADODB.Connection
On Error GoTo DeleteTable_Error
Set Conn = CurrentProject.Connection
strCommand = "DROP TABLE " & strTableName
'Delete Table
Set cmd = New ADODB.Command
cmd.ActiveConnection = Conn
cmd.CommandText = strCommand
cmd.CommandType = adCmdText
cmd.Execute
DeleteTable = True
Set Conn = Nothing
Set cmd = Nothing
Exit Function
DeleteTable_Error:
MsgBox Err.Number & "-" & Error$, vbCritical, "Error...Error...Error"
Set Conn = Nothing
Set cmd = Nothing
End Function -
RE: Xoá các table theo điều kiện trong Access
dannynguyen1980 > 12-11-11, 02:56 PM
Cảm ơn bác Noname! Tôi đã xóa table được rồi. Dưới đây là đoạn mã kiểm tra điều kiện table tồn tại mà tôi sưu tầm được:
Public Function IfTableExists(strTableName As String) As Boolean
Dim cn As ADODB.Connection
Dim rs As New ADODB.Recordset
IfTableExists = False
Set cn = CurrentProject.Connection
Set rs = cn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))
Do While Not rs.EOF
If rs(2).Value = strTableName Then
IfTableExists = True
rs.MoveNext
Loop
Set rs = Nothing
cn.Close
Set cn = Nothing
End Function
Cho hởi trước đây bác học viết code này ở đâu vậy? Tôi cũng muốn đăng ký học thêm vài khóa về lập trình mà chưa biết học ở đâu?!