Visual Basic, .NET, ASP, VBScript
 

   
   
     

Форум - VBA

Страница: 1 |

 

  Вопрос: Access, кол-во полей, соответствующих условию Добавлено: 12.10.04 01:30  

Автор вопроса:  sergee | Web-сайт: webserge.no-ip.info | ICQ: 345653131 
Подскажите как программно посчитать количество полей в таблице соответствующих определенному условию.

Ответить

  Ответы Всего ответов: 1  

Номер ответа: 1
Автор ответа:
 Mihalыch



ICQ: 373-509-101 

Вопросов: 56
Ответов: 330
 Профиль | | #1 Добавлено: 12.10.04 07:40
Если тебе нужно узнать структуру базы (список таблиц, тип и размер полей),
помести на форму ListBox и ListView, потом используй этот код:

    ;Dim StructureItem As ListItem
    ;Dim cnnCheckStructure As ADODB.Connection
    ;Dim rsCheckSchema As ADODB.Recordset
    ;Dim rsCheckStructure As ADODB.Recordset

Private Sub Form_Load()
    ;Dim OpenResult As Byte
    ;Dim clmnh As ColumnHeader
    ListView1.ColumnHeaders.Clear
    Set clmnh = ListView1.ColumnHeaders.Add(, , "Fields";)
    ListView1.ColumnHeaders(1).Width = 2000
    Set clmnh = ListView1.ColumnHeaders.Add(, , "Type";)
    ListView1.ColumnHeaders(2).Width = 1000
    Set clmnh = ListView1.ColumnHeaders.Add(, , "Size";)
    ListView1.ColumnHeaders(3).Width = 1000
    OpenResult = OpenConnection(App.Path & "\Proba.mdb", cnnCheckStructure)
    If OpenResult = 1 Then ReadingOfSchema
End Sub
Function OpenConnection(strConnectionString As String, cnnName As ADODB.Connection) As Byte
    ;Dim Mb As Integer
    On Error GoTo ErrorLine
    If cnnName Is Nothing = True Then
        Set cnnName = New ADODB.Connection
        With cnnName
            .Mode = adModeShareDenyNone
            .Provider = "Microsoft.Jet.OLEDB.4.0"
            .ConnectionString = strConnectionString
            .Open
        End With
    Else
        If cnnName.State <> adStateOpen Then
            With cnnCheckStructure
                .Mode = adModeShareDenyNone
                .Provider = "Microsoft.Jet.OLEDB.4.0"
                .ConnectionString = strConnectionString
                .Open
            End With
        Else
            cnnName.Close
            Set cnnName = Nothing
            OpenConnection = OpenConnection(strConnectionString, cnnName)
        End If
    End If
    OpenConnection = cnnName.State
    Exit Function
ErrorLine:
    MsgBox "Нераспознаваемый формат базы данных!", vbCritical, "Ошибка соединения!"
    OpenConnection = 3
End Function
Sub ReadingOfSchema()
    ;Dim strCurrTableName As String
    ;Dim strNewTableName As String
    Set rsCheckSchema = cnnCheckStructure.OpenSchema(adSchemaColumns)
    ;Do While Not rsCheckSchema.EOF
        If Left(rsCheckSchema!TABLE_NAME, 4) <> "MSys" And rsCheckSchema!TABLE_NAME <> "Запрос1" And Left(rsCheckSchema!TABLE_NAME, 1) <> "~" Then
            strCurrTableName = rsCheckSchema!TABLE_NAME
            If strCurrTableName <> strNewTableName Then
                List1.AddItem rsCheckSchema!TABLE_NAME
                strNewTableName = rsCheckSchema!TABLE_NAME
            End If
        End If
        rsCheckSchema.MoveNext
    Loop
End Sub
Sub ReadingOgStructure(TableName As String)
    ;Dim i As Long
    Call RsOpen("SELECT * FROM " & TableName, rsCheckStructure, cnnCheckStructure)
    For i = 0 To rsCheckStructure.Fields.Count - 1
        Set StructureItem = ListView1.ListItems.Add()
        StructureItem.Text = StrConv(rsCheckStructure.Fields(i).Name, vbProperCase)
        StructureItem.SubItems(1) = DefinitionType(rsCheckStructure.Fields(i).Type)
        StructureItem.SubItems(2) = rsCheckStructure.Fields(i).DefinedSize
    Next i
End Sub
Function DefinitionType(lngType As Long) As String
    ;Dim strType As String
    Select Case lngType
        Case 3: strType = "Long"
        Case 7: strType = ";Date"
        Case 202: strType = "String"
        Case 2: strType = "Integer"
        Case 11: strType = "Boolean"
        Case 17: strType = "Byte"
        Case 203: strType = "Memo или гиперссылка"
        Case 4: strType = "Single"
        Case 5: strType = ";Doble"
        Case 17: strType = "Код репликации"
        Case 131: strType = "Действительное"
        Case 6: strType = "Денежный"
        Case 72: strType = "Счетчик код репликации"
        Case 205: strType = "Поле объекта"
    End Select
    ;DefinitionType = strType
End Function
Sub RsOpen(strSQL As String, rsName As ADODB.Recordset, cnnName As ADODB.Connection)
    Set rsName = New ADODB.Recordset
    rsName.ActiveConnection = cnnName
    If rsName.State <> adStateOpen Then
        rsName.CursorType = adOpenStatic
        rsName.LockType = adLockOptimistic
        rsName.Source = strSQL
        rsName.Open
    End If
End Sub

Private Sub List1_Click()
    ListView1.ListItems.Clear
    ReadingOgStructure (List1.Text)
End Sub

Ответить

Страница: 1 |

Поиск по форуму



© Copyright 2002-2011 VBNet.RU | Пишите нам