cancel
Showing results for 
Search instead for 
Did you mean: 

Create Index On User-defined Field

Former Member
0 Kudos

Good Day

Experts:

I would like to create Indexes on User-Defined fields at the time I create the table.

However, I do not want to click on the CreateIndex checkbox once inside a specific field. I want to do it programatically like when you hit the Keys button after highlighting a table from the list.

Any ideas how I can achieve this?

Thanks,

Ed

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ed

Try something like this function

    Private Sub addUserTableKeys(ByVal strTableName As String, ByVal strKeyFieldName As String, ByVal strKeyName As String, Optional ByVal bolUnique As Boolean = True)
        Try
            Dim bFlagFirst As Boolean = True
            Dim oUserKeysMD As SAPbobsCOM.UserKeysMD
            oUserKeysMD = msboCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserKeys) ' Set table name and key name
            oUserKeysMD.TableName = strTableName
            oUserKeysMD.KeyName = strKeyName
            If bFlagFirst = True Then
                bFlagFirst = False ' Add method not used for first element
            Else
                oUserKeysMD.Elements.Add() ' Add item to Elements collection
            End If
            oUserKeysMD.Elements.ColumnAlias = strKeyFieldName ' Set column's alias
            oUserKeysMD.Unique = IIf(bolUnique, SAPbobsCOM.BoYesNoEnum.tYES, SAPbobsCOM.BoYesNoEnum.tNO) ' whether key is unique or not
            oUserKeysMD.Add() ' Add the key
        Catch ex As Exception
            msboApplication.StatusBar.SetText("Fail to create data!" & vbCrLf & ex.Message)
        End Try
    End Sub

now, all you need to pass is a couple of parameters to the function like this ..

Call addUserTableKeys("TAB1", "PP_Field1", "KEY1")

hope it helps

Former Member
0 Kudos

Neftali:

That clears up a few items that were unclear to me in the bit of info I found on the object. I am just curios about the flag checking. Is that if there already is an index on the table and you are adding another one? If so, how do I check if there is one present for the next time I call the subroutine to add another one?

Thanks,

EJD

Former Member
0 Kudos

Hmm

I can understand your curiosity. Looking at this code again (which probably found on the SAP SDK documentation) , the flag should be pass as a parameter! And maybe you'll end up with something like this ...

Call addUserTableKeys("TAB1", "PP_Field1", "KEY1", True)

Call addUserTableKeys("TAB1", "PP_Field2", "KEY2")

Call addUserTableKeys("TAB1", "PP_Field2", "KEY3")

in the case of adding multiple keys.

Although my code seems to be working so far I believe this could be a bug on my part.

Former Member
0 Kudos

Hmm

I can understand your curiosity. Looking at this code again (which probably found on the SAP SDK documentation) , the flag should be pass as a parameter! And maybe you'll end up with something like this ...


Call addUserTableKeys("TAB1", "PP_Field1", "KEY1", True)
Call addUserTableKeys("TAB1", "PP_Field2", "KEY2")
Call addUserTableKeys("TAB1", "PP_Field2", "KEY3")

in the case of adding multiple keys.

Although my code seems to be working so far I believe this could be a bug on my part.

Former Member
0 Kudos

Ok I understand...just thought I might have been missing something. So, you might be calling this routine from a loop and sending over whether it is the first time through the loop or the second or third. It is the first time adding a group at a time not to use the Add and not the first one ever created ?

Thanks,

EJD

Former Member
0 Kudos

Right

I thought about it myself also! I use it in a more simple manner thought. In the beginning I was trying though to establish database constraints instead, like sql relationships, foreign and parent keys. I found out though that SAP don't let set these in a more easy-straight forward manner. Is a pain, really!

I can't for instance set a master-child relationship between the Employees Master Table and a User table!

Answers (0)