VBA, MS Access MS Access в примерах

Вывод результата выполнения запроса SELECT в текстовый файл

По материалам: https://www.cyberforum.ru/ms-access/thread2860557.html

' выводит в окно Immediate результат выполнения Select 
Public Function q(Optional strSQL As String = "", Optional intWidth As Integer = 10, Optional intMax As Integer = 100) As Boolean
     
      Dim tmpRCDSet As Recordset, tmpFeld As Field, tmpString As String, i As Integer, intTemplen As Integer
      Dim intNr As Integer
      
      On Error GoTo Err_SQL
      Debug.Print "   [  Running [" & strSQL & "]"
      Set tmpRCDSet = CurrentDb.OpenRecordset(strSQL)
        tmpRCDSet.MoveLast
        Debug.Print "   [  Query; returned; " & tmpRCDSet.RecordCount & "; entries."
        tmpRCDSet.MoveFirst
        tmpString = "| ? | "
        For Each tmpFeld In tmpRCDSet.Fields
          tmpString = tmpString & padleft(tmpFeld.Name, intWidth) & " | "
        Next
        
        Debug.Print "   [  " & String(Len(tmpString) - 1, "-")
        Debug.Print "   [  " & tmpString
        Debug.Print "   [  " & String(Len(tmpString) - 1, "-")
        
        intNr = 1
        While (Not (tmpRCDSet.EOF)) And (intNr <= intMax)
          tmpString = "| " & padleft(str(intNr), 4) & "| "
          For Each tmpFeld In tmpRCDSet.Fields
            tmpString = tmpString & padleft(Nz(tmpFeld.Value, ""), intWidth) & " | "
          Next
          Debug.Print "   [  " & tmpString
          intNr = intNr + 1
          tmpRCDSet.MoveNext
        Wend
        Debug.Print "   [  " & String(Len(tmpString) - 1, "-")
      
      Exit Function
     
     
Err_SQL:
      
      Debug.Print "   [  " & Err.Number & " " & Err.Description
      Debug.Print "   [  " & "Bad; SQL; string"
     
     
End Function
     
Function padleft(strLineIn As String, intWidth As Integer) As String
     
    If Len(strLineIn) = intWidth Then
      padleft = strLineIn
    ElseIf Len(strLineIn) > intWidth Then
      padleft = Mid(strLineIn, 1, intWidth)
    Else
      padleft = String(intWidth - Len(strLineIn), " ") & strLineIn
    End If
     
End Function
Назад ToTop
L.E. 29.07.2021
Рейтинг@Mail.ru