Õî÷ó ïðåäñòàâèòü Âàì ôóíêöèè, áåç êîòîðûõ íîðìàëüíàÿ æèçíü ïðîãðàììû íà MS Access íå âîçìîæíî.
Èõ âñåãî òðè:
CM_LT_AddAllExt()
- äîáàâëÿåò â òåêóùóþ áàçó ññûëêè íà òàáëèöû èç mdb ôàéëà
CM_LT_AddAllExt_ODBC()
- äîáàâëÿåò â òåêóùóþ áàçó ññûëêè íà ODBC òàáëèöû íà ñåðâåðå
CM_LT_DelAll()
- óäàëÿåò òàáëèöû-ññûëêè èç òåêóùåé áàçû
Îíè ïîçâîëÿþò ñîçäàòü â èíòåðôåéñíîé áàçå ññûëêè íà òàáëèöû èç áàçû ñ äàííûì. Ó ìåíÿ îíè çàïóñêàþòñÿ êàæäûé ðàç ïðè çàïóñêå. Íàïèñàíû îíè äàâíûì äàâíî, íî ïîëåçíû äî ñèõ ïîð.
Òåêñò ôóíêöèé:
view plain copy to clipboard print
- Public Function CM_LT_AddAllExt(ByVal stPathToBase As String) As Long
- ' <Ñêîêîâ Ñ.À.> ñîçäàíà: 2004-02-05
- ' ïîäëèíêîâûâàåò âñå òàáëèöû èç óêàçàííîé áàçû
- ' ïðîâåðÿåò ñóùåñòâóåò ëè ïîäëèíêîâûâàåìàÿ òàáëèöà â òåêóùåé êàê ññûëêà, òî îáíîâëÿåòñÿ ñòðîêà ïîäêëþ÷åíèÿ.
- ' åñëè æå â òåê. áàçå åñòü òàáëèöà ñ òàêèì èìåíåì (íå ññûëêà), òî ïîäëèíêîâûâàåìàÿ òàáëèöà ïðîïóñêàåòñÿ
- ' ò.î. ïåðåä âûçîâîì ýòîé ôóíêöèè óäàëÿòü ëèíêîâàííûå òàáëèöû íå íóæíî
- ' âõîä: stPathToBase - ïóòü è èìÿ áàçû
- ' âûõîä: êîëè÷åñòâî íå ïîäëèíêîâàííûõ òàáëèö, â ñëó÷àå îøèáêè âîçâðàùàåò -1
-
- On Error GoTo Err_
- CM_LT_AddAllExt = 0
-
- Dim tdf As TableDef
- Dim db As Database
- Dim bIsSysOrLink As Boolean
- Dim stNameTbl As String
- Dim lCountNotLinket As Long ' êîëè÷åñòâî íå ïîäëèíêîâàííûõ òàáëèö
- Dim stConnect As String
- Dim dbCur As DAO.Database
- Dim tdfNew As DAO.TableDef
- Dim tdfsCur As DAO.TableDefs
-
- stConnect = ";DATABASE=" & stPathToBase
- Set dbCur = CurrentDb
- Set tdfsCur = dbCur.TableDefs
-
- '-- äåëàåì ìàñèâ òàáëèö â òåêóùåé áàçå
- Dim masNameTbl() As String
- Dim i As Long
-
- tdfsCur.Refresh
- ReDim masNameTbl(tdfsCur.count - 1)
- i = 0
- For Each tdf In tdfsCur
- masNameTbl(i) = tdf.Name
- i = i + 1
- Next tdf
-
- '-- êîííåêòèìñÿ ê áàçå
- Set db = OpenDatabase(stPathToBase)
-
- lCountNotLinket = 0
- '-- ëèíêóåì
- For Each tdf In db.TableDefs
- bIsSysOrLink = (tdf.Attributes And dbSystemObject) Or _
- (tdf.Attributes And dbHiddenObject) _
- Or (tdf.Attributes And dbAttachedTable) ' ñèñòåìíàÿ èëè ïðèñåîåäèíåííàÿ ëè?
-
- If Not bIsSysOrLink Then ' åñëè íå òî ÷òî âûøå, òî ìîæíî äåëàòü ëèíê
- stNameTbl = tdf.Name
- '-- åñëè òàêàÿ òàáëèöà ñóùåñòâóåò â òåêóùåé áàçå
- If SerchStrInMas(masNameTbl, stNameTbl) <> -1 Then
- '-- òî ïðîâåðÿåì ïîäëèíêîâàíàÿ ëè? èíà÷å ïðîïóñêàåì ýòó òàáëèöó è ïåðåõîäèì íà ñëåäóþùóþ
- If (tdfsCur(stNameTbl).Attributes And dbAttachedTable) Then
- '-- îáíîâëÿåì ïóòü ê áä
- tdfsCur(stNameTbl).Connect = stConnect
- tdfsCur(stNameTbl).RefreshLink
- Else
- Debug.Print "CM_LT_AddAllExt(), ïðîïóùåíà òàáëèöà:", stNameTbl
- lCountNotLinket = lCountNotLinket + 1
- End If
- Else
- '-- íå ñóùåñòâóåò - òî ëèíêóåì
- Set tdfNew = dbCur.CreateTableDef(stNameTbl)
- tdfNew.SourceTableName = stNameTbl
- tdfNew.Connect = stConnect
- tdfsCur.Append tdfNew
- End If
- End If
- Next tdf
-
- db.Close
- Set db = Nothing
-
- tdfsCur.Refresh
- Set tdfsCur = Nothing
- Set dbCur = Nothing
-
- CM_LT_AddAllExt = lCountNotLinket
- Exit_:
- Exit Function
-
- Err_:
- CM_LT_AddAllExt = -1
- Err.Raise Err.Number, "CM_LT_AddAllExt()->" & Err.Source, Err.Description '-- ïåðåäàåì îøèáêó â âûçâàâøóþ ôóíêöèþ
-
- Resume Exit_
- End Function
-
- Private Function SerchStrInMas(ByRef masStr() As String, ByRef SerchStr As String) As Long
- ' <Ñêîêîâ Ñ.À.> ñîçäàíà: 2004-02-05
-
- ' Ïîèñê ñòðîêè â ñòðîêîâîì ìàññèâå
- ' âõîä: masStr - ìàññèâ ñòðîê
- ' SerchStr - èñêîìàÿ ñòðîêà
- ' âûõîä:
- ' íîìåð ýëåìåíòà ìàññèâà, â êîòîðîì áûëà íàéäåíà ïîäñòðîêà SerchStr, èíà÷å -1 (êîãäà íåò ñîâïàäåíèé)
- ' ïðè îøèáêå âîçâðàùàåò -1
-
- On Error GoTo Err_
-
- Dim i As Long
-
- SerchStrInMas = -1
-
- For i = LBound(masStr) To UBound(masStr)
- If masStr(i) = SerchStr Then
- SerchStrInMas = i
- Exit For
- End If
- Next i
-
- Exit_:
- Exit Function
- Err_:
- SerchStrInMas = -1
- Resume Exit_
- End Function
-
- Public Function CM_LT_AddAllExt_ODBC(ByVal stConnectStr As String) As Long
- ' <Êóëàãà Ñ.Þ.> ñîçäàíà: 2006-10-12
-
- ' ïîäëèíêîâûâàåò âñå òàáëèöû èç óêàçàííîé áàçû
- ' ïðîâåðÿåò ñóùåñòâóåò ëè ïîäëèíêîâûâàåìàÿ òàáëèöà â òåêóùåé êàê ññûëêà, òî óäàëÿåò.
- ' åñëè æå ýòî ÿâëÿåòñÿ òàáëèöåé, òî ïîäëèíêîâûâàåìàÿ òàáëèöà ïðîïóñêàåòñÿ
- ' ò.å. ïåðåä âûçîâîì ýòîé ôóíêöèè óäàëÿòü ëèíêîâàííûå òàáëèöû íå íóæíî, îí óäàëèòü íåîáõîäèìûå ñàìà
- ' âõîä: stConnectStr - ñòðîêà ïîäêëþ÷åíèÿ ADO
- ' âûõîä: êîëè÷åñòâî íå ïîäëèíêîâàííûõ òàáëèö, â ñëó÷àå îøèáêè âîçâðàùàåò -1
-
- On Error GoTo Err_
- CM_LT_AddAllExt_ODBC = 0
-
- Dim bIsSysOrLink As Boolean
- Dim stNameTbl As String
- Dim tdf As TableDef
- Dim lCountNotLinket As Long ' êîëè÷åñòâî íå ïîäëèíêîâàííûõ òàáëèö
- Dim cnn As ADODB.Connection
- Dim rst As ADODB.Recordset
- Dim stConnectTbl As String
- Dim dbCur As DAO.Database
- Dim tdfNew As DAO.TableDef
- Dim tdfsCur As DAO.TableDefs
-
- stConnectTbl = "ODBC;" & stConnectStr
- Set dbCur = CurrentDb
- Set tdfsCur = dbCur.TableDefs
-
- ' äåëàåì ìàñèâ òàáëèö â òåêóùåé áàçå
- Dim masNameTbl() As String
- Dim i As Long
-
- ReDim masNameTbl(tdfsCur.count - 1)
- i = 0
- For Each tdf In tdfsCur
- masNameTbl(i) = tdf.Name
- i = i + 1
- Next tdf
-
- ' êîííåêòèìñÿ ê áàçå
- Set cnn = New ADODB.Connection
- cnn.Open (stConnectStr)
- Set rst = cnn.OpenSchema(adSchemaTables)
-
- lCountNotLinket = 0
- ' ëèíêóåì
- Do While Not rst.EOF
- stNameTbl = rst("TABLE_NAME")
- ' åñëè òàêàÿ òàáëèöà ñóùåñòâóåò â òåêóùåé áàçå
- If SerchStrInMas(masNameTbl, stNameTbl) <> -1 Then
- ' òî ïðîâåðÿåì ëèíêîâàíàÿ ëè? èíà÷å ïðîïóñàåì ýòó òàáëèöó è ïåðåõîäèì íà ñëåäóþùóþ
- If (tdfsCur(stNameTbl).Attributes And (dbAttachedTable + dbAttachedODBC)) Then
- '-- îáíîâëÿåì ïóòü ê áä
- tdfsCur(stNameTbl).Connect = stConnectTbl
- tdfsCur(stNameTbl).RefreshLink
- Else
- Debug.Print "CM_LT_AddAllExt_ODBC(), ïðîïóùåíà òàáëèöà:", stNameTbl
- lCountNotLinket = lCountNotLinket + 1
- End If
- Else
- '-- íå ñóùåñòâóåò - òî ëèíêóåì
- Set tdfNew = dbCur.CreateTableDef(stNameTbl)
- tdfNew.SourceTableName = stNameTbl
- tdfNew.Connect = stConnectTbl
- tdfsCur.Append tdfNew
- End If
- rst.MoveNext
- Loop
-
- tdfsCur.Refresh
- Set tdfsCur = Nothing
- Set dbCur = Nothing
-
- rst.Close
- cnn.Close
- CM_LT_AddAllExt_ODBC = lCountNotLinket
-
- Exit_:
- Exit Function
-
- Err_:
- Err.Raise Err.Number, "CM_LT_AddAllExt_ODBC()->" & Err.Source, Err.Description '-- ïåðåäàåì îøèáêó â âûçâàâøóþ ôóíêöèþ
- CM_LT_AddAllExt_ODBC = -1
- Resume Exit_
- End Function
-
- Public Function CM_LT_DelAll() As Boolean
- ' <Ñêîêîâ Ñ.À.> ñîçäàíà: 2003-12-12
-
- ' óäàëÿåò âñå ñâÿçàíûå òàáëèöû â òåêóùåé áàçå
-
- On Error GoTo Err_
-
- Dim tdf As TableDef
- Dim db As Database
- Dim bIsAttached As Boolean
-
- Set db = CurrentDb
-
- For Each tdf In db.TableDefs
- bIsAttached = (tdf.Attributes And dbAttachedODBC) _
- Or (tdf.Attributes And dbAttachedTable) ' ïðèñåîåäèíåííàÿ òàáëèöà îáûêíîâåííàÿ èëè ODBC
-
- If bIsAttached Then ' óäàëÿåì òîëüêî ïðèëèíêîâàííûå
- DoCmd.DeleteObject acTable, tdf.Name
- End If
- Next
-
- Set db = Nothing
- CM_LT_DelAll = True
- Exit_:
- Exit Function
- Err_:
- CM_LT_DelAll = False
-
- Err.Raise Err.Number, "CM_LT_DelAll()->" & Err.Source, Err.Description '-- ïåðåäàåì îøèáêó â âûçâàâøóþ ôóíêöèþ
- Resume Exit_
- End Function
Äî âñòðå÷è!