Results 1 to 2 of 2

Thread: Creating a primary key

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Post

    Hello dear VB users,

    Can somebody tell me how set to the field "NewField" to a primairy key:


    Set Ws = DBEngine.Workspaces(0)
    Set Db = Ws.CreateDatabase(DbSettings, dbLangGeneral)
    Dim tdfNew As TableDef

    Set tdfNew = Db.CreateTableDef("newTable")
    tdfNew.Fields.Append tdfNew.CreateField("NewField", dbText)

    tdfNew.Fields("newfield").DefaultValue = "new"

    Db.TableDefs.Append tdfNew
    Db.Close

    Nice greetings,

    Michelle.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Here is what I do. You'll have to modify the ErrorRoutine.
    Code:
    Public Sub AddPrimaryKey(sTableName As String, sFieldName As String)
    
        Dim ndxIndex As Index
        Dim tdTableDef As TableDef
        Dim fldField As Field
        Dim nChars As Integer
        Dim nColons As Integer
        Dim sCurrChar As String
        Dim sFields(5) As String
        Dim nCtr As Integer
        
        On Error GoTo ErrorRoutine
        
        Erase sFields()
        
        'Parse the names of the fields to be included in the index
        For nChars = 1 To Len(sFieldName)
            sCurrChar = Mid(sFieldName, nChars, 1)
            If sCurrChar = ":" Then
                nColons = nColons + 1
                If nColons > 4 Then
                    gsErrText = "Too many fields in index"
                    Err.Raise 10007
                End If
            Else
                sFields(nColons) = sFields(nColons) & sCurrChar
            End If
        Next
        
        Set tdTableDef = gdbTargetDB.TableDefs(sTableName)
        
        'Create new Index object.
        Set ndxIndex = tdTableDef.CreateIndex(sFields(0))
        ndxIndex.Primary = True
        ndxIndex.Unique = True
        ndxIndex.Name = "PrimaryKey"
        
        For nCtr = 0 To nColons
            Set fldField = ndxIndex.CreateField(sFields(nCtr))
            ndxIndex.Fields.Append fldField
        Next
        
        'Save Index definition by appending it to Indexes collection.
        tdTableDef.Indexes.Append ndxIndex
    
    ErrorRoutine:
    
        If Err.Number <> 0 Then
            PrintDetailRpt "AddPrimaryKey", FOUND_ERROR
        Else
            PrintDetailRpt "", OK
        End If
            
    
    End Sub
    ------------------
    Marty
    What did the fish say when it hit the concrete wall?
    > > > > > "Dam!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width