Bài viết này sẽ hướng dẫn cách tạo file acces tự động bằng VBA trên excel.
1. Tạo file access tự động bằng macro
Cách 1: Ưu điểm, chạy code tự động, tiện lợi khi chia sẻ, không cần thiết định gì.
Kết quả:
Cách 2: Cần thiết định trên file excel vì vậy hơi bất tiện.
Cách thiết định:
2. Tạo file access tự động, đồng thời tạo newtable trong file access.
Kết quả:
Bạn có thể download file excel đã chứa những đoạn code trên.
File :
1. Tạo file access tự động bằng macro
Cách 1: Ưu điểm, chạy code tự động, tiện lợi khi chia sẻ, không cần thiết định gì.
Mã:
Sub Example1()
'the path to create the new access database
Dim strPath As String
'an Access object
Dim objAccess As Object
strPath = "D:\VBA\NewDB"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Quit
End Sub
Bạn cần đăng nhập để thấy hình ảnh
Cách 2: Cần thiết định trên file excel vì vậy hơi bất tiện.
Cách thiết định:
Bạn cần đăng nhập để thấy hình ảnh
Mã:
Sub Example2()
'the path to create the new access database
Dim strPath As String
'an Access object
Dim objAccess As Access.Application
strPath = "D:\VBA\NewDB2"
Set objAccess = New Access.Application
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Quit
End Sub
Mã:
Sub Example3()
'the path to create the new access database
Dim strPath As String
'an Access object
Dim objAccess As Access.Application
strPath = "D:\VBA\NewDB3"
Set objAccess = New Access.Application
Call objAccess.NewCurrentDatabase(strPath)
'create table
objAccess.CurrentProject.Connection.Execute ( _
"Create Table tuhocvba_table")
End Sub
Bạn cần đăng nhập để thấy hình ảnh
Bạn có thể download file excel đã chứa những đoạn code trên.
File :
Bạn cần đăng nhập để thấy link