Vous êtes sur la page 1sur 2

21/5/2018 Ado - Verificar si una tabla existe

Texto planoImprimir

1. Option Explicit
2.
3. '***************************************************************************
4. ' Ejemplo para verificar si una tabla se encuentra en la base de datos _
5. Nota: Agregar la Referencia a -->> Microsoft Activex Data Objects
6. '***************************************************************************
7.
8. Private Function Tabla_Existe(ByVal Path_BD As String, _
9. ByVal La_Tabla As String) As Boolean
10.
11.
12. On Error GoTo Err_Sub
13.
14. Const Cadena As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
15.
16. Dim datos() As String
17. Dim i As Integer
18. Dim cnn As Connection
19.
20. 'Nueva conexión Ado
21. Set cnn = New Connection
22.
23. 'Abre la base de datos
24. cnn.Open Cadena & Path_BD
25.
26. 'Declara y abre el recordset
27. Dim RS As Recordset
28. Set RS = cnn.OpenSchema(adSchemaTables)
29.
30. datos = Split(RS.GetString, vbTab)
31.
32.
33. For i = 0 To UBound(datos)
34. 'Compara el valor,( la tabla )
35. If Trim(UCase(datos(i))) = Trim(UCase(La_Tabla)) Then
36. Tabla_Existe = True ' La tabla existe
37. Exit For
38. End If
39. Next
40.
41. 'cierra el recordset y la base de datos
42. On Error Resume Next
43. RS.Close
44. Set RS = Nothing
45. cnn.Close
46. Set cnn = Nothing
47. Exit Function
48. 'error
49. Err_Sub:
50. MsgBox Err.Description, vbCritical
51. On Error Resume Next
52. RS.Close
53. Set RS = Nothing
54. cnn.Close
55. Set cnn = Nothing

http://www.recursosvisualbasic.com.ar/htm/trucos-codigofuente-visual-basic/281-verificar-si-tabla-existe-con-ado.htm# 1/2
21/5/2018 Ado - Verificar si una tabla existe

56. End Function


57.
58. Private Sub Command1_Click()
59. Dim ret As Boolean
60. Dim Path As String
61.
62. ' Path de la base de datos Biblio.mdb de visual basic
63. Path = "C:\Archivos de programa\Microsoft Visual Studio\VB98\biblio.mdb"
64.
65. 'verifica si la tabla Authors existe
66. ret = Tabla_Existe(Path, "Authors")
67.
68. If ret Then
69. MsgBox "La tabla existe", vbInformation 'Si
70. Else
71. MsgBox "La tabla NO existe", vbInformation 'No
72. End If
73.
74.
75. End Sub
76.
77. Private Sub Form_Load()
78. Command1.Caption = " Verificar tabla "
79. End Sub

http://www.recursosvisualbasic.com.ar/htm/trucos-codigofuente-visual-basic/281-verificar-si-tabla-existe-con-ado.htm# 2/2

Vous aimerez peut-être aussi