'dim....
'路径,是否存在
cPath = CurrentProject.Path
If Right(cPath, 1) <> "/" Then cPath = cPath & "/"
'sFile = "Dashboard.mdb"
If IsNull(sFile) Or Trim(sFile) = "" Or Right(sFile, 4) <> ".mdb" Then
MsgBox "file name error"
Exit Sub
End If
Set tmpFSO = CreateObject("Scripting.FileSystemObject")
If tmpFSO.FileExists(cPath & sFile) Then
'tmpFSO.DeleteFile cPath & sFile
MsgBox sFile & " has exist"
Exit Sub
End If
Set tmpFSO = Nothing
’创建
Set accessApp = CreateObject("Access.Application")
accessApp.CreateNewWorkgroupFile (cPath & sFile)
‘写入数据
sql_str = " select * into [" & cPath & sFile & "].T_summary from T_summary "
Call DoCmd.RunSQL(sql_str, True)
'路径,是否存在
cPath = CurrentProject.Path
If Right(cPath, 1) <> "/" Then cPath = cPath & "/"
'sFile = "Dashboard.mdb"
If IsNull(sFile) Or Trim(sFile) = "" Or Right(sFile, 4) <> ".mdb" Then
MsgBox "file name error"
Exit Sub
End If
Set tmpFSO = CreateObject("Scripting.FileSystemObject")
If tmpFSO.FileExists(cPath & sFile) Then
'tmpFSO.DeleteFile cPath & sFile
MsgBox sFile & " has exist"
Exit Sub
End If
Set tmpFSO = Nothing
’创建
Set accessApp = CreateObject("Access.Application")
accessApp.CreateNewWorkgroupFile (cPath & sFile)
‘写入数据
sql_str = " select * into [" & cPath & sFile & "].T_summary from T_summary "
Call DoCmd.RunSQL(sql_str, True)

这段VBA代码演示了如何在Access中动态创建一个新的mdb文件,并从现有表`T_summary`中通过SQL语句将数据写入新建文件的同名表中。首先检查文件路径是否正确,然后判断文件是否存在,如果已存在则提示并退出,否则创建新文件并执行写入操作。

2万+

被折叠的 条评论
为什么被折叠?



