Use field name as a parameter in a subroutine or function

  • Thread starter Thread starter bassi.carlo
  • Start date Start date
B

bassi.carlo

I have an application with many combobox in different forms used for
choose a particular length (for example items are "ft" - feet, "in" -
inch, "m" - meters, and so on).
I don't want to put in every form (class), in load event, the code
needed to load items from a Db table using items.add.
I want to create a public subroutine (or function), when i call it I
pass, as a parameters, the form and field name, then the subroutine
read from the table and add items in the form/field I have passed as
parameters.
I don't know if I can do that and how.
Any suggestion?
 
I have an application with many combobox in different forms used for
choose a particular length (for example items are "ft" - feet, "in" -
inch, "m" - meters, and so on).
I don't want to put in every form (class), in load event, the code
needed to load items from a Db table using items.add.
I want to create a public subroutine (or function), when i call it I
pass, as a parameters, the form and field name, then the subroutine
read from the table and add items in the form/field I have passed as
parameters.
I don't know if I can do that and how.
Any suggestion?

Your function should be in a module and probably look like this:

Public Sub AddItemsToComboBox(byref combo as ComboBox,byval field as
String,byval table as String)
combo.items.clear()

'get the records from the database
combo.items.add(field & " from function")
combo.items.add("another record")

end sub

and then you can call it like this:

public sub form_load(...)

AddItemsToComboBox(ComboBox1,"fieldname","tablename")

end sub

That's all you should need to do. Adapt this as you need it :-)

Phill
 
Back
Top