Using a string to call a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jriechers
    New Member
    • Mar 2008
    • 11

    Using a string to call a function

    I have a combobox with a list of names that reflect a list of functions. The name of this combobox is [PatchDateFuncti on]. When a specific function name is chosen from that list I want to make [PatchDate] = functionname.

    I have a total of 34 functions, each of which calculates various dates. For simplicity I will show two here.
    Function First_Saturday( ) As Date
    For x = 0 To 6
    If Weekday(DateSer ial(Year(Date), Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x, vbUseSystemDayO fWeek) = vbSaturday Then
    First_Saturday = DateSerial(Year (Date), Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x
    End If
    Next x
    End Function
    Function Second_Thursday _after_First_Tu esday() As Date
    For x = 0 To 6
    If Weekday(DateSer ial(Year(Date), Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x, vbUseSystemDayO fWeek) = vbTuesday Then
    Second_Thursday _after_First_Tu esday = DateSerial(Year (Date), Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x + 9
    End If
    Next x
    End Function

    The example method would looks like this if it was hard coded:
    Private Sub cmbPatchDateFun ction_AfterUpda te()

    [PatchDate] = First_Saturday( )

    End Sub

    The problem is making the function name a variable and how to change the method... Im guessing Ill probably need to create another method but dont know where to begin.
  • jriechers
    New Member
    • Mar 2008
    • 11

    #2
    I think i figured it out...
    Private Sub PatchDateFuncti on_AfterUpdate( )

    Dim strFunctionName As String

    strFunctionName = [PatchDateFuncti on]
    [PatchDate] = Eval(strFunctio nName)

    End Sub

    It seems to work, but seems.... crude somehow.

    Comment

    • Delerna
      Recognized Expert Top Contributor
      • Jan 2008
      • 1134

      #3
      You solution works so well done. However for your reference.
      Your fuctions seem to be all the same except for 2 parameters, so you could have written the function once and passed the differences as parameters
      For example the two functions you show could be done like this.

      [code=vb]

      Function GetSelectedDate (DesiredDay as long,c as int) As Date
      For x = 0 To 6
      If Weekday(DateSer ial(Year(Date), _
      Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x + c, _
      vbUseSystemDayO fWeek) = DesiredDay
      Then
      GetSelectedDate =DateSerial(Yea r(Date), _
      Forms![frmDateCalculat ions]![cmbMonthSelect], 1) + x + c
      End If
      Next x
      End Function
      [/code]

      Then you could call the function like this for the first function

      [code=vb]
      GetSelectedDate vbSaturday,0
      [/code]

      and this for the second


      [code=vb]
      GetSelectedDate vbTuesday,9
      [/code]

      So instead of storing function names in the combo you could have stored the vbSaturday,0 and vbTuesday,9 etc.

      By the way, I didn't check the data type for vbSaturday and vbTuesday so the type in the functions parameters may need to be adjusted

      Comment

      Working...