Guest User

Untitled

a guest
Nov 17th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.06 KB | None | 0 0
  1. Option Explicit
  2. Option Compare Database
  3. Function SaveToFile() 'Save the code for all modules to files in currentDatabaseDirCode
  4.  
  5. Dim Name As String
  6. Dim WasOpen As Boolean
  7. Dim Last As Integer
  8. Dim I As Integer
  9. Dim TopDir As String, Path As String, FileName As String
  10. Dim F As Long 'File for saving code
  11. Dim LineCount As Long 'Line count of current module
  12.  
  13. I = InStrRev(CurrentDb.Name, "")
  14. TopDir = VBA.Left(CurrentDb.Name, I - 1)
  15. Path = TopDir & "" & "Code" 'Path where the files will be written
  16.  
  17. If (Dir(Path, vbDirectory) = "") Then
  18. MkDir Path 'Ensure this exists
  19. End If
  20.  
  21. '--- SAVE THE STANDARD MODULES CODE ---
  22.  
  23. Last = Application.CurrentProject.AllModules.Count - 1
  24.  
  25. For I = 0 To Last
  26. Name = CurrentProject.AllModules(I).Name
  27. WasOpen = True 'Assume already open
  28.  
  29. If Not CurrentProject.AllModules(I).IsLoaded Then
  30. WasOpen = False 'Not currently open
  31. DoCmd.OpenModule Name 'So open it
  32. End If
  33.  
  34. LineCount = Access.Modules(Name).CountOfLines
  35. FileName = Path & "" & Name & ".vba"
  36.  
  37. If (Dir(FileName) <> "") Then
  38. Kill FileName 'Delete previous version
  39. End If
  40.  
  41. 'Save current version
  42. F = FreeFile
  43. Open FileName For Output Access Write As #F
  44. Print #F, Access.Modules(Name).Lines(1, LineCount)
  45. Close #F
  46.  
  47. If Not WasOpen Then
  48. DoCmd.Close acModule, Name 'It wasn't open, so close it again
  49. End If
  50. Next
  51.  
  52. '--- SAVE FORMS MODULES CODE ---
  53.  
  54. Last = Application.CurrentProject.AllForms.Count - 1
  55.  
  56. For I = 0 To Last
  57. Name = CurrentProject.AllForms(I).Name
  58. WasOpen = True
  59.  
  60. If Not CurrentProject.AllForms(I).IsLoaded Then
  61. WasOpen = False
  62. DoCmd.OpenForm Name, acDesign
  63. End If
  64.  
  65. LineCount = Access.Forms(Name).Module.CountOfLines
  66. FileName = Path & "" & Name & ".vba"
  67.  
  68. If (Dir(FileName) <> "") Then
  69. Kill FileName
  70. End If
  71.  
  72. F = FreeFile
  73. Open FileName For Output Access Write As #F
  74. Print #F, Access.Forms(Name).Module.Lines(1, LineCount)
  75. Close #F
  76.  
  77. If Not WasOpen Then
  78. DoCmd.Close acForm, Name
  79. End If
  80. Next
  81. MsgBox "Created source files in " & Path
  82. End Function
  83.  
  84. Private Sub exportToFile(db_path As String, db_id As String, loginInfo As AuthInfoz, errFile As Variant)
  85.  
  86. Dim pdbeNew As PrivDBEngine
  87. Dim db As DAO.Database
  88. Dim ws As DAO.Workspace
  89. Dim rst As DAO.Recordset
  90.  
  91. Dim cn As ADODB.Connection ' ADODB.Connection
  92. Dim rs As ADODB.Recordset ' ADODB.Recordset
  93. Dim strConnect As String
  94. Dim blnReturn As Boolean
  95.  
  96. Dim Doc As Document
  97. Dim mdl As Module
  98. Dim lngCount As Long
  99. Dim strForm As String
  100. Dim strOneLine As String
  101. Dim sPtr As Integer
  102.  
  103. Set fso = CreateObject("Scripting.FileSystemObject")
  104. Set exportFile = fso.CreateTextFile("E:TicketsCSN1006218vbacode" & db_id & ".txt", ForAppending)
  105.  
  106. ' Export stuff...
  107.  
  108. On Error GoTo errorOut
  109.  
  110. Set pdbeNew = New PrivDBEngine
  111. With pdbeNew
  112. .SystemDB = loginInfo.workgroup
  113. .DefaultUser = loginInfo.username
  114. .DefaultPassword = loginInfo.password
  115. End With
  116.  
  117.  
  118. Set ws = pdbeNew.Workspaces(0)
  119.  
  120.  
  121. Set db = ws.OpenDatabase(db_path)
  122.  
  123. For Each Doc In db.Containers("Modules").Documents
  124. DoCmd.OpenModule Doc.Name
  125. Set mdl = Modules(Doc.Name)
  126.  
  127. exportFile.WriteLine ("---------------------")
  128. exportFile.WriteLine ("Module Name: " & Doc.Name)
  129. exportFile.WriteLine ("Module Type: " & mdl.Type)
  130. exportFile.WriteLine ("---------------------")
  131.  
  132. lngCount = lngCount + mdl.CountOfLines
  133.  
  134. 'For i = 1 To lngCount
  135. ' strOneLine = mdl.Lines(i, 1)
  136. ' exportFile.WriteLine (strOneLine)
  137. 'Next i
  138.  
  139. Set mdl = Nothing
  140. DoCmd.Close acModule, Doc.Name
  141. Next Doc
  142.  
  143. Close_n_exit:
  144.  
  145. If Not (db Is Nothing) Then
  146. Call wk.Close
  147. Set wk = Nothing
  148. Call db.Close
  149. End If
  150.  
  151.  
  152.  
  153. Call exportFile.Close
  154. Set exportFile = Nothing
  155. Set fso = Nothing
  156.  
  157. Exit Sub
  158.  
  159. errorOut:
  160. Debug.Print "----------------"
  161. Debug.Print "BEGIN: Err"
  162. If err.Number <> 0 Then
  163. Msg = "Error # " & Str(err.Number) & " was generated by " _
  164. & err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & err.Description
  165. 'MsgBox Msg, , "Error", err.HelpFile, err.HelpContext
  166. Debug.Print Msg
  167. End If
  168. Resume Close_n_exit
  169.  
  170. End Sub
  171.  
  172. Public Sub ExportAllCode()
  173.  
  174. Dim c As VBComponent
  175. Dim Sfx As String
  176.  
  177. For Each c In Application.VBE.VBProjects(1).VBComponents
  178. Select Case c.Type
  179. Case vbext_ct_ClassModule, vbext_ct_Document
  180. Sfx = ".cls"
  181. Case vbext_ct_MSForm
  182. Sfx = ".frm"
  183. Case vbext_ct_StdModule
  184. Sfx = ".bas"
  185. Case Else
  186. Sfx = ""
  187. End Select
  188.  
  189. If Sfx <> "" Then
  190. c.Export _
  191. Filename:=CurrentProject.Path & "" & _
  192. c.Name & Sfx
  193. End If
  194. Next c
  195.  
  196. End Sub
  197.  
  198. Option Explicit
  199. Option Compare Database
  200.  
  201.  
  202. 'Save the code for all modules to files in currentDatabaseDirCode
  203. Public Function SaveToFile()
  204.  
  205. On Error GoTo SaveToFile_Err
  206.  
  207. Dim Name As String
  208. Dim WasOpen As Boolean
  209. Dim Last As Integer
  210. Dim i As Integer
  211. Dim TopDir As String, Path As String, FileName As String
  212. Dim F As Long 'File for saving code
  213. Dim LineCount As Long 'Line count of current module
  214.  
  215. Dim oApp As New Access.Application
  216.  
  217. ' Open remote database
  218. oApp.OpenCurrentDatabase ("D:AccessmyDatabase.mdb"), False
  219.  
  220.  
  221. i = InStrRev(oApp.CurrentDb.Name, "")
  222. TopDir = VBA.Left(oApp.CurrentDb.Name, i - 1)
  223. Path = TopDir & "" & "Code" 'Path where the files will be written
  224.  
  225. If (Dir(Path, vbDirectory) = "") Then
  226. MkDir Path 'Ensure this exists
  227. End If
  228.  
  229. '--- SAVE THE STANDARD MODULES CODE ---
  230.  
  231. Last = oApp.CurrentProject.AllModules.Count - 1
  232.  
  233. For i = 0 To Last
  234. Name = oApp.CurrentProject.AllModules(i).Name
  235. WasOpen = True 'Assume already open
  236.  
  237. If Not oApp.CurrentProject.AllModules(i).IsLoaded Then
  238. WasOpen = False 'Not currently open
  239. oApp.DoCmd.OpenModule Name 'So open it
  240. End If
  241.  
  242. LineCount = oApp.Modules(Name).CountOfLines
  243. FileName = Path & "" & Name & ".vba"
  244.  
  245. If (Dir(FileName) <> "") Then
  246. Kill FileName 'Delete previous version
  247. End If
  248.  
  249. 'Save current version
  250. F = FreeFile
  251. Open FileName For Output Access Write As #F
  252. Print #F, oApp.Modules(Name).Lines(1, LineCount)
  253. Close #F
  254.  
  255. If Not WasOpen Then
  256. oApp.DoCmd.Close acModule, Name 'It wasn't open, so close it again
  257. End If
  258. Next
  259.  
  260. '--- SAVE FORMS MODULES CODE ---
  261.  
  262. Last = oApp.CurrentProject.AllForms.Count - 1
  263.  
  264. For i = 0 To Last
  265. Name = oApp.CurrentProject.AllForms(i).Name
  266. WasOpen = True
  267.  
  268. If Not oApp.CurrentProject.AllForms(i).IsLoaded Then
  269. WasOpen = False
  270. oApp.DoCmd.OpenForm Name, acDesign
  271. End If
  272.  
  273. LineCount = oApp.Forms(Name).Module.CountOfLines
  274. FileName = Path & "" & Name & ".vba"
  275.  
  276. If (Dir(FileName) <> "") Then
  277. Kill FileName
  278. End If
  279.  
  280. F = FreeFile
  281. Open FileName For Output Access Write As #F
  282. Print #F, oApp.Forms(Name).Module.Lines(1, LineCount)
  283. Close #F
  284.  
  285. If Not WasOpen Then
  286. oApp.DoCmd.Close acForm, Name
  287. End If
  288. Next
  289.  
  290. '--- SAVE REPORTS MODULES CODE ---
  291.  
  292. Last = oApp.CurrentProject.AllReports.Count - 1
  293.  
  294. For i = 0 To Last
  295. Name = oApp.CurrentProject.AllReports(i).Name
  296. WasOpen = True
  297.  
  298. If Not oApp.CurrentProject.AllReports(i).IsLoaded Then
  299. WasOpen = False
  300. oApp.DoCmd.OpenReport Name, acDesign
  301. End If
  302.  
  303. LineCount = oApp.Reports(Name).Module.CountOfLines
  304. FileName = Path & "" & Name & ".vba"
  305.  
  306. If (Dir(FileName) <> "") Then
  307. Kill FileName
  308. End If
  309.  
  310. F = FreeFile
  311. Open FileName For Output Access Write As #F
  312. Print #F, oApp.Reports(Name).Module.Lines(1, LineCount)
  313. Close #F
  314.  
  315. If Not WasOpen Then
  316. oApp.DoCmd.Close acReport, Name
  317. End If
  318. Next
  319.  
  320. MsgBox "Created source files in " & Path
  321.  
  322. ' Reset the security level
  323. Application.AutomationSecurity = msoAutomationSecurityByUI
  324.  
  325. SaveToFile_Exit:
  326.  
  327. If Not oApp.CurrentDb Is Nothing Then oApp.CloseCurrentDatabase
  328. If Not oApp Is Nothing Then Set oApp = Nothing
  329. Exit function
  330.  
  331. SaveToFile_Err:
  332.  
  333. MsgBox ("Error " & Err.Number & vbCrLf & Err.Description)
  334. Resume SaveToFile_Exit
  335.  
  336. End Function
  337.  
  338. Option Compare Database
  339. Option Explicit
  340.  
  341. Private Const VB_MODULE As Integer = 1
  342. Private Const VB_CLASS As Integer = 2
  343. Private Const VB_FORM As Integer = 100
  344. Private Const EXT_TABLE As String = ".tbl"
  345. Private Const EXT_QUERY As String = ".qry"
  346. Private Const EXT_MODULE As String = ".bas"
  347. Private Const EXT_CLASS As String = ".cls"
  348. Private Const EXT_FORM As String = ".frm"
  349. Private Const CODE_FLD As String = "code"
  350.  
  351. Private Const mblnSave As Boolean = True ' False: just generate the script
  352. '
  353. '
  354.  
  355. Public Sub saveAllAsText()
  356.  
  357. Dim oTable As TableDef
  358. Dim oQuery As QueryDef
  359. Dim oCont As Container
  360. Dim oForm As Document
  361. Dim oModule As Object
  362. Dim FSO As Object
  363.  
  364. Dim strPath As String
  365. Dim strName As String
  366. Dim strFileName As String
  367.  
  368. '**
  369. On Error GoTo errHandler
  370.  
  371. strPath = CurrentProject.path
  372. Set FSO = CreateObject("Scripting.FileSystemObject")
  373. strPath = addFolder(FSO, strPath, Application.CurrentProject.name & "_" & CODE_FLD)
  374. strPath = addFolder(FSO, strPath, Format(Date, "yyyy.mm.dd"))
  375.  
  376.  
  377. For Each oTable In CurrentDb.TableDefs
  378. strName = oTable.name
  379. If left(strName, 4) <> "MSys" Then
  380. strFileName = strPath & "" & strName & EXT_TABLE
  381. If mblnSave Then Application.ExportXML acExportTable, strName, strFileName, strFileName & ".XSD", strFileName & ".XSL", , acUTF8, acEmbedSchema + acExportAllTableAndFieldProperties
  382. Debug.Print "Application.ImportXML """ & strFileName & """, acStructureAndData"
  383. End If
  384. Next
  385.  
  386. For Each oQuery In CurrentDb.QueryDefs
  387. strName = oQuery.name
  388. If left(strName, 1) <> "~" Then
  389. strFileName = strPath & "" & strName & EXT_QUERY
  390. If mblnSave Then Application.SaveAsText acQuery, strName, strFileName
  391. Debug.Print "Application.LoadFromText acQuery, """ & strName & """, """ & strFileName & """"
  392. End If
  393. Next
  394.  
  395. Set oCont = CurrentDb.Containers("Forms")
  396. For Each oForm In oCont.Documents
  397. strName = oForm.name
  398. strFileName = strPath & "" & strName & EXT_FORM
  399. If mblnSave Then Application.SaveAsText acForm, strName, strFileName
  400. Debug.Print "Application.LoadFromText acForm, """ & strName & """, """ & strFileName & """"
  401. Next
  402.  
  403. strPath = addFolder(FSO, strPath, "modules")
  404. For Each oModule In Application.VBE.ActiveVBProject.VBComponents
  405. strName = oModule.name
  406. strFileName = strPath & "" & strName
  407. Select Case oModule.Type
  408. Case VB_MODULE
  409. If mblnSave Then oModule.Export strFileName & EXT_MODULE
  410. Debug.Print "Application.VBE.ActiveVBProject.VBComponents.Import """ & strFileName & EXT_MODULE; """"
  411. Case VB_CLASS
  412. If mblnSave Then oModule.Export strFileName & EXT_CLASS
  413. Debug.Print "Application.VBE.ActiveVBProject.VBComponents.Import """ & strFileName & EXT_CLASS; """"
  414. Case VB_FORM
  415. ' Do not export form modules (already exported the complete forms)
  416. Case Else
  417. Debug.Print "Unknown module type: " & oModule.Type, oModule.name
  418. End Select
  419. Next
  420.  
  421. If mblnSave Then MsgBox "Files saved in " & strPath, vbOKOnly, "Export Complete"
  422.  
  423. Exit Sub
  424.  
  425. errHandler:
  426. MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf
  427. Stop: Resume
  428.  
  429. End Sub
  430. '
  431.  
  432. '
  433. ' Create a folder when necessary. Append the folder name to the given path.
  434. '
  435. Private Function addFolder(ByRef FSO As Object, ByVal strPath As String, ByVal strAdd As String) As String
  436. addFolder = strPath & "" & strAdd
  437. If Not FSO.FolderExists(addFolder) Then MkDir addFolder
  438. End Function
  439. '
  440.  
  441. Public Sub VBAExportModule()
  442. On Error GoTo Errg
  443. Dim rs As DAO.Recordset
  444. Set rs = CurrentDb.OpenRecordset("SELECT MSysObjects.Name FROM MSysObjects WHERE Type=-32761", dbOpenDynaset, dbSeeChanges)
  445.  
  446. Do Until rs.EOF
  447. Application.SaveAsText acModule, rs("Name"), "C:" & rs("Name") & ".txt"
  448. rs.MoveNext
  449. Loop
  450.  
  451. Cleanup:
  452. If Not rs Is Nothing Then rs.Close
  453. Set rs = Nothing
  454. Exit Sub
  455. Errg:
  456. GoTo Cleanup
  457. End Sub
Add Comment
Please, Sign In to add comment