KySoto

crazy export function

May 30th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Sub export2()
  2. If Not BasicInclude.DebugMode Then On Error GoTo Error_Handler Else On Error GoTo 0
  3. 'On Error GoTo Error_Handler
  4. Dim app As Object
  5. Dim w As Object
  6. Dim sh As Object
  7. Dim iCols As Long
  8. Dim i As Long
  9. Dim j As Long
  10. Dim k As Long
  11. Dim c As Long
  12. Dim d As Long
  13. Dim e As Boolean
  14. Dim s(5) As String
  15. Dim v() As Variant
  16. Const xlCenter = -4108
  17. Dim q As Variant
  18. Dim qu As Long
  19. Dim r As Variant
  20. Dim t As Variant
  21. Dim out() As Variant
  22. Dim TidList As Variant
  23. Dim rs() As ADODB.Recordset
  24. Dim count As Long
  25. Dim f As Field
  26. Dim v2 As Variant
  27. Dim counter As Long
  28. Dim mem() As Variant
  29. Dim DescGroup As Long
  30. Dim ubrs As Long
  31. If Me.lstTestType.ItemsSelected.count > 0 And Trim(Me.cmbPartNumber & "") <> "" Then
  32.     Set app = CreateObject("Excel.Application")
  33.     app.ScreenUpdating = False
  34.     app.Visible = False
  35.     Set w = app.Workbooks.Add()
  36.     For i = 0 To lstTestType.ItemsSelected.count - 1
  37.         'Set sh = w.Sheets(1)
  38.        'Set rs2(0) = New ADODB.Recordset
  39.        'Set rs = rs2(0)
  40.        'the parameter list is as follows
  41.        '0 = part number
  42.        '1 = test type
  43.        '2 = serial number start
  44.        '3 = serial number end
  45.        '4 = date start
  46.        '5 = date end
  47.        s(0) = Me.cmbPartNumber
  48.         s(1) = Me.lstTestType.Column(0, Me.lstTestType.ItemsSelected(i))
  49.         If Me.txtFirst & "" <> "" Then
  50.             s(2) = Me.txtFirst
  51.             If Me.txtLast & "" = "" Then
  52.             s(3) = Me.txtFirst
  53.             Else
  54.             s(3) = Me.txtLast
  55.             End If
  56.         Else
  57.        
  58.             s(2) = 0
  59.             s(3) = 99999999
  60.         End If
  61.         If Me.txtStart & "" = "" Then
  62.         s(4) = DateSerial(Year(Now) - 20, Month(Now), Day(Now))
  63.         Else
  64.         s(4) = DateValue(Me.txtStart) & " 12:00:00 AM"
  65.         End If
  66.         If Me.txtEnd & "" = "" Then
  67.         s(5) = DateSerial(Year(Now) + 20, Month(Now), Day(Now))
  68.         Else
  69.         s(5) = DateValue(Me.txtEnd) & " 11:59:59 PM"
  70.         End If
  71.         TidList = Qlookup("qry" & Me.lstTestType.Column(1, Me.lstTestType.ItemsSelected(i)) & "Header", s)
  72.         If Not IsNull(TidList) Then
  73.             Debug.Print
  74.             q = Qlookup("qry" & Me.lstTestType.Column(1, Me.lstTestType.ItemsSelected(i)) & "Descriptions", s)
  75.             qu = UBound2(q)
  76.             DescGroup = qu
  77.             ReDim rs(qu) As ADODB.Recordset
  78.             ReDim mem(qu, 1, 100) As Variant
  79.             For j = 0 To qu
  80.                 Set rs(j) = New ADODB.Recordset
  81.                 rs(j).Fields.Append "PartNumber", adVarChar, 35, adFldKeyColumn
  82.                 rs(j).Fields.Append "SerialNumber", adInteger, , adFldKeyColumn
  83.                 rs(j).Fields.Append "TestType", adVarChar, 35
  84.                 rs(j).Fields.Append "TestDate", adDate, , adFldKeyColumn
  85.                 rs(j).Fields.Append "Good", adChar, 1
  86.                 d = 0
  87.                 On Error Resume Next
  88.                     For k = 0 To UBound2(q, 2) - 1
  89.                         If Trim(q(j, k) & "") <> "" Then
  90.                             c = 0
  91.                             e = False
  92.                             Do
  93.                             Err.Clear
  94.                                 If c = 0 Then
  95.                                     rs(j).Fields.Append Trim(q(j, k)), adDouble, , adFldIsNullable
  96.                                 Else
  97.                                     rs(j).Fields.Append Trim(q(j, k)) & c, adDouble, , adFldIsNullable
  98.                                     e = True
  99.                                 End If
  100.                                 c = c + 1
  101.                             Loop Until Err.Number = 0
  102.                             c = c - 1
  103.                             If e Then
  104.                             mem(j, 0, d) = Trim(q(j, k))
  105.                             mem(j, 1, d) = k + 6
  106.                             d = d + 1
  107.                             End If
  108.                         End If
  109.                     Next
  110.                 On Error GoTo Error_Handler
  111.             rs(j).Open
  112.             Next
  113.            
  114.             Debug.Print
  115. '-----------------I think this is where the error is-----------------------------------------------------------------
  116.            r = Qlookup("qry" & Me.lstTestType.Column(1, Me.lstTestType.ItemsSelected(i)) & "DescriptionsAndData", s)
  117.             qu = UBound2(r)
  118.             ubrs = UBound2(rs)
  119.             For j = 0 To qu
  120.                 If ubrs > 0 Then
  121.                     DescGroup = GetDescGroupNumber(r, j, q)
  122.                 Else
  123.                     DescGroup = 0
  124.                 End If
  125.                 If DescGroup > -1 Then
  126.                     rs(DescGroup).AddNew
  127.                     rs(DescGroup).Fields("SerialNumber").Value = TidList(j, 0)
  128.                     rs(DescGroup).Fields("TestDate").Value = TidList(j, 1)
  129.                     rs(DescGroup).Fields("PartNumber").Value = Me.cmbPartNumber
  130.                     rs(DescGroup).Fields("TestType").Value = Me.lstTestType.Column(0, Me.lstTestType.ItemsSelected(i))
  131.                     rs(DescGroup).Fields("Good").Value = TidList(j, 2)
  132.                     On Error Resume Next
  133.                     For k = 0 To UBound(r, 2) - 1 Step 2
  134.                         If Trim(r(j, k) & "") <> "" Then
  135.                             If IsNull(rs(DescGroup).Fields(Trim(r(j, k))).Value) Then
  136.                                 rs(DescGroup).Fields(Trim(r(j, k))).Value = Trim(r(j, k + 1))
  137.                             Else
  138.                                 c = 0
  139.                                 Do
  140.                                     c = c + 1
  141.                                 Loop Until IsNull(rs(DescGroup).Fields(Trim(r(j, k)) & c).Value)
  142.                                 rs(DescGroup).Fields(Trim(r(j, k)) & c).Value = Trim(r(j, k + 1))
  143.                             End If
  144.                         End If
  145.                         If Err.Number <> 0 Then
  146.                             Debug.Print Err.Source & " " & Err.Number & " " & Err.Description
  147.                             Err.Clear
  148.                         End If
  149.                     Next
  150.                     On Error GoTo Error_Handler
  151.                     rs(DescGroup).Update
  152.                 Else
  153.                     Debug.Print "Somehow there is no matching description group"
  154.                 End If
  155.             Next
  156. '--------------------------------------------------------------------------------------------------------------------
  157.            Debug.Print
  158.             On Error GoTo 0
  159.             For j = 0 To UBound2(q)
  160.             With rs(j)
  161.             If .RecordCount <> 0 Then
  162.                 Set sh = w.Sheets(1)
  163.                 v2 = Right(Me.lstTestType.Column(1, Me.lstTestType.ItemsSelected(i)), 2)
  164.                 If v2 = "00" Then v2 = "100"
  165.                 sh.Name = s(0) & " ITD " & v2 & " " & Me.lstTestType.Column(0, Me.lstTestType.ItemsSelected(i)) & IIf(UBound2(q) <> 0, " " & j, "")
  166.                 'Build our Header
  167.                For iCols = 0 To .Fields.count - 1
  168.                     sh.Cells(1, iCols + 1).numberformat = "@"
  169.                     sh.Cells(1, iCols + 1).Value = .Fields(iCols).Name
  170.                 Next
  171.                 With sh.Range(sh.Cells(1, 1), _
  172.                                        sh.Cells(1, .Fields.count))
  173.                     .Font.Bold = True
  174.                     .Font.ColorIndex = 2
  175.                     .Interior.ColorIndex = 1
  176.                     .HorizontalAlignment = xlCenter
  177.                 End With
  178.                 sh.Range(sh.Cells(2, 1), sh.Cells(.RecordCount + 1, 3)).numberformat = "@"
  179.                 sh.Range(sh.Cells(2, 4), sh.Cells(.RecordCount + 1, .Fields.count)).numberformat = "0.0000"
  180.                 'Copy the data from our query into Excel
  181.                sh.Range("A2").CopyFromRecordset rs(j)
  182.                 sh.Range("A1").SELECT  'Return to the top of the page
  183.                
  184.                 If mem(j, 0, 0) & "" <> "" Then
  185.                     For k = 0 To UBound2(mem, 3)
  186.                         If mem(j, 0, k) & "" <> "" Then
  187.                         sh.Cells(1, mem(j, 1, k)).Value = mem(j, 0, k)
  188.                         Else
  189.                         Exit For
  190.                         End If
  191.                        
  192.                     Next
  193.                 End If
  194.                 sh.Range(sh.Cells(1, 1), sh.Cells(.RecordCount, .Fields.count)).Columns.AutoFit    'Resize our Columns based on the headings
  195.                    w.Sheets.Add
  196.                 app.activewindow.splitcolumn = 0
  197.                 app.activewindow.splitrow = 1
  198.                 app.activewindow.freezepanes = True
  199.             End If
  200.             End With
  201.             Next
  202.            
  203.            
  204.             'Else
  205.            'MsgBox "code note implemented to handle 2 sets of data"
  206.            
  207.             'End If
  208.        Else
  209.         counter = counter + 1
  210.         End If
  211.     Next
  212.     For Each sh In w.Sheets
  213.                 If sh.Name Like "Sheet*" And w.Sheets.count > 1 Then
  214.                     w.Sheets(sh.Name).Delete
  215.                 End If
  216.     Next
  217.     If counter = Me.lstTestType.ItemsSelected.count Then
  218.     app.Quit
  219.     MsgBox "No Data Found."
  220.     Else
  221.     app.ScreenUpdating = True
  222.     app.Visible = True
  223.     End If
  224. Else
  225.     MsgBox "Please choose a part and test."
  226. End If
  227. Error_Exit:
  228. Set app = Nothing
  229. Exit Sub
  230. Error_Handler:
  231. If Not app Is Nothing Then
  232. app.Quit
  233. End If
  234.  
  235. MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
  236.            "Error Number: " & Err.Number & vbCrLf & _
  237.            "Error Source: " & Err.Source & vbCrLf & _
  238.            "Error Description: " & Err.Description _
  239.            , vbOKOnly + vbCritical, "An Error has Occured!"
  240. Resume Error_Exit
  241. End Sub
Advertisement
Add Comment
Please, Sign In to add comment