I use the following code and it works but I am trying to convert the repeating code into a function
The function in a module
Code:
Sub ModkMilk(FormName)
Dim ctl As Control
If Forms(FormName).milk = -1 Then
For Each ctl In Forms(FormName).Controls
If ctl.Tag = "milk" Then
' do stuff
End Sub
Sub ModCheese(FormName)
Dim ctl As Control
If Forms(FormName).cheese = -1 Then
For Each ctl In Forms(FormName).Controls
If ctl.Tag = "cheese" Then
' do same stuff
Code on form:
Code:
Private Sub Form_Current()
Dim FormName As String
FormName = Me.Name
ModMilk (FormName)
ModCheese(FormName)
' etc
End Sub
I have this code repeated for each product checked. The milk variable can be swapped out for cheese, carrots, onions, green_beans, etc.
I want to make it a function that passes both the FormName and the ProductName (which is referenced twice as tag matches product control name).
Tried below but it bombs:
Code:
Sub ModProduct(FormName), (ProductName)
Dim ctl As Control
If Forms(FormName).ProductName = -1 Then 'bombs here
For Each ctl In Forms(FormName).Controls
If ctl.Tag = "ProductName" Then
' do stuff
Private Sub Form_Current()
Dim FormName As String
Dim ProductName As String
FormName = Me.Name
ModProduct(FormName), ("milk")
'ModProduct(FormName), ("cheese")
'ModProduct(FormName), ("carrots")
'ModProduct(FormName), ("onions")
' etc
End Sub
If I change function code to below it works, so obviously I am having a hard time passing the variable to a control name.:
Code:
If Forms(FormName).milk = -1 Then
For Each ctl In Forms(FormName).Controls
If ctl.Tag = "ProductName" Then
' do stuff
Also, is there a way to pass all the variables as a list in a single command, such as
Code:
MyList = ("milk", "cheese", "carrots", "onions")
ModProduct (FormName), (MyList)
As that would be even more awesome.
Thanks.