Public Sub 链接()
On Error GoTo LJ_error
Dim TABNAME As String
Dim Tab1 As TableDef
Dim MyPath As String
MyPath = Application.CurrentProject.Path
CurrentDb.TableDefs.Refresh '刷新当前数据库中的表对象
If CurrentDb.TableDefs(15).Connect = ";DATABASE=" & MyPath & "\出口业务记录_data" & FORMs!窗体1!所属年份 & ".mdb" Then
Exit Sub
Else
For Each Tab1 In CurrentDb.TableDefs
TABNAME = Tab1.Name
If Left(TABNAME, 2) <> "MS" And Left(TABNAME, 2) <> "SW" And Left(TABNAME, 2) <> "Us" Then
If Left(TABNAME, 2) = "Or" Then
Tab1.Connect = ";DATABASE=" & MyPath & "\出口业务记录_dataOrigin.mdb"
Else
Tab1.Connect = ";DATABASE=" & MyPath & "\出口业务记录_data" & FORMs!窗体1!所属年份 & ".mdb"
End If
Tab1.RefreshLink
End If
Next Tab1
MsgBox FORMs!窗体1!所属年份 & "年的基础数据库连接成功!"
End If
Exit_LJ_error:
Exit Sub
LJ_error:
MsgBox FORMs!窗体1!所属年份 & "年的后端数据库文件不存在!"
Resume Exit_LJ_error
End Sub
Public Sub 查找文件()
Dim MyPath As String
Dim fs As Variant
Dim TABNAME As String
Dim Tab1 As TableDef
MyPath = Application.CurrentProject.Path
Set fs = Application.FileSearch
With fs
.LookIn = MyPath
.SearchSubFolders = True
.Filename = "出口业务记录_data" & FORMs!窗体1!所属年份 & ".mdb"
If .Execute() <= 0 Then
If MsgBox("没有" & FORMs!窗体1!所属年份 & "年的数据库,是否要创建一个?", vbYesNo) = vbYes Then
FORMs!窗体1.FORM!版本.FORM.RecordSource = ""
FileCopy MyPath & "\出口业务记录_dataOrigin.mdb", MyPath & "\出口业务记录_data" & FORMs!窗体1!所属年份 & ".mdb"
Else
FORMs!窗体1!所属年份 = Year(Now())
MsgBox "没有" & FORMs!窗体1!所属年份 & "年的数据库!"
Exit Sub
End If
End If
End With
链接
End Sub
4.在窗体1的Close事件中写:
Private Sub FORM_Close()
Dim TABNAME As String
Dim Tab1 As TableDef
Dim MyPath As String
MyPath = Application.CurrentProject.Path
CurrentDb.TableDefs.Refresh '刷新当前数据库中的表对象
If CurrentDb.TableDefs(15).Connect = ";DATABASE=" & MyPath & "\出口业务记录_data" & Year(Now()) & ".mdb" Then
Exit Sub
Else
For Each Tab1 In CurrentDb.TableDefs
TABNAME = Tab1.Name
If Left(TABNAME, 2) <> "MS" And Left(TABNAME, 2) <> "SW" And Left(TABNAME, 2) <> "Us" Then
If Left(TABNAME, 2) = "Or" Then
Tab1.Connect = ";DATABASE=" & MyPath & "\出口业务记录_dataOrigin.mdb"
Else
Tab1.Connect = ";DATABASE=" & MyPath & "\出口业务记录_data" & Year(Now()) & ".mdb"
End If
Tab1.RefreshLink
End If
Next Tab1
End If
End Sub
注意窗体一最好是没有任何绑定控件的切换面板,如果有这样的控件,在更改链接表connect属性和FileCopy之前要设置这些控件的所有***Source(如RcordSource,RowSource等)="",完成相关语句后再设置成原来的值.
希望指正!!!
相关文章