Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. Option Explicit
  2. Dim dbFileName
  3. Dim curQuery As String
  4.  
  5. Private Sub CommandButton1_Click()
  6. Dim cn As ADODB.Connection
  7. Dim rst As ADODB.Recordset
  8.  
  9. dbFileName = Application.GetOpenFilename _
  10. ("Access Files (*.accdb), *.accdb")
  11. If dbFileName = False Then Exit Sub
  12.  
  13. Set cn = New ADODB.Connection
  14. cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  15. "Data Source=" & dbFileName & ";Mode=Read"
  16. On Error Resume Next
  17. cn.Open
  18.  
  19. If cn.State = 1 Then
  20. ListBox1.Clear
  21. Set rst = New ADODB.Recordset
  22. Set rst = cn.OpenSchema(adSchemaTables)
  23. rst.MoveFirst
  24. Do While Not rst.EOF
  25. If rst.Fields.Item("TABLE_TYPE") = "TABLE" Then
  26. ListBox1.AddItem rst.Fields.Item("TABLE_NAME")
  27. End If
  28. rst.MoveNext
  29. Loop
  30. rst.Close
  31. Set rst = Nothing
  32. Else
  33. MsgBox "Не удается подключиться к базе" & vbCr & dbFileName
  34. dbFileName = False
  35. End If
  36. cn.Close
  37. Set cn = Nothing
  38. Label1.Caption = dbFileName
  39. End Sub
  40.  
  41. Private Sub CommandButton2_Click()
  42. Dim cn As ADODB.Connection
  43. Dim rst As ADODB.Recordset
  44.  
  45. Set cn = New ADODB.Connection
  46. Set rst = New ADODB.Recordset
  47.  
  48. cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  49. "Data Source=" & dbFileName & ";Mode=Read"
  50. cn.Open
  51. rst.Open curQuery, cn
  52. Cells.ClearContents
  53. Cells.NumberFormat = "General"
  54. ActiveSheet.Range("A2").CopyFromRecordset rst
  55. Cells.Columns.AutoFit
  56. rst.Close
  57. cn.Close
  58. Set rst = Nothing
  59. Set cn = Nothing
  60. End Sub
  61.  
  62. Private Sub ListBox1_Click()
  63. Dim cn As ADODB.Connection
  64. Dim rst As ADODB.Recordset
  65. Dim curTable As String
  66. Dim i As Integer
  67. Dim curField
  68.  
  69. Set cn = New ADODB.Connection
  70. Set rst = New ADODB.Recordset
  71.  
  72. cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  73. "Data Source=" & dbFileName & ";Mode=Read"
  74. On Error Resume Next
  75. cn.Open
  76. If cn.State = 1 Then
  77. curTable = ListBox1.List(ListBox1.ListIndex)
  78. ListBox2.Clear
  79. ListBox2.ColumnWidths = "100,140"
  80. rst.LockType = adLockOptimistic
  81. rst.Open "SELECT * FROM [" & curTable & "]", cn
  82. For Each curField In rst.Fields
  83. ListBox2.AddItem
  84. ListBox2.List(ListBox2.ListCount - 1, 0) = curField.Name
  85. ListBox2.List(ListBox2.ListCount - 1, 1) = ""
  86. Next curField
  87. 'MsgBox rst.RecordCount
  88. rst.MoveFirst
  89. i = 0
  90. Do While Not rst.EOF
  91. i = i + 1
  92. rst.MoveNext
  93. Loop
  94. Label2.Caption = "Кол-во записей :" & vbCr & i
  95. rst.Close
  96. End If
  97. cn.Close
  98. Set rst = Nothing
  99. Set cn = Nothing
  100. TextBox1.Value = ""
  101.  
  102. End Sub
  103.  
  104. Private Sub ListBox2_Change()
  105. Dim i As Integer
  106. Dim fieldsList As String
  107. Dim curTable As String
  108.  
  109.  
  110. fieldsList = ""
  111. curTable = ListBox1.List(ListBox1.ListIndex)
  112. For i = 0 To ListBox2.ListCount - 1
  113. If ListBox2.Selected(i) Then
  114. fieldsList = _
  115. fieldsList & "[" & curTable & "." & ListBox2.List(i) & "], "
  116. End If
  117. Next i
  118. If fieldsList <> "" Then
  119. fieldsList = Trim(Left(fieldsList, Len(fieldsList) - 2))
  120. curQuery = "SELECT " & fieldsList & " FROM [" & curTable & "]"
  121. TextBox1.Value = curQuery
  122. Else
  123. TextBox1.Value = ""
  124. End If
  125. End Sub
  126.  
  127. Private Sub ListBox2_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  128. Dim curCondition As String
  129. Dim curFieldName As String
  130. Dim curPrompt As String
  131.  
  132. If Button = 2 Then
  133. curCondition = ListBox2.List(ListBox2.ListIndex, 1)
  134. curFieldName = ListBox2.List(ListBox2.ListIndex, 0)
  135. curPrompt = "Изменить условие отбора для поля " & curFieldName & " ?"
  136. curCondition = InputBox(curPrompt, , curCondition)
  137. If curCondition <> "" Then
  138. ListBox2.List(ListBox2.ListIndex, 1) = curCondition
  139. End If
  140. End If
  141. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement