visual basic adodc - category - 具有相同键的多个记录

时间:2015-03-27 20:25:58

标签: vb6

我正试图在VB6中创建一个表单来控制我通过访问创建的数据库,我做了所有事情,一切都很完美,直到我注意到你输入相同的" ID&#34 ;我从访问中得到一个错误,因为我有多个具有相同编号的ID,因为我在访问中激活了主键,所以它不允许这样做,所以我想要做的是当用户从对于其他单元格的Id单元格,visual basic首先扫描id单元格并检查是否没有其他具有相同编号的id,如果是,我需要visual basic来提示我使用msgbox表示有多个id与相同的数字。 我希望你明白我试图说我需要一种方法来做到这一点,只需要遵循的方式。谢谢 * 图片 : http://i.stack.imgur.com/oqBpI.png

1 个答案:

答案 0 :(得分:1)

用户不输入标识字段值。

如果这是关于DataGrid的,通常会锁定Identity列,如下所示:

With Adodc
    With New DbHandler 'A custom Class in the program.
        'This method will create a new database if we don't have one:
        Adodc.ConnectionString = .GetConnectionString(App.Path & "\" & DATABASE)
    End With
    'Use the table "Simple" here.
    .CommandType = adCmdTable 'Could be adCmdText.
    .RecordSource = "Simple"  'And then here a SELECT statement.
End With
With DataGrid
    Set .DataSource = Adodc

    'Lock and skip Column 0, which is a Counter/Identity field in this
    'database query resultset:
    .Columns(0).Locked = True
    .Col = 1
End With

处理此事件也很有用:

Private Sub DataGrid_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    'This logic is meant to skip over our locked Column 0 in the user
    'interface when the user moves by cell-clicking or pressing cursor
    'keys.  Column 0 holds a Counter/Identity field in this query
    'resultset, so manual entry or editing would be a bad thing.
    '
    'Note:
    '
    '   Confusing, but here LastRow and LastCol parameters are really
    '   the "previous" cell coordinates and not the limits or counts
    '   of the rows and columns in the grid.
    With DataGrid
        If .Col = 0 Then
            If IsNull(LastRow) Then 'User moved down.
                .Col = 1
            ElseIf LastCol = .Col + 1 Then 'User moved left.
                If .Row = 0 Then
                    'No previous row to move to, so just move back!
                    .Col = LastCol
                Else
                    .Row = .Row - 1
                    .Col = .Columns.Count - 1
                End If
            Else 'User moved right.
                .Col = 1
            End If
        End If
    End With
End Sub