Searchable userform to update a table

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm trying to create a userform to search, update and delete Items from a database, I'm not really sure on how to achieve this. I'm not very good with VBA but I'm slowly learning. I have so far managed to create a form to enter data into this database, which works well, but I don't know how to change this code to then search and update the database. I have attached the first few rows of the database.

The userform I am wanting to use to edit the data is the same as the one I'm using to enter the data, with the exception that I'm wanting to use Combobox 2 to search the Product names in the database column C, and I want command button 2 to not only clear the form, but I want it to delete the entry in the database and sort the database alphabetically by product name in Column C.

I have found a few versions online, but I can't figure out how to make any of them work in my application.

Any help with this would be greatly appreciated.

Ordering Sheet ver 2 - lets see if this works.xlsm
ABCDFHJLNOP
1Cost PriceUnit priceProductsFridaySaturdaySundayNoshGoodstartTotal for OrderingKG/UnitsWeekly Cost Per Item
26.007.00700g eggs unit 
32.004.20Apples Kg KG 
43.001.00Avocadoes unit 
54.002.90baby spinach bag 
Sheet1
Cell Formulas
RangeFormula
N2:N5N2=SUM(E2,G2,I2,K2,M2)
P2:P5P2=A2*N2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:AExpression=MOD(ROW(),2)=0textNO
B2:C56Expression=MOD(ROW(),2)=0textNO
B1:O1,B324:O1048576,D2:O2,P:P,D3:M56,B57:M323,N3:O323Expression=MOD(ROW(),2)=0textNO



This is the code I use to input into the database.

VBA Code:
Private Sub CommandButton1_Click()
If TextBox1.Value = "" Then
     MsgBox "You must add product description", vbCritical
    Exit Sub
 End If
 If TextBox2.Value = "" Then
    MsgBox "You must add cost price", vbCritical
    Exit Sub
 End If
 If TextBox3.Value = "" Then
    MsgBox "You must add selling price", vbCritical
    Exit Sub
 End If
  If ComboBox1.Value = "" Then
    MsgBox "You must select KG/UNIT", vbCritical
    Exit Sub
 End If
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
rw = ws.Cells.Find(what:="*", searchorder:=xlRows, Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(rw, 3).Value = Me.TextBox1.Value
ws.Cells(rw, 1).Value = Me.TextBox2.Value
ws.Cells(rw, 2).Value = Me.TextBox3.Value
ws.Cells(rw, 15).Value = Me.ComboBox1.Value
UserForm2.TextBox1.Value = ""
UserForm2.TextBox2.Value = ""
UserForm2.TextBox3.Value = ""
UserForm2.ComboBox1.Value = ""
Call Sort_Me
End Sub

Private Sub CommandButton2_Click()
UserForm2.TextBox1.Value = ""
UserForm2.TextBox2.Value = ""
UserForm2.TextBox3.Value = ""
UserForm2.ComboBox1.Value = ""
End Sub

Private Sub CommandButton3_Click()
If TextBox1.Value = "" Then
     MsgBox "You must add product description", vbCritical
    Exit Sub
 End If
 If TextBox2.Value = "" Then
    MsgBox "You must add cost price", vbCritical
    Exit Sub
 End If
 If TextBox3.Value = "" Then
    MsgBox "You must add selling price", vbCritical
    Exit Sub
 End If
  If ComboBox1.Value = "" Then
    MsgBox "You must select KG/UNIT", vbCritical
    Exit Sub
 End If
Dim rw As Integer
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
rw = ws.Cells.Find(what:="*", searchorder:=xlRows, Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1
ws.Cells(rw, 3).Value = Me.TextBox1.Value
ws.Cells(rw, 1).Value = Me.TextBox2.Value
ws.Cells(rw, 2).Value = Me.TextBox3.Value
ws.Cells(rw, 15).Value = Me.ComboBox1.Value
Call Sort_Me
Unload Me
End Sub
Private Sub CommandButton4_Click()
Call Sort_Me
Unload Me
End Sub

Private Sub Label4_Click()

End Sub

Private Sub UserForm_Initialize()
TextBox1.SetFocus
With ComboBox1
    .AddItem "KG"
    .AddItem "Unit"
    .AddItem "Bunch"
    .AddItem "Punnet"
    .AddItem "Bag"
End With
End Sub
Sub Sort_Me()
With Worksheets("Sheet1").Range("$C$2").CurrentRegion
    .Sort Key1:=.Columns(3), Header:=xlYes, Order1:=xlAscending
End With
        
End Sub
 
I forgot to mention one thing in the above: At the top of the code I dim a few variabales which can be used throughout this module by all the subs. So called variables with a module scope. I start these variables with an extra m in front for module. So mvUnits is a variant (v) with a module scope (m). If it were a variable with a global scope (declared in a standard module, available everywhere) then I would declare it as gvItemDB, a variant with global scope.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
sijpie,
Thanks a lot mate, it works a treat. Also thank you very much for all those tips I will definitely put that into practice in the future. Simple things like changing the names of the command buttons and textboxes are something I never even thought about, but makes so much sense once I'm told about it. I really appreciate you taking the time to help me with this, thank you very much.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top