Hello there,
I've never used VBA as much as I'd like, but now i'm building something and I'm trying to figure out how to do things. in this case, I already figured out how to write what I need the code to do but I realise that I will have to write a bunch of repetitive code and I want to know if there's a way to reduce it some how, here's the deal:
I'm creating a form that will be used on touchscreen, so I will have more than a 100 buttons, each one represents a product, but I have 2 different prices depending on the type of purchase, so the code is written under the after update event of the "SaleType" Combobox, type "Local" will show "PriceL" and the rest "PriceP".
I've figure out a code that works just fine but I see that I would have to write it over and over again changing the PCode value and the button name, below is the code with only two buttons made (PN2001Btn and PN2003Btn), I suppose there should be a way to declare the formulas or something so that I only need to call them below the PCode...
Thanks in advance for your time and help!
Code:
Private Sub SaleType_AfterUpdate()
Dim LPrice As Variant
Dim PPrice As Variant
Dim ProdName As Variant
Dim PCode As Integer
PCode = 2001
LPrice = DLookup("[PrecioL]", "[Precios]", "[PN]=" & PCode)
PPrice = DLookup("[PrecioP]", "[Precios]", "[PN]=" & PCode)
ProdName = DLookup("[Producto]", "[Products]", "[PN]=" & PCode)
If Me.SaleType = "Local" Then
PN2001Btn.Caption = ProdName & " $" & LPrice
Else
PN2001Btn.Caption = ProdName & " $" & PPrice
End If
PCode = 2003
LPrice = DLookup("[PrecioL]", "[Precios]", "[PN]=" & PCode)
PPrice = DLookup("[PrecioP]", "[Precios]", "[PN]=" & PCode)
ProdName = DLookup("[Producto]", "[Products]", "[PN]=" & PCode)
If Me.SaleType = "Local" Then
PN2003Btn.Caption = ProdName & " $" & LPrice
Else
PN2003Btn.Caption = ProdName & " $" & PPrice
End If
End Sub