code_junkie

exporting a tab delimited file from an access database

Nov 12th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. db = "C:Dokumente und EinstellungenhomAnwendungsdatenBayWotch4Neuer Ordnerbaywotch.db5"
  2. TextExportFile = "C:Dokumente und EinstellungenhomAnwendungsdatenBayWotch4Neuer OrdnerExp.txt"
  3.  
  4. Set cn = CreateObject("ADODB.Connection")
  5. Set rs = CreateObject("ADODB.Recordset")
  6.  
  7. cn.Open _
  8. "Provider = Microsoft.Jet.OLEDB.4.0; " & _
  9. "Data Source =" & db
  10.  
  11. strSQL = "SELECT * FROM tblAuction1"
  12.  
  13. rs.Open strSQL, cn, 3, 3
  14.  
  15. Set fs = CreateObject("Scripting.FileSystemObject")
  16.  
  17. Set f = fs.CreateTextFile(TextExportFile, True, True)
  18.  
  19. a = rs.GetString
  20.  
  21. f.WriteLine a
  22.  
  23. f.Close
  24.  
  25. Const Access = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:Dokumente und EinstellungenhomAnwendungsdatenBayWotch4Neuer Ordnerbaywotch.db5"
  26. Const SQLServer = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=.SQLEXPRESS"
  27. Dim arrFields
  28.  
  29. Set SQLConn = CreateObject("ADODB.Connection")
  30. WITH SQLConn
  31. .ConnectionString = SQLServer
  32. .Open
  33. End WITH
  34. Set AccessConn = CreateObject("ADODB.Connection")
  35. WITH AccessConn
  36. .ConnectionString = Access
  37. .Open
  38. End WITH
  39. Set SQLRS = CreateObject("ADODB.Recordset")
  40. WITH SQLRS
  41. .CursorType = 3
  42. .LockType = 3
  43. End WITH
  44. Set AccessRS = CreateObject("ADODB.Recordset")
  45. WITH AccessRS
  46. .ActiveConnection = AccessConn
  47. .CursorType = 3
  48. .LockType = 3
  49. End WITH
  50.  
  51. strSQL = "SELECT * FROM tblAuction1"
  52. AccessRS.Open strSQL
  53.  
  54. If AccessRS.RecordCount <> 0 Then
  55. AccessRS.MoveFirst
  56. ReDim arrFields(AccessRS.Fields.Count)
  57. Do Until AccessRS.BOF OR AccessRS.EOF
  58. For i = 0 To AccessRS.Fields.Count - 1
  59. If AccessRS.Fields(i).Type = 202 Then
  60. arrFields(i) = Chr(39) & AccessRS.Fields(i).Value & Chr(39)
  61. Else
  62. arrFields(i) = AccessRS.Fields(i).Value
  63. End If
  64. Next
  65. strSQL1 = "INSERT INTO {Table in mySQL} VALUES("
  66. For j = 1 To UBound(arrFields) - 2
  67. strSQL1 = strSQL1 & arrFields(j) & ","
  68. Next
  69. strSQL1 = strSQL1 & arrFields(UBound(arrFields) - 1) & ")"
  70. SQLRS = SQLConn.Execute(strSQL1)
  71. AccessRS.MoveNext
  72. Loop
  73. Else
  74. MsgBox "No records found"
  75. End If
Add Comment
Please, Sign In to add comment