home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD7388732000.psc / SqlFileCopyWorking / modTransfers.bas < prev    next >
Encoding:
BASIC Source File  |  2000-02-16  |  12.8 KB  |  491 lines

  1. Attribute VB_Name = "modDataTransfer"
  2. Option Explicit
  3. Declare Function timeGetTime Lib "winmm.dll" () As Long
  4. Public sBuffer As String
  5.  
  6. ' Declare RDO objects
  7. Public Sourcetest As rdoConnection
  8. Public SourceEr As rdoError
  9. Public SourceEnv As rdoEnvironment
  10. Public SourceCon As New rdoConnection
  11. Public SourceQuery As New rdoQuery
  12. Public SourceResult As rdoResultset
  13. Public SourceRowBuf As Variant
  14. Public SourceRowsReturned As Integer
  15. Public TargetEr As rdoError
  16. Public TargetEnv As rdoEnvironment
  17. Public TargetCon As New rdoConnection
  18. Public TargetQuery As New rdoQuery
  19. Public TargetResult As rdoResultset
  20. Public TargetRowBuf As Variant
  21. Public TargetRowsReturned As Integer
  22. Public MaintEr As rdoError
  23. Public MaintEnv As rdoEnvironment
  24. Public MaintCon As New rdoConnection
  25. Public MaintQuery As New rdoQuery
  26. Public MaintResult As rdoResultset
  27. Public MaintRowBuf As Variant
  28. Public MaintRowsReturned As Integer
  29. 'End of RDO declarations
  30. Public iCount As Integer
  31. Public iRecordCount As Long
  32. Public iCurrentCount As Long
  33. Public Progress
  34. Public iRecordsAffected As Long
  35.  
  36. Global SourceConnectTest As Boolean
  37. Global TargetConnectTest As Boolean
  38. Public Function ConvertDate(sDate As Variant) As String
  39.  
  40. Dim sWorkDate
  41.  
  42. If CDate(sDate) Then
  43.     sWorkDate = Mid(sDate, 1, 2) & "-" & GetMonth(Mid(sDate, 4, 2)) & "-" & Mid(sDate, 7, Len(sDate) - 6)
  44. Else
  45.     sWorkDate = "'null'"
  46. End If
  47.  
  48. ConvertDate = sWorkDate
  49. End Function
  50. Public Function GetMonth(sMonth As Integer) As String
  51. 'array aMonths ("Jan","Feb","Mar","apr")
  52.  
  53. Dim aMonths As Variant
  54. aMonths = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
  55. GetMonth = aMonths(sMonth - 1)
  56.  
  57. End Function
  58.  
  59.  
  60. Public Function GetFields(sFilename As String, FormX As Form, ListX As ListBox, ConX As rdoConnection) As Boolean
  61.  
  62. 'Dim RowBuf As Variant
  63. Dim iFields As Integer
  64. Dim ii As Integer
  65. Dim sFieldName As String * 20
  66. Dim sFieldType As String * 12
  67.  
  68. Set MaintQuery = Nothing
  69.     With MaintQuery
  70.         .Name = "GetRowsQuery"
  71.         .SQL = "Select * from " & sFilename
  72.         .RowsetSize = 1
  73.         Set .ActiveConnection = SourceCon
  74.         Set MaintResult = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  75.     End With
  76.     MaintResult.Requery
  77. Dim Test
  78. 'Test = ConX.LastQueryResults.Status
  79. DoEvents
  80. 'RowBuf = MaintResult.GetRows(2)      'Get the total number of records in the SourceFile
  81.  
  82. DoEvents
  83.  
  84. iFields = MaintResult.rdoColumns.Count
  85.  
  86.  For ii = 0 To iFields - 1
  87.  sFieldName = MaintResult.rdoColumns(ii).Name
  88.  sFieldType = GetType(MaintResult.rdoColumns(ii).Type)
  89.     ListX.AddItem sFieldName & sFieldType & MaintResult.rdoColumns(ii).Size
  90. Next ii
  91.  
  92. End Function
  93. Public Function ClearFile(sFilename As String, FormX As Form, ConX As rdoConnection) As Boolean
  94.  
  95. On Error GoTo Clearfile_Error
  96.  
  97. If FormX.optClear(0).Value = True Then Exit Function
  98.  
  99. 'Dim RowBuf As Variant
  100. Dim iFields As Integer
  101. Dim ii As Integer
  102.  
  103. Set MaintQuery = Nothing
  104.     With MaintQuery
  105.         .Name = "GetRowsQuery"
  106.         If FormX.optClear(1).Value = True Then
  107.             .SQL = "Truncate Table " & sFilename
  108.         Else
  109.             .SQL = "Delete from " & sFilename
  110.         End If
  111.         .RowsetSize = 1
  112.         Set .ActiveConnection = ConX
  113.         Set MaintResult = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  114.     End With
  115.     'MaintResult.Requery
  116.  
  117. DoEvents
  118. FormX.lstResults.AddItem "Cleared " & sFilename
  119. FormX.stb.Panels(2).Text = "Cleared " & sFilename
  120. DoEvents
  121. Exit Function
  122. Clearfile_Error:
  123. If FormX.optClear(1).Value = True Then
  124.     MsgBox ("Failed to truncate " & sFilename)
  125. Else
  126.     MsgBox ("Failed to delete from " & sFilename)
  127. End If
  128.  
  129. End Function
  130. Public Function GetType(Index As Integer) As String
  131. Select Case Index
  132.  
  133. Case 1
  134.     GetType = "Char"
  135.     
  136. Case 2
  137.     GetType = "Numeric"
  138.     
  139. Case 3
  140.     GetType = "Decimal"
  141.     
  142. Case 4
  143.     GetType = "Integer"
  144.     
  145. Case 5
  146.     GetType = "Small Integer"
  147.     
  148. Case 6
  149.     GetType = "Float"
  150.     
  151. Case 7
  152.     GetType = "Real"
  153.     
  154. Case 8
  155.     GetType = "Double"
  156.     
  157. Case 9
  158.     GetType = "Date"
  159.     
  160. Case 10
  161.     GetType = "Time"
  162.     
  163. Case 11
  164.     GetType = "TimeStamp"
  165.     
  166. Case 12
  167.     GetType = "VarChar"
  168.     
  169. Case -1
  170.     GetType = "LongVarChar"
  171.  
  172. Case -2
  173.     GetType = "Binary"
  174.  
  175. Case -3
  176.     GetType = "VarBinary"
  177.  
  178. Case -4
  179.     GetType = "LongVarBinary"
  180.  
  181. Case -5
  182.     GetType = "BigInteger"
  183.     
  184. Case -6
  185.     GetType = "TinyInteger"
  186.     
  187. Case -7
  188.     GetType = "Bit"
  189.     
  190. End Select
  191.  
  192. End Function
  193. Public Function Transfer(sSourceFile As String, sTargetFile As String, FormX As Form) As Boolean
  194.  
  195. 'On Error GoTo transfer_Error
  196.  
  197. Dim TotalRecords
  198. Dim Printline As String
  199. Dim NewKey As String
  200. Dim FileName
  201. Dim iFields As Integer
  202. Dim RowBuf As Variant
  203. Dim RowsReturned As Long
  204. Dim bUpdate As Boolean
  205. Dim TargetSQL As String
  206.  
  207. With FormX
  208.     DoEvents
  209.     TotalRecords = 0
  210.     Load pgb
  211.     pgb.Show
  212.     DoEvents
  213.     .stb.Panels(2).Text = "Setting up the Data Environment"
  214.     
  215.     ' Get the Rowcount on the Source File
  216.     Set SourceQuery = Nothing
  217.     With SourceQuery
  218.         .Name = "GetRowsQuery"
  219.         .SQL = "Select count (*) from " & sSourceFile
  220.         .RowsetSize = 1
  221.         Set .ActiveConnection = SourceCon
  222.         Set SourceResult = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  223.     End With
  224.     SourceResult.Requery
  225.     DoEvents
  226.     RowBuf = SourceResult.GetRows(5)      'Get the total number of records in the SourceFile
  227.     iRecordCount = RowBuf(0, 0)
  228.     .lstResults.AddItem iRecordCount & " selected from sourcefile for upload *"
  229.     DoEvents
  230.     'End of recordcount code
  231.     
  232.     'Start the Update
  233.     iCurrentCount = 0
  234.     'Set up the Source Environment
  235.     Set SourceQuery = Nothing
  236.     With SourceQuery
  237.         .Name = "GetRowsQuery"
  238.         .SQL = "Select * from " & sSourceFile
  239.         .RowsetSize = 1
  240.         Set .ActiveConnection = SourceCon
  241.         Set SourceResult = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  242.     End With
  243.  
  244.     .stb.Panels(2).Text = "Submitting the querry"
  245.  
  246.      'Now Execute the SQL statement and get the Records
  247.      'We need to loop for every 50 lines
  248.  
  249.     SourceResult.Requery 'Get all the records from the Sourcefile
  250.     DoEvents
  251.     
  252.     Dim i As Integer
  253.     Do Until SourceResult.EOF
  254.     .stb.Panels(2).Text = "Retreiving the Recordsets"
  255.         SourceRowBuf = SourceResult.GetRows(1000)      'Get all the records from the source file
  256.         RowsReturned = UBound(SourceRowBuf, 2) + 1
  257.  
  258.         For i = 0 To RowsReturned - 1
  259.             iCurrentCount = iCurrentCount + 1
  260.             Progress = pgb.Progress(iCurrentCount, iRecordCount)
  261.             .stb.Panels(2).Text = "Processing for : " & SourceRowBuf(0, i)
  262.             DoEvents
  263.             
  264.             TargetSQL = CreateSQL(SourceResult, SourceRowBuf, i, sTargetFile)
  265.             bUpdate = DoUpdate(TargetSQL)
  266.  
  267.         Next i
  268.  
  269.     TotalRecords = TotalRecords + RowsReturned
  270.  
  271.     Loop
  272.     'End of Update
  273.     
  274.     .stb.Panels(2).Text = "Upload Completed"
  275.     DoEvents
  276.     .lstResults.AddItem "Upload completed"
  277.     Unload pgb
  278. End With
  279. Exit Function
  280.  
  281. transfer_Error:
  282. MsgBox (Err.Description)
  283. End Function
  284. Public Function DoUpdate(sSQL As String) As Boolean
  285. On Error GoTo Update_Error
  286.  
  287. Set TargetQuery = Nothing
  288.     With TargetQuery
  289.         .Name = "GetRowsQuery"
  290.         .SQL = sSQL
  291.         .RowsetSize = 1
  292.         Set .ActiveConnection = TargetCon
  293.         Set TargetResult = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  294.     End With
  295.     'TargetResult.Requery 'Get all the records from the Sourcefile
  296.     
  297.     DoEvents
  298.     Exit Function
  299. Update_Error:
  300.     MsgBox (Err.Description)
  301. End Function
  302. Public Function CreateSQL(ResultSetX As rdoResultset, BuffX As Variant, iRowNumber As Integer, sTargetFile As String) As String
  303. 'This function will check the fields types , and then convert the data to the correct field formats
  304. Dim sSQL As String
  305. Dim iFields As Integer
  306. Dim ii As Integer
  307. Dim check
  308.  
  309. sSQL = "Insert into " & sTargetFile & " Values("
  310.  
  311.  
  312. 'Loop throug the fields to determine the field type
  313.  iFields = ResultSetX.rdoColumns.Count
  314.  For ii = 0 To iFields - 1
  315.     Select Case ResultSetX.rdoColumns(ii).Type
  316.     
  317.     Case rdTypeCHAR, rdTypeVARCHAR, rdTypeLONGVARCHAR 'Strings
  318.         If IsNull(BuffX(ii, iRowNumber)) Then
  319.             sSQL = sSQL & "" & "Null" & ","
  320.         Else
  321.             sSQL = sSQL & "'" & BuffX(ii, iRowNumber) & "',"
  322.         End If
  323.     
  324.     Case rdTypeDATE, rdTypeTIMESTAMP
  325.  
  326.         sSQL = sSQL & "'" & ConvertDate(BuffX(ii, iRowNumber)) & "',"
  327.     
  328.     Case Else
  329.     'If IsNumeric(BuffX(ii, iRowNumber)) Then
  330.         sSQL = sSQL & BuffX(ii, iRowNumber) & ","
  331.     'Else
  332.         'sSQL = sSQL & "'" & BuffX(ii, iRowNumber) & "',"
  333.     'End If
  334.     End Select
  335.  
  336. Next ii
  337.  
  338. sSQL = Left(sSQL, Len(sSQL) - 1)
  339.  
  340. sSQL = sSQL & ")"
  341.  
  342. CreateSQL = sSQL
  343. End Function
  344. Public Function SourceInitRDO(sConnectString, FormX As Form, ListX As ListBox) As Boolean
  345. ' This routine will initialize the RDO environment
  346. SourceInitRDO = True
  347. On Error GoTo mERROR
  348. With FormX
  349.  
  350.     ' Now Connect to the RDO database
  351.     .stb.Panels(2).Text = "Initializing...."
  352.     .lstResults.AddItem "Initialising Source Environment"
  353.     Set SourceEnv = rdoEnvironments(0)
  354.     
  355.     Set SourceCon = SourceEnv.OpenConnection(dsName:=sConnectString, _
  356.          Prompt:=rdDriverCompleteRequired)
  357.          
  358.     .lstResults.AddItem "Attempting connection to " & sConnectString
  359.          
  360.     While SourceCon.StillConnecting
  361.         .stb.Panels(2).Text = "Busy connecting to Source Database"
  362.         DoEvents
  363.     Wend
  364.     
  365.     .stb.Panels(2).Text = "Connected To Source"
  366.     .lstResults.AddItem "Connected  to " & sConnectString
  367.  
  368. End With
  369.  
  370. Call ShowTest(FormX, ListX, SourceCon)
  371.  
  372. Exit Function
  373.  
  374. mERROR:
  375. SourceInitRDO = False
  376. End Function
  377. Public Function TargetInitRDO(sConnectString, FormX As Form, ListX As ListBox) As Boolean
  378. ' This routine will initialize the RDO environment
  379. TargetInitRDO = True
  380. On Error GoTo mERROR
  381. With FormX
  382.  
  383.     ' Now Connect to the RDO database
  384.     .stb.Panels(2).Text = "Initializing...."
  385.     .lstResults.AddItem "Initialising Target Environment"
  386.     Set TargetEnv = rdoEnvironments(0)
  387.     
  388.     Set TargetCon = TargetEnv.OpenConnection(dsName:=sConnectString, _
  389.          Prompt:=rdDriverCompleteRequired)
  390.          
  391.     .lstResults.AddItem "Attempting connection to " & sConnectString
  392.          
  393.     While TargetCon.StillConnecting
  394.         .stb.Panels(2).Text = "Busy connecting to Target Database"
  395.         DoEvents
  396.     Wend
  397.     
  398.     .stb.Panels(2).Text = "Connected To Target"
  399.     .lstResults.AddItem "Connected  to " & sConnectString
  400.  
  401. End With
  402.  
  403. Call ShowTest(FormX, ListX, TargetCon)
  404.  
  405. Exit Function
  406.  
  407. mERROR:
  408. TargetInitRDO = False
  409. End Function
  410. Public Function ShowTest(FormX As Form, ListX As ListBox, rdoConnVar As rdoConnection)
  411. Dim ii As Integer
  412.  
  413. For ii = 0 To rdoConnVar.rdoTables.Count - 1
  414.     ListX.AddItem rdoConnVar.rdoTables(ii).Name
  415.     DoEvents
  416. Next ii
  417. DoEvents
  418.  
  419. End Function
  420.  
  421. Public Function getResults(sSqlString As String) As Variant
  422.  
  423. Dim TotalRecords
  424. Dim Printline As String
  425. Dim NewKey As String
  426. Dim FileName
  427. Dim iFields As Integer
  428.  
  429. With frmMain
  430.     DoEvents
  431.     TotalRecords = 0
  432.     Load pgb
  433.     pgb.Show
  434.     frmMain.Caption = "Submitting SQL to database"
  435.     DoEvents
  436.     .stb.Panels(2).Text = "Setting up the Data Environment"
  437.     
  438.     
  439.     
  440.     ' Set up the Environment to execute
  441.     Set qy = Nothing
  442.     With qy
  443.         .Name = "GetRowsQuery"
  444.         .SQL = sSqlString
  445.         .RowsetSize = 1
  446.         Set .ActiveConnection = cn
  447.         Set rs = .OpenResultset(rdOpenKeyset, rdConcurRowVer)
  448.     End With
  449.     
  450.     .stb.Panels(2).Text = "Submitting the querry"
  451.     
  452.     ' Now Execute the SQL statement and get the Records
  453.     ' We need to loop for every 50 lines
  454.     
  455.     rs.Requery
  456.     DoEvents
  457.     
  458.     'Do Until rs.EOF
  459.     .stb.Panels(2).Text = "Retreiving the Recordsets"
  460.         RowBuf = rs.GetRows(10)      'Get the next 6000 rows
  461.         RowsReturned = UBound(RowBuf, 2) + 1
  462.     
  463.         For i = 0 To RowsReturned - 1
  464.             Dummy = pgb.Progress(i, RowsReturned - 1)
  465.             .stb.Panels(2).Text = "Processing for : " & RowBuf(0, i)
  466.             DoEvents
  467.             
  468.             'Now , create the new record
  469.             'NewKey = RowBuf(0, i)
  470.             'NewKey = EnCrypt(NewKey, 10)
  471.             'Printline = NewKey & "," & RowBuf(1, i) & "," & RowBuf(2, i)
  472.             'Print #1, Printline ' Write the record to the text file
  473.     
  474.         Next i
  475.     getResults = RowBuf
  476.     TotalRecords = TotalRecords + RowsReturned
  477.     
  478.     iFields = rs.rdoColumns.Count
  479.  
  480.    For ii = 0 To iFields - 1
  481.         MsgBox (rs.rdoColumns(ii).Name)
  482.    Next ii
  483.     
  484.     'Loop
  485.     .stb.Panels(2).Text = "Retreiving the Recordsets"
  486.     .stb.Panels(2).Text = "Total Records Retreived : " & TotalRecords
  487.     Unload pgb
  488. End With
  489. End Function
  490.  
  491.