Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Create Table and fields

Hi All,

   I need to create UDT and UDF when button Pressed. How to do this?

replied

Hi,

Please call the below function on button press

For Table Creation

Function CreateTable(ByVal TableName As String, ByVal TableDesc As String, ByVal TableType As SAPbobsCOM.BoUTBTableType) As Boolean

        CreateTable = False

        Dim v_RetVal As Long

        Dim v_ErrCode As Long

        Dim v_ErrMsg As String = ""

        Try

            If Not Me.TableExists(TableName) Then

                Dim v_UserTableMD As SAPbobsCOM.UserTablesMD

                oApplication.StatusBar.SetText("Creating Table " + TableName + " ...................", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)

                v_UserTableMD = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserTables)

                v_UserTableMD.TableName = TableName

                v_UserTableMD.TableDescription = TableDesc

                v_UserTableMD.TableType = TableType

                v_RetVal = v_UserTableMD.Add()

                If v_RetVal <> 0 Then

                    oCompany.GetLastError(v_ErrCode, v_ErrMsg)

                    oApplication.StatusBar.SetText("Failed to Create Table " & TableDesc & v_ErrCode & " " & v_ErrMsg, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(v_UserTableMD)

                    v_UserTableMD = Nothing

                    GC.Collect()

                    Return False

                Else

                    oApplication.StatusBar.SetText("[" & TableName & "] - " & TableDesc & " Created Successfully!!!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(v_UserTableMD)

                    v_UserTableMD = Nothing

                    GC.Collect()

                    Return True

                End If

            Else

                GC.Collect()

                Return False

            End If

        Catch ex As Exception

            oApplication.StatusBar.SetText(addonName & ":> " & ex.Message & " @ " & ex.Source)

        End Try

    End Function

To Create Field

Function CreateUserFields(ByVal TableName As String, ByVal FieldName As String, ByVal FieldDescription As String, ByVal type As SAPbobsCOM.BoFieldTypes, Optional ByVal size As Long = 0, Optional ByVal subType As SAPbobsCOM.BoFldSubTypes = SAPbobsCOM.BoFldSubTypes.st_None, Optional ByVal LinkedTable As String = "", Optional ByVal ComboValidValues As String(,) = Nothing, Optional ByVal DefaultValidValues As String = "") As Boolean

        Try

            'If TableName.StartsWith("@") = False Then

            If Not Me.UDFExists(TableName, FieldName) Then

                Dim v_UserField As SAPbobsCOM.UserFieldsMD

                v_UserField = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oUserFields)

                v_UserField.TableName = TableName

                v_UserField.Name = FieldName

                v_UserField.Description = FieldDescription

                v_UserField.Type = type

                If type <> SAPbobsCOM.BoFieldTypes.db_Date Then

                    If size <> 0 Then

                        v_UserField.Size = size

                    End If

                End If

                If subType <> SAPbobsCOM.BoFldSubTypes.st_None Then

                    v_UserField.SubType = subType

                End If

                For i As Int16 = 0 To ComboValidValues.GetLength(0) - 1

                    If i > 0 Then v_UserField.ValidValues.Add()

                    v_UserField.ValidValues.Value = ComboValidValues(i, 0)

                    v_UserField.ValidValues.Description = ComboValidValues(i, 1)

                Next

                If DefaultValidValues <> "" Then v_UserField.DefaultValue = DefaultValidValues

                If LinkedTable <> "" Then v_UserField.LinkedTable = LinkedTable

                v_RetVal = v_UserField.Add()

                If v_RetVal <> 0 Then

                    oCompany.GetLastError(v_ErrCode, v_ErrMsg)

                    oApplication.StatusBar.SetText("Failed to add UserField " & FieldDescription & " - " & v_ErrCode & " " & v_ErrMsg, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(v_UserField)

                    v_UserField = Nothing

                    Return False

                Else

                    oApplication.StatusBar.SetText(" & TableName & - " & FieldDescription & " added successfully!!!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(v_UserField)

                    v_UserField = Nothing

                    Return True

                End If

            Else

                Return False

            End If

            ' End If

        Catch ex As Exception

            oApplication.MessageBox(ex.Message)

        End Try

    End Function

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question