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 '刷新當(dāng)前數(shù)據(jù)庫(kù)中的表對(duì)象
If CurrentDb.TableDefs(15).Connect = ";DATABASE=" & MyPath & "\出口業(yè)務(wù)記錄_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 & "\出口業(yè)務(wù)記錄_dataOrigin.mdb"
Else
Tab1.Connect = ";DATABASE=" & MyPath & "\出口業(yè)務(wù)記錄_data" & FORMs!窗體1!所屬年份 & ".mdb"
End If
Tab1.RefreshLink
End If
Next Tab1
MsgBox FORMs!窗體1!所屬年份 & "年的基礎(chǔ)數(shù)據(jù)庫(kù)連接成功!"
End If
Exit_LJ_error:
Exit Sub
LJ_error:
MsgBox FORMs!窗體1!所屬年份 & "年的后端數(shù)據(jù)庫(kù)文件不存在!"
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 = "出口業(yè)務(wù)記錄_data" & FORMs!窗體1!所屬年份 & ".mdb"
If .Execute() <= 0 Then
If MsgBox("沒(méi)有" & FORMs!窗體1!所屬年份 & "年的數(shù)據(jù)庫(kù),是否要?jiǎng)?chuàng)建一個(gè)?", vbYesNo) = vbYes Then
FORMs!窗體1.FORM!版本.FORM.RecordSource = ""
FileCopy MyPath & "\出口業(yè)務(wù)記錄_dataOrigin.mdb", MyPath & "\出口業(yè)務(wù)記錄_data" & FORMs!窗體1!所屬年份 & ".mdb"
Else
FORMs!窗體1!所屬年份 = Year(Now())
MsgBox "沒(méi)有" & FORMs!窗體1!所屬年份 & "年的數(shù)據(jù)庫(kù)!"
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 '刷新當(dāng)前數(shù)據(jù)庫(kù)中的表對(duì)象
If CurrentDb.TableDefs(15).Connect = ";DATABASE=" & MyPath & "\出口業(yè)務(wù)記錄_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 & "\出口業(yè)務(wù)記錄_dataOrigin.mdb"
Else
Tab1.Connect = ";DATABASE=" & MyPath & "\出口業(yè)務(wù)記錄_data" & Year(Now()) & ".mdb"
End If
Tab1.RefreshLink
End If
Next Tab1
End If
End Sub
注意窗體一最好是沒(méi)有任何綁定控件的切換面板,如果有這樣的控件,在更改鏈接表connect屬性和FileCopy之前要設(shè)置這些控件的所有***Source(如RcordSource,RowSource等)="",完成相關(guān)語(yǔ)句后再設(shè)置成原來(lái)的值.
希望指正!!!
相關(guān)文章