I call a specific piece of code several times, so I would like to use optional parameters. I can write something like:
Public Sub main() strA = "A" 'Calling the function CalculateMe (strA) End Sub Public Sub CalculateMe(strA As String) Set rs = DB.OpenRecordset("tbl_A") rs.MoveFirst Do Until rs.EOF If rs.Fields(0) = strA Then dblA = rs.fields(2).Value End If rs.MoveNext Loop End Sub
How can I change a function to store more than 1 optional parameters?
Sort of:
Public Sub main() strA = "A" strB = "B" 'Calling the function CalculateMe (strA, strB) more code End Sub Public Sub CalculateMe(Optional strA As String, Optional strB as String) Set rs = DB.OpenRecordset("tbl_A") rs.MoveFirst Do Until rs.EOF If rs.Fields(0).Value = strA And rs.Fields(1).Value = strB Then dblA = rs.Fields(2).Value End If rs.MoveNext Loop End Sub
Following the advice of Pankaj Jaju, I managed to run it by changing it to:
Public Sub main() strA = "A" strB = "B" 'Calling the function dblA = CalculateMe (strA, strB) End Sub Public Function CalculateMe(Optional ByVal strA As String, Optional ByVal strB as String) Set rs = DB.OpenRecordset("tbl_A") rs.MoveFirst Do Until rs.EOF If rs.Fields(0).Value = strA And rs.Fields(1).Value = strB Then dblA = rs.Fields(2).Value End If rs.MoveNext Loop End Sub
Now, how can I clear the value of an optional parameter? I will need this for some calculations. Sort of:
Set strA = Nothing
source share