Se vi dovesse capitare, come a me, di avere in un database Access dei file di testo collegati come tabelle e voleste ricollegarli via codice in modo che l'utente dell'applicazione faccia il meno possibile, doveste stare attenti alle proprietà del file collegato, non basta ricollegarli come si farebbe con le tabelle, perchè cambia il provider che è non o mal documentato. La cosa migliore da fare è recuperare i dati dal file collegato esistente; leproprietà sono accessibili anche se il percorso del file è cambiato (e quindi va ricollegato) perchè sono è cmq un oggetto presente nel db.
io ho usato queste procedure (ok, il codice fa schifo e i nomi delle variabili sono criptici, ma tenete conto che odio vb
)
questa procedura mette in un array le proprietà del file collegato.
codice:
Public Function pf_getLinkedFileProp(f_strFileName As String) As String()
Dim f_astrFileProp(10) As String
Dim f_cn As Connection
Dim f_cat As ADOX.Catalog
Set f_cn = New ADODB.Connection
Set f_cat = New ADOX.Catalog
Set f_cat.ActiveConnection = CurrentProject.Connection
f_astrFileProp(1) = f_cat.Tables(f_strFileName).Name
Dim x As Integer
For x = 1 To 9
f_astrFileProp(x + 1) = f_cat.Tables(f_strFileName).Properties(x).Value
Next x
pf_getLinkedFileProp = f_astrFileProp
End Function
e questa lo ricollega. Alcune cose sono un po' strane, ho adattato una procedura trovata su un libro che tiene i nomi dei file o tabelle nel db. La procedura serve sia a ricollegare tabelle collegate normali che file di testo collegati ed è in teoria utilizzabile per qulunque collegamento attraverso JET OLEDB (ISAM?).
codice:
Public Sub sp_CollegaTabelle(f_rsTblCol As ADODB.Recordset, f_strDBName As String, f_strTipo As String)
Dim f_catCur As New ADOX.Catalog
Dim f_intTotTbl As Integer
Dim f_intCurTbl As Integer
Dim f_strCurTbl As String
Dim f_astrFileProp() As String
On Error GoTo Errore_CollegaTabelle
If f_strTipo = "text" Then
f_astrFileProp = pf_getLinkedFileProp(pstrNomeFile)
End If
f_rsTblCol.MoveLast
f_intTotTbl = f_rsTblCol.RecordCount
f_rsTblCol.MoveFirst
f_catCur.ActiveConnection = CurrentProject.Connection
f_intCurTbl = 1
Do Until f_rsTblCol.EOF
On Error Resume Next
f_strCurTbl = f_rsTblCol!tabella
f_catCur.Tables.Delete f_strCurTbl
f_catCur.Tables.Refresh
On Error GoTo Errore_CollegaTabelle
Dim f_tblCur As New ADOX.Table
Set f_tblCur.ParentCatalog = f_catCur
f_tblCur.Name = f_rsTblCol!tabella
If f_strTipo = "text" Then
Dim i As Integer
Dim b_strNome As String
i = InStr(1, f_strDBName, f_astrFileProp(1))
b_strNome = Left(f_strDBName, i - 1)
f_tblCur.Properties("Jet OLEDB:Link Datasource") = b_strNome
f_tblCur.Properties("Jet OLEDB:Create Link") = True
f_tblCur.Properties("Jet OLEDB:Remote Table Name") = f_rsTblCol!tabella & "#txt"
f_tblCur.Properties("Jet OLEDB:Link Provider String") = f_astrFileProp(6)
f_tblCur.Properties("Jet OLEDB:Exclusive Link") = False
Else
f_tblCur.Properties("Jet OLEDB:Link Datasource") = f_strDBName
f_tblCur.Properties("Jet OLEDB:Create Link") = True
f_tblCur.Properties("Jet OLEDB:Remote Table Name") = f_rsTblCol!tabella
End If
f_catCur.Tables.Append f_tblCur
Set f_tblCur = Nothing
f_rsTblCol.MoveNext
f_intCurTbl = f_intCurTbl + 1
Loop
Exit Sub
Errore_CollegaTabelle:
End Sub
Queste cmq sono le proprietà di un file di testo collegato
codice:
Jet OLEDB:Table Validation Text
Jet OLEDB:Table Validation Rule
Jet OLEDB:Cache Link Name/Password False
Jet OLEDB:Remote Table Name FILE_TESTATA#txt
Jet OLEDB:Link Provider String Text;DSN=FILE_TESTATA - specifica di collegamento1;FMT=Delimited;HDR=NO;IMEX=2;CharacterSet=850;
Jet OLEDB:Link Datasource C:\Documents and Settings\alex\Desktop
Jet OLEDB:Exclusive Link False
Jet OLEDB:Create Link True
Jet OLEDB:Table Hidden In Access False
Ma vanno riprese ogni volta perchè la stringa del provider può cambiare
Saluti