Ciao Phil
Philcattivocarattere ha scritto:
Che dire... se non: "funziona!".
uomo di poca fede ... funziona ... (la funzione).
Basta che provi questa funzione che ho riesumato (adattandola al caso):
Option Compare Database
Option Explicit
' ------------------------------------------------------------------------------
' Funzione che scrive un file sequenziale (ciclando un recordset con DAO)
' in modo da porvi due campi (ID e Note ove quest'ultimo è un campo memo)
' In tal modo si supera il limite dei 255 caratteri di lunghezza di una stringa
' quali quelli che possono essere presenti in un campo memo
' Uso:
' If DAOLoopingWritingMemo("C:\MyFile.XML", "Anagrafica", "ID", "Note") = True Then
' MsgBox ("Il file è stato scritto")
' Else
' MsgBox ("Il file NON è stato scritto")
' End If
' ------------------------------------------------------------------------------
Public Function DAOLoopingWritingMemo(strNameFile As String, _
strSQL As String, _
strFieldName1 As String, _
strFieldName2 As String) _
As Boolean
' Dim strNomeFile As String is the Name of File (with path) to write
' Dim strSQL As String is the table or query
' Dim strFieldName1 As String is the name of field key (example "ID")
' Dim strFieldName2 As String is the name of memo field (example "Note")
' Return Boolean True if writing OK, else False
On Error GoTo ErrorHandler
Dim rs As DAO.Recordset
Open strNameFile For Output As #1
' As:
' Open "C:\Prova.txt" For Output As #1
' As:
' strSQL = "Anagrafica"
' For the purposes of this post, we are simply going to make
' strSQL equal to Anagrafica.
' You could use a full SELECT statement such as:
' SELECT * FROM Anagrafica (this would produce the same result in fact).
' You could also add a Where clause to filter which records are returned:
' SELECT * FROM Anagrafica Where CAP = '00100'
' (this would return n records)
Set rs = CurrentDb.OpenRecordset(strSQL)
' This line of code instantiates the recordset object!!!
' In English, this means that we have opened up a recordset
' and can access its values using the rs variable.
With rs
If Not rs.BOF And Not rs.EOF Then
' We don’t know if the recordset has any records,
' so we use this line of code to check. If there are no records
' we won’t execute any code in the if..end if statement.
rs.MoveLast
rs.MoveFirst
' It is not necessary to move to the last record and then back
' to the first one but it is good practice to do so.
' example heading
Print #1, "<?xml version=""1.0"" encoding=""utf-8""?>"
Print #1, "<cars>"
While (Not rs.EOF)
' With this code, we are using a while loop to loop
' through the records. If we reach the end of the recordset, .EOF
' will return true and we will exit the while loop.
' Debug.Print rs.Fields("ID") & " " & rs.Fields("Note")
' prints info from fields to the immediate window
' Write #1, rs.Fields("ID") & Chr(124) & rs.Fields("Note") )
' With Write there are double quote at start and end of the string
' (Con Write la stringa scritta viene racchiusa da doppi apici, con Print no)
' Print #1, rs.Fields("ID") & Chr(124) & rs.Fields("Note")
Print #1, "<element>"
' Print #1, "<external_id>" & rs.Fields("ID") & "</external_id>"
Print #1, "<external_id>" & rs.Fields(strFieldName1) & "</external_id>"
' Print #1, "<description><![CDATA[" & rs.Fields("Note") & "]]></description>"
Print #1, "<description><![CDATA[" & rs.Fields(strFieldName2) & "]]></description>"
Print #1, "</element>"
rs.MoveNext
' We need to ensure that we use .MoveNext,
' otherwise we will be stuck in a loop forever…
' (or at least until you press CTRL+Break)
Wend
Print #1, "</cars>"
End If
rs.Close
' Make sure you close the recordset...
Close #1 ' Close sequential file (Chiusura File)
DAOLoopingWritingMemo = True ' Exit function with True (OK on file writing)
End With
ExitSub:
Set rs = Nothing
' ..and set it to nothing
Exit Function
ErrorHandler:
Resume ExitSub
End Function
PS Si vede che non hai vissuto l'inizio dei primi software per i pc (degli anni '80 ma del secolo scorso) dove, magari, avevi a disposizione esclusivamente il Basic e potevi impiegare solo file sequenziali sui dispositivi.