Posted By

corydeppen on 10/10/07


Tagged

access vba 2003 vb


Versions (?)

Run SQL Script From External File


 / Published in: Visual Basic
 

  1. ' Runs a SQL script from an external file.
  2. '
  3. ' @date 20071010
  4. ' @param FilePath Path to SQL script file
  5. ' @remarks http://www.thescripts.com/forum/thread195269.html
  6.  
  7. Sub ExecuteSqlScript(FilePath As String)
  8.  
  9. Dim Script As String
  10. Dim FileNumber As Integer
  11. Dim Delimiter As String
  12. Dim aSubscript() As String
  13. Dim Subscript As String
  14. Dim i As Long
  15.  
  16. Delimiter = ";"
  17. FileNumber = FreeFile
  18. Script = String(FileLen(FilePath), vbNullChar)
  19.  
  20. ' Grab the scripts inside the file
  21. Open FilePath For Binary As #FileNumber
  22. Get #FileNumber, , Script
  23. Close #FileNumber
  24.  
  25. ' Put the scripts into an array
  26. aSubscript = Split(Script, Delimiter)
  27.  
  28. ' Run each script in the array
  29. For i = 0 To UBound(aSubscript) - 1
  30. aSubscript(i) = Trim(aSubscript(i))
  31. Subscript = aSubscript(i)
  32. CurrentProject.Connection.Execute Subscript
  33.  
  34. Next i
  35.  
  36. End Sub

Report this snippet  

You need to login to post a comment.