Results 1 to 7 of 7

Thread: [RESOLVED] Calling a Function from a form...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Location
    Virginia Beach, VA
    Posts
    22

    Resolved [RESOLVED] Calling a Function from a form...

    I am using this function to populate a combo box...

    Public Function AddStates(F As Form, ctl As Control)
    'MsgBox F & "." & ctl
    Set RS = DB.OpenRecordset("tblUnited_States")

    For i = 0 To RS.RecordCount - 1
    F.ctl.AddItem RS![Abbreviation]
    RS.MoveNext
    Next
    RS.Close
    Set RS = Nothing
    End Function

    I call it from my form using this call...

    AddStates frmAdd_Store, Combo1

    When I run this I get Run-Time error 438...

    I'm not so sure that the form name or the control names are getting passed, as it will err out on the msgbox statement alone.

    Any ideas?

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Calling a Function from a form...

    Instead fo passing Form and Control pass only control:

    Public Sub AddStates(ctl As Control)


    And here is how to call that procedure:

    AddStates frmAdd_Store.Combo1


    And finally, you don't need a function since it doesn't return anything.

  3. #3
    Addicted Member BlueRose's Avatar
    Join Date
    Jan 2002
    Location
    ISTANBUL
    Posts
    245

    Re: Calling a Function from a form...

    i think you must use this

    vb Code:
    1. CallByName F.Controls(ctl.Name), "additem", VbLet, RS![Abbreviation]

    instead of
    vb Code:
    1. F.ctl.AddItem RS![Abbreviation]
    You can do while you think that you can do

    If you think my answer solve your question, please rate it.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2011
    Location
    Virginia Beach, VA
    Posts
    22

    Re: Calling a Function from a form...

    Thanks Rhino, worked perfectly.

  5. #5

  6. #6
    Banned
    Join Date
    May 2011
    Posts
    27

    Re: [RESOLVED] Calling a Function from a form...

    wow thats a wack way to add item to a combo box id just make it add what u want it to add instead of using code u dont need

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Calling a Function from a form...

    And what would you do if dozens of controls need to reuse the same logic? Writng code for each?
    My only "problem" with what's being posted here is how recordset is populated: I would pass some additional criteria (flag, sql, etc) so recordset would get different set of data every time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width