Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Add new item to table name

0

I want this code to add value to all table, (Code, item name, unit, price)

Private Sub cmdAdd_Click()
 Const PW As String = "8521" 'using const for fixed values
    Dim allTables(), lo As ListObject, rw As ListRow, tName
     allTables = Array("T_Food", "T_Beverage", "T_Shisha")
    If Me.comDepartment.Text = "" Or Me.txtItemName.Text = "" Or _
       Me.txtPrice.Text = "" Or Me.comUnit.Text = "" Then
        MsgBox "Please enter data  ", vbCritical, "Inventory program "
         Exit Sub
            End If
       new_name = Me.txtItemName.Text
        'see if the new name already exists in any of the tables
        If Not AnyTableRowMatch(allTables, "Item Name", new_name) Is Nothing Then
             MsgBox "The new name '" & new_name & "' already exists  ", _
                   vbCritical, "Inventory Program "
            Exit Sub
        End If
    'select the correct table
    Select Case Me.comDepartment.Value
        Case "Food": tName = "T_Food"
        Case "Beverage": tName = "T_Beverage"
        Case "Shisha": tName = "T_Shisha"
        Case "Other": tName = "T_Other"
    End Select
    'find the record being edited
    Set rw = TableRowMatch(Data.ListObjects(tName), "Item Name, new_name")
    If Not rw Is Nothing Then
        Data.Unprotect PW
        'update the row
        '-----------------------------------------------------
        ' update the row values (but I don't know where Start and Price come from)
         rw.Range.Value = Array(Me.txtCode.Text, new_name, Me.comUnit.Text, _
                                    Me.txtPrice.Text, Me.TxtSalePrice.Text, _
                                    "", "", Me.CbSubCategory.Text)
        '-----------------------------------------------------
        Data.Protect Password:=PW
    Else
        MsgBox "Edited row not found!"
    End If
    ' update the userfrom
    Call CbSubCategory_Change
  End Sub
Answer
Discuss

Discussion

Amin

I will answer later if I get time. For future questions please note that we expect you to attempt to solve your own problem before asking a question - which should not be like "make my code do this..." (In this case it looks like you just copied code from other UserForm controls then asked for code additions to add thenew item).
John_Ru (rep: 6142) Jan 22, '24 at 5:55 am
I like your code the one to add new items but I tried to use it with a table that has many columns and I don't know how to edit it to use with the table have many columns, Here is your code but this code if only with one textbox and new userforn is around 4 textbox so how can I fix it?
Amin25 (rep: 16) Jan 22, '24 at 6:16 am
Here is your code the one I like 

Private Sub cmdAdd_Click()
 
   Const PW As String = "8521"
   Dim NewSub As String
  
   Set ss = Data
   ' give shorter name the chosen subcategory
   NewSub = txtSubCategory.Text
   
    If ComCategory.Text = "" Or txtSubCategory.Text = "" Then
        Beep
        MsgBox " Please input the data  ", vbInformation, "Inventory Program"
        Exit Sub
    End If
  
  
    Data.Unprotect PW
   
    Application.ScreenUpdating = False
    'Application.DisplayAlerts = False
    
    ' get the chosen category table data
    With ss.ListObjects("TSub_" & ComCategory.Text)
 
        If .ListRows.Count = 0 Then
            'if first entry is blank, add row and value
                With .ListRows.Add
                    .Range = NewSub
                End With
Amin25 (rep: 16) Jan 22, '24 at 6:19 am
Continue code 

 Else
            ' otherwise loop to check if value exists
            For checkn = 1 To .ListRows.Count
                If .ListRows(checkn).Range.Value = NewSub Then
                    ' if it does, say then quit
                    MsgBox NewSub & " already exists"
                    Exit Sub
                End If
            Next checkn
            ' otherwise, add to end
            With .ListRows.Add
                    .Range = NewSub
            End With
        End If
    End With
    ' make change visible before you say what's been done
    Application.ScreenUpdating = True
    'Application.DisplayAlerts = False
 
    Me.Label9.Visible = True
    
    Data.Protect Password:=PW
    
    MsgBox NewSub & " has been successfully added"
    
    Me.ComCategory = ""
    Me.txtSubCategory = ""
 
   
End Sub
Amin25 (rep: 16) Jan 22, '24 at 6:20 am
So, can you help me fix both codes to add items and edit items? 
Amin25 (rep: 16) Jan 22, '24 at 6:24 am
Amin. Please see my Answer. Also, next time kindly define your question well (and please don't add new parts to it!)
John_Ru (rep: 6142) Jan 22, '24 at 10:25 am
Add to Discussion

Answers

0
Selected Answer

Amin

You've added to your first (inadequately-defined) Question in its Discussion but I'm answering only the question of adding a new item (since the Update button already does any record editting).

In the attached revised code, I've added a new variable cl (for first column of a chosen table) then created n new code number for the new item in a similar way to how you have done elsewhere in the UserForm code. See bold items.

Private Sub cmdAdd_Click()

    Const PW As String = "8521" 'using const for fixed values

    Dim allTables(), lo As ListObject, rw As ListRow, tName
    Dim cl As Long


     allTables = Array("T_Food", "T_Beverage", "T_Shisha")


    If Me.comDepartment.Text = "" Or Me.txtItemName.Text = "" Or _
       Me.txtPrice.Text = "" Or Me.comUnit.Text = "" Then
        MsgBox "Please enter data  ", vbCritical, "Inventory program "
         Exit Sub
    End If

    new_name = Me.txtItemName.Text

     'see if the new name already exists in any of the tables

     If Not AnyTableRowMatch(allTables, "Item Name", new_name) Is Nothing Then

          MsgBox "The new name '" & new_name & "' already exists  ", _
                vbCritical, "Inventory Program "

         Exit Sub

     End If

    'select the correct table

    Select Case Me.comDepartment.Value

        Case "Food": tName = "T_Food"
        Case "Beverage": tName = "T_Beverage"
        Case "Shisha": tName = "T_Shisha"
        Case "Other": tName = "T_Other"

    End Select

    With Data
        .Unprotect PW
        ' add a new record at the end of correct table
        Set rw = .ListObjects(tName).ListRows.Add
        With rw
            ' get first column of table
            cl = .Range.Columns(1).Column
            ' set the code as highest in first column + 1
            Me.txtCode.Text = WorksheetFunction.Max(Range(Cells(7, cl), Cells(2000, cl))) + 1
            ' write the values to new record
            .Range.Value = Array(Me.txtCode.Text, new_name, Me.comUnit.Text, _
                            Me.txtPrice.Text, Me.TxtSalePrice.Text, _
                            "", "", Me.CbSubCategory.Text)
        End With
        .Protect Password:=PW
    End With


    ' update the userform
    Call CbSubCategory_Change

End Sub

Hope this is what you need. If so, please mark this Answer as Selected.

Discuss

Discussion

Thank you so much 
Amin25 (rep: 16) Jan 22, '24 at 10:30 am
Glad that worked for you. Thanks for selecting my Answer, Amin..

Hope you don't have problems this week since I will have very little time to do anything for the Forum. If you do, be sure to write full questions so others (probably WillieD24) can understand what problem you have encountered.
John_Ru (rep: 6142) Jan 22, '24 at 10:38 am
Thank you so much
Amin25 (rep: 16) Jan 22, '24 at 12:59 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login