Constancia

VB_CLS_ACCESS

Oct 9th, 2014
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 14.17 KB | None | 0 0
  1. 'Imports Microsoft.Office.Interop
  2.  
  3. Public Class Access
  4.  
  5.     'Private oAccess As Microsoft.Office.Interop.Access.Application
  6.     'Public BD As Microsoft.Office.Interop.Access.Dao.Database
  7.  
  8.     Private oAccess As Object
  9.     Public BD As Object
  10.  
  11.  
  12.     Public Sub New(StrRutaBD As String, StrBD As String, Optional esVisible As Boolean = True)
  13.  
  14.         _sRutaBD = StrRutaBD : _sBD = StrBD
  15.         _bVisible = esVisible
  16.  
  17.         'oAccess = New Microsoft.Office.Interop.Access.Application
  18.         oAccess = CreateObject("Access.Application")
  19.  
  20.  
  21.         'If StrRutaBD <> "" Then
  22.         '    oAccess.OpenCurrentDatabase(sRutaBD & sBD, False)
  23.         'End If
  24.  
  25.         'Run the macro.
  26.         'oAccess.Run("ImportTxtFile")
  27.  
  28.         'Quit Access without saving the database.
  29.  
  30.         '  oAccess.CurrentDb.Close()
  31.         ' oAccess = Nothing
  32.  
  33.     End Sub
  34.  
  35.  
  36. #Region "PROPIEDADES"
  37.  
  38.     Private _sError As String
  39.     Public Property sError() As String
  40.         Get
  41.             Return _sError
  42.         End Get
  43.         Set(ByVal value As String)
  44.             _sError = value
  45.         End Set
  46.     End Property
  47.  
  48.     Private _bVisible As Boolean
  49.     Public Property bVisible() As Boolean
  50.         Get
  51.             Return _bVisible
  52.         End Get
  53.         Set(ByVal value As Boolean)
  54.             _bVisible = value
  55.         End Set
  56.     End Property
  57.  
  58.     Private _sRutaBD As String
  59.     Public Property sRutaBD() As String
  60.         Get
  61.             Return _sRutaBD
  62.         End Get
  63.         Set(ByVal value As String)
  64.             _sRutaBD = value
  65.         End Set
  66.     End Property
  67.  
  68.     Private _sBD As String
  69.     Public Property sBD() As String
  70.         Get
  71.             Return _sBD
  72.         End Get
  73.         Set(ByVal value As String)
  74.             _sBD = value
  75.         End Set
  76.     End Property
  77.  
  78.     Private _Sql As String
  79.     Public Property Sql() As String
  80.         Get
  81.             Return _Sql
  82.         End Get
  83.         Set(ByVal value As String)
  84.             _Sql = value
  85.         End Set
  86.     End Property
  87.  
  88.     Private _Macro As String
  89.     Public Property Macro() As String
  90.         Get
  91.             Return _Macro
  92.         End Get
  93.         Set(ByVal value As String)
  94.             _Macro = value
  95.         End Set
  96.     End Property
  97.  
  98. #End Region
  99.  
  100.  
  101. #Region "MÉTODOS PÚBLICOS"
  102.  
  103.  
  104.     Public Sub Abrir()
  105.         oAccess.OpenCurrentDatabase(_sRutaBD & _sBD, False, "Jorge")
  106.         BD = oAccess.CurrentDb
  107.         oAccess.Application.Visible = _bVisible
  108.         oAccess.DoCmd.Minimize()
  109.     End Sub
  110.  
  111.     Public Sub Cerrar()
  112.         BD.Close() : BD = Nothing
  113.         ''oAccess.Application.Quit()
  114.         'oAccess = Nothing
  115.  
  116.         oAccess.CloseCurrentDatabase()
  117.         oAccess.Application.Quit()
  118.  
  119.     End Sub
  120.  
  121.     Public Sub Matar()
  122.         oAccess.Application.Quit()
  123.     End Sub
  124.  
  125.     'Public Sub CompactaBD(RutaBD As String, NomBD As String)
  126.     'Public Sub CompactaBD()
  127.  
  128.     '    'oAccess.CompactRepair(sRutaBD & "aaa.mbd", sRutaBD & sBD)
  129.  
  130.     '    Dim jro As JRO.JetEngine
  131.  
  132.     '    jro = New JRO.JetEngine()
  133.  
  134.     '    jro.CompactDatabase("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sRutaBD & sBD, _
  135.     '    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sRutaBD & "aaa.mdb;Jet OLEDB:Engine Type=5")
  136.  
  137.     'End Sub
  138.  
  139.     Public Sub EjecutaSQL()
  140.         On Error GoTo Trata_Error
  141.  
  142.         oAccess.DoCmd.RunSQL(_Sql)
  143.  
  144.         Exit Sub
  145. Trata_Error:
  146.         _sError = "oAccess.EjecutaSQL : " & Err.Description
  147.     End Sub
  148.  
  149.     Public Sub EjecutaMacro()
  150.         On Error GoTo Trata_Error
  151.  
  152.         oAccess.DoCmd.RunMacro(_Macro)
  153.  
  154.         Exit Sub
  155. Trata_Error:
  156.         _sError = "oAccess.EjecutaMacro : " & Err.Description
  157.     End Sub
  158.  
  159.  
  160.     Public Sub ModificaVista(Vista As String, sQuery As String)
  161.  
  162.         'Between Date()-1 And Date()
  163.         'Between(#7/15/2013# And #7/16/2013#)
  164.  
  165.         'sQuery = Replace(sQuery, "Between Date()-1 And Date()", "Between #7/15/2013# And #7/16/2013#")
  166.  
  167.         BD.QueryDefs(Vista).SQL = sQuery
  168.  
  169.     End Sub
  170.  
  171.     Public Function DameMaxFecha(StrTabla As String, StrCampo As String) As String
  172.  
  173.         On Error GoTo TrataError
  174.  
  175.         'Dim StrRes As String, Rs As Dao.Recordset
  176.         Dim StrRes As String, Rs As Object
  177.  
  178.         _Sql = "SELECT MAX(" & StrCampo & ") FROM " & StrTabla
  179.  
  180.         Rs = oAccess.CurrentDb.OpenRecordset(_Sql)
  181.  
  182.         If IsDBNull(Rs(0)) Then StrRes = "" Else StrRes = CStr(Rs(0).Value)
  183.  
  184.         DameMaxFecha = StrRes
  185.  
  186.         Rs.Close() : Rs = Nothing
  187.  
  188.         Exit Function
  189. TrataError:
  190.         DameMaxFecha = "DameMaxFecha() : " & Err.Description
  191.     End Function
  192.  
  193.     ''' <summary>
  194.     ''' Exportación de Access a Txt/csv Mediante DoCmd.TransferText
  195.     ''' </summary>
  196.     ''' <param name="sVista">Vista de Origen y Especificación Access de Exportación</param>
  197.     ''' <param name="RutaFic"></param>
  198.     ''' <param name="Tipo">Txt / csv</param>
  199.     ''' <returns></returns>
  200.     ''' <remarks></remarks>
  201.     Public Function ExportaTxt(sVista As String, RutaFic As String, Optional Tipo As String = "txt") As String
  202.  
  203.         ExportaTxt = ""
  204.         On Error GoTo TrataError
  205.  
  206.         ' .TransferText(TransferType (1-Import;2-Export), SpecificationName, TableName, FileName, HasFieldNames, HTMLTableName, CodePage)
  207.         oAccess.DoCmd.TransferText(2, sVista, sVista, RutaFic, True)
  208.  
  209.         Exit Function
  210. TrataError:
  211.         ExportaTxt = "ERROR : Access.ExportaTxt " & Err.Description
  212.     End Function
  213.  
  214.     Public Sub CamposTabla(sTabla As String)
  215.  
  216.         'Dim oTabla As Dao.TableDef, oCampo As Dao.Field
  217.         Dim oTabla As Object, oCampo As Object
  218.  
  219.         oTabla = oAccess.CurrentDb.TableDefs(sTabla)
  220.  
  221.         Debug.Print("TABLA : " & sTabla)
  222.  
  223.         For i = 0 To oTabla.Fields.Count - 1
  224.             oCampo = oTabla.Fields(i)
  225.             Debug.Print(oCampo.Name & " : " & TipoCampo(oCampo))
  226.         Next
  227.  
  228.         'For i = 0 To oAccess.CurrentDb.TableDefs.Count - 1
  229.         '    Debug.Print("TABLA : " & oAccess.CurrentDb.TableDefs(i).Name)
  230.         '    For x = 0 To oAccess.CurrentDb.TableDefs(i).Fields.Count - 1
  231.         '        Debug.Print(oAccess.CurrentDb.TableDefs(i).Fields(x).Name & " : " & TipoCampo(oAccess.CurrentDb.TableDefs(i).Fields(x)))
  232.         '        'For z = 0 To oAccess.CurrentDb.TableDefs(i).Fields(x).Properties.Count - 1
  233.         '        '    Debug.Print(oAccess.CurrentDb.TableDefs(i).Fields(x).Properties(z).Name) '& " : " & oAccess.CurrentDb.TableDefs(i).Fields(x).Properties(z).value)
  234.         '        'Next
  235.         '    Next
  236.         '    Debug.Print("***********************************")
  237.         'Next
  238.  
  239.     End Sub
  240.  
  241.     'Private Function TipoCampo(Campo As Dao.Field) As String
  242.     Private Function TipoCampo(Campo As Object) As String
  243.  
  244.         Dim StrTipo As String
  245.  
  246.         Select Case CLng(Campo.Type)
  247.             Case 1 : StrTipo = "Yes/No"
  248.             Case 2 : StrTipo = "Byte"
  249.             Case 3 : StrTipo = "Integer"
  250.             Case 4 : StrTipo = "AutoNumber"
  251.                 'If (Campo.Attributes And dbAutoIncrField) = 0& Then
  252.                 '    StrTipo = "Long Integer"
  253.                 'Else
  254.                 '    StrTipo = "AutoNumber"
  255.                 'End If
  256.             Case 5 : StrTipo = "Currency"         '
  257.             Case 6 : StrTipo = "Single"
  258.             Case 7 : StrTipo = "Double"
  259.             Case 8 : StrTipo = "Date/Time"
  260.             Case 9 : StrTipo = "Binary"
  261.             Case 10 : StrTipo = "Text"
  262.                 'If (Campo.Attributes And dbFixedField) = 0& Then
  263.                 '    StrTipo = "Text"
  264.                 'Else
  265.                 '    StrTipo = "Text (fixed width)"        '(no interface)
  266.                 'End If
  267.             Case 11 : StrTipo = "OLE Object"
  268.             Case 12 : StrTipo = "Memo"
  269.                 'If (Campo.Attributes And dbHyperlinkField) = 0& Then
  270.                 '    StrTipo = "Memo"
  271.                 'Else
  272.                 '    StrTipo = "Hyperlink"
  273.                 'End If
  274.             Case 15 : StrTipo = "GUID"                 '15
  275.  
  276.                 'Attached tables only: cannot create these in JET.
  277.             Case 16 : StrTipo = "Big Integer"
  278.             Case 17 : StrTipo = "VarBinary"
  279.             Case 18 : StrTipo = "Char"
  280.             Case 19 : StrTipo = "Numeric"
  281.             Case 20 : StrTipo = "Decimal"
  282.             Case 21 : StrTipo = "Float"
  283.             Case 22 : StrTipo = "Time"
  284.             Case 23 : StrTipo = "Time Stamp"
  285.  
  286.                 'Constants for complex types don't work prior to Access 2007 and later.
  287.             Case 101& : StrTipo = "Attachment"
  288.             Case 102& : StrTipo = "Complex Byte"
  289.             Case 103& : StrTipo = "Complex Integer"
  290.             Case 104& : StrTipo = "Complex Long"
  291.             Case 105& : StrTipo = "Complex Single"
  292.             Case 106& : StrTipo = "Complex Double"
  293.             Case 107& : StrTipo = "Complex GUID"
  294.             Case 108& : StrTipo = "Complex Decimal"
  295.             Case 109& : StrTipo = "Complex Text"
  296.             Case Else : StrTipo = "Desconocido"
  297.         End Select
  298.  
  299.         TipoCampo = StrTipo
  300.  
  301.     End Function
  302.  
  303. #End Region
  304.  
  305. End Class
  306.  
  307.  
  308.  
  309.  
  310. #Region "REFERENCIA"
  311.  
  312.  
  313. 'http://msdn.microsoft.com/es-es/library/office/dn142571%28v=office.15%29.aspx
  314.  
  315. '************* DoCmd.TransferText
  316. 'http://msdn.microsoft.com/es-es/library/office/ff835958%28v=office.15%29.aspx
  317.  
  318. '************* DoCmd.TransferSpreadsheet
  319. 'http://msdn.microsoft.com/es-es/library/office/ff844793%28v=office.15%29.aspx
  320.  
  321.  
  322. #End Region
  323.  
  324. #Region "CÓDIGO FUENTE"
  325.  
  326. 'Private Sub PropiedadesBD()
  327.  
  328. 'For Each prpLoop In BD.Properties
  329. '    With prpLoop
  330. '        Debug.Print(" " & .Name)
  331. '        Debug.Print(" Type: " & .Type)
  332. '        Try
  333. '            Debug.Print(" Value: " & .Value)
  334. '        Catch ex As Exception
  335. '            Debug.Print(" Value: ERROR")
  336. '        End Try
  337.  
  338. '        Debug.Print(" Inherited: " & .Inherited)
  339. '        Debug.Print("***************************************************************")
  340. '    End With
  341. 'Next prpLoop
  342.  
  343. 'End Sub
  344.  
  345.  
  346. 'Function TableInfo(strTableName As String)
  347. '    On Error GoTo TableInfoErr
  348. '    ' Purpose:   Display the field names, types, sizes and descriptions for a table.
  349. '    ' Argument:  Name of a table in the current database.
  350. '    Dim db As DAO.Database
  351. '    Dim tdf As DAO.TableDef
  352. '    Dim fld As DAO.Field
  353.  
  354. '    db = CurrentDb()
  355. '    tdf = db.TableDefs(strTableName)
  356. '    Debug.Print("FIELD NAME", "FIELD TYPE", "SIZE", "DESCRIPTION")
  357. '    Debug.Print("==========", "==========", "====", "===========")
  358.  
  359. '    For Each fld In tdf.Fields
  360. '      Debug.Print fld.Name,
  361. '      Debug.Print FieldTypeName(fld),
  362. '      Debug.Print fld.Size,
  363. '      Debug.Print GetDescrip(fld)
  364. '    Next
  365. '    Debug.Print("==========", "==========", "====", "===========")
  366.  
  367. 'TableInfoExit:
  368. '    db = Nothing
  369. '    Exit Function
  370.  
  371. 'TableInfoErr:
  372. '    Select Case Err
  373. '        Case 3265&  'Table name invalid
  374. '            MsgBox(strTableName & " table doesn't exist")
  375. '        Case Else
  376. '      Debug.Print "TableInfo() Error " & Err & ": " & Error
  377. '    End Select
  378. '    Resume TableInfoExit
  379. 'End Function
  380.  
  381.  
  382. 'Function GetDescrip(obj As Object) As String
  383. '    On Error Resume Next
  384. '    GetDescrip = obj.Properties("Description")
  385. 'End Function
  386.  
  387.  
  388. 'Function FieldTypeName(fld As DAO.Field) As String
  389. '    'Purpose: Converts the numeric results of DAO Field.Type to text.
  390. '    Dim strReturn As String    'Name to return
  391.  
  392. '    Select Case CLng(fld.Type) 'fld.Type is Integer, but constants are Long.
  393. '        Case dbBoolean : strReturn = "Yes/No"            ' 1
  394. '        Case dbByte : strReturn = "Byte"                 ' 2
  395. '        Case dbInteger : strReturn = "Integer"           ' 3
  396. '        Case dbLong                                     ' 4
  397. '            If (fld.Attributes And dbAutoIncrField) = 0& Then
  398. '                strReturn = "Long Integer"
  399. '            Else
  400. '                strReturn = "AutoNumber"
  401. '            End If
  402. '        Case dbCurrency : strReturn = "Currency"         ' 5
  403. '        Case dbSingle : strReturn = "Single"             ' 6
  404. '        Case dbDouble : strReturn = "Double"             ' 7
  405. '        Case dbDate : strReturn = "Date/Time"            ' 8
  406. '        Case dbBinary : strReturn = "Binary"             ' 9 (no interface)
  407. '        Case dbText                                     '10
  408. '            If (fld.Attributes And dbFixedField) = 0& Then
  409. '                strReturn = "Text"
  410. '            Else
  411. '                strReturn = "Text (fixed width)"        '(no interface)
  412. '            End If
  413. '        Case dbLongBinary : strReturn = "OLE Object"     '11
  414. '        Case dbMemo                                     '12
  415. '            If (fld.Attributes And dbHyperlinkField) = 0& Then
  416. '                strReturn = "Memo"
  417. '            Else
  418. '                strReturn = "Hyperlink"
  419. '            End If
  420. '        Case dbGUID : strReturn = "GUID"                 '15
  421.  
  422. '            'Attached tables only: cannot create these in JET.
  423. '        Case dbBigInt : strReturn = "Big Integer"        '16
  424. '        Case dbVarBinary : strReturn = "VarBinary"       '17
  425. '        Case dbChar : strReturn = "Char"                 '18
  426. '        Case dbNumeric : strReturn = "Numeric"           '19
  427. '        Case dbDecimal : strReturn = "Decimal"           '20
  428. '        Case dbFloat : strReturn = "Float"               '21
  429. '        Case dbTime : strReturn = "Time"                 '22
  430. '        Case dbTimeStamp : strReturn = "Time Stamp"      '23
  431.  
  432. '            'Constants for complex types don't work prior to Access 2007 and later.
  433. '        Case 101& : strReturn = "Attachment"         'dbAttachment
  434. '        Case 102& : strReturn = "Complex Byte"       'dbComplexByte
  435. '        Case 103& : strReturn = "Complex Integer"    'dbComplexInteger
  436. '        Case 104& : strReturn = "Complex Long"       'dbComplexLong
  437. '        Case 105& : strReturn = "Complex Single"     'dbComplexSingle
  438. '        Case 106& : strReturn = "Complex Double"     'dbComplexDouble
  439. '        Case 107& : strReturn = "Complex GUID"       'dbComplexGUID
  440. '        Case 108& : strReturn = "Complex Decimal"    'dbComplexDecimal
  441. '        Case 109& : strReturn = "Complex Text"       'dbComplexText
  442. '        Case Else : strReturn = "Field type " & fld.Type & " unknown"
  443. '    End Select
  444.  
  445. '    FieldTypeName = strReturn
  446. 'End Function
  447.  
  448. #End Region
Advertisement
Add Comment
Please, Sign In to add comment