VBA/Excel/Access/Word/Access/Recordset Convert

Материал из VB Эксперт

Перейти к: навигация, поиск

Converting the Recordset to a String

Sub GetRecords_AsString()
   Dim conn As ADODB.Connection
   Dim rst As ADODB.Recordset
   Dim varRst As Variant
   Dim myFileSystemObject As Object
   Dim myFile As Object
   Set conn = CurrentProject.Connection
   Set rst = New ADODB.Recordset
   rst.Open "SELECT EmployeeId, LastName ,FirstName as FullName FROM Employees", _
      conn, adOpenForwardOnly, adLockReadOnly, adCmdText
   If Not rst.EOF Then
      varRst = rst.GetString(adClipString, , vbTab, vbCrLf)
      Debug.Print varRst
   End If
   Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
      Set myFile = myFileSystemObject.CreateTextFile(CurrentProject.Path & _
          "\RstString.txt", True)
   myFile.WriteLine varRst
   Set myFileSystemObject = Nothing
   Set rst = Nothing
   Set conn = Nothing
End Sub