Problem giving control name as a argument .

Lestatos

Registered User.
Local time
Today, 06:45
Joined
Oct 22, 2013
Messages
16
Good evening , dear programmers .
I have a small problem (hope solvable :) ) .
I am designing a Public Function F()
I want this function to populate value from a any field selected from any table to any text box in any form ...... Sounds a bit crazy probably but I will try to explain my idea .
Lets say in a Database named TestDB we have a Table named tblTest , a Form named frmTest and in this form( frmTest ) we have one Bound Combo Box named cmbTest and one unbound TextBox named txtTest

We assume that the table tblTest has three fields : TestID , FName and LName .
We also assume that there are already some records in the Table tblTest .

If the function F() is already programmed it should take as arguments as it follows :
F(FormName as??? ,ControlName as ???, TableName as ???, FieldName as ???, ID_Field_Name_of_the_Table as ???, Combo_box_selected_ID as ???)
In result the function should (probably) DLookup (FieldName , TableName , ID_Field_Name_of_the_Table = Combo_box_selected_ID )
and then store the value in a variable ( probably Variant ???) , lets say called varSetValue
The problem is IF this is the correct way to handle that issue , than what should i do next....
I really don't know how to pass a Control's name to a function from the Event module of the ComboBox -
I mean - how can I obtain the value so it is usable for the function .... What data type should be the function arguments so I can use them to set a value for a control in any form .
In example :
If the Dlookup() is somehow successfull then I want to assign the varSetValue to the txtTest what should I do :
FormName.ControlName.Value = varSetValue >??????
I have read a lot materials but couldn't find a good answer for that , and aswell what data type should be the arguments that the function accepts and how do I obtain Controls and Forms names so I could use them in the function as described above .
Sorry for the long writing but I am really into a blackhole :(
 
I think you make it to complicate, a function is usually returning a "value" back to the calling "part" so you don't need to handle with the control name.
Set the control source for the control to:
Code:
=ReturnValue("TECHINFOTBL";"AD";"ID";[Combo2])
Tablename = "TECHINFOTBL"
Fieldname to get the returned value from = "AD"
Filedname where the criteria should be like the value from the combobox ="ID"
The name of the combobox on the form=[Combo2]
Remember to change the above names to what you use.

Code:
Function ReturnValue(TableName As String, FieldName As String, ID_Field_Name_of_the_Table As String, Combo_box_selected_ID As Variant)
  Dim dbs As Database, rst As Recordset
  
  If Not IsNull(Combo_box_selected_ID) Then
    Set dbs = CurrentDb
    
    Set rst = dbs.OpenRecordset("Select [" & FieldName & "] From " & TableName & " Where [" & ID_Field_Name_of_the_Table & "]=" & Combo_box_selected_ID)
    If Not rst.EOF Then
      ReturnValue = rst(FieldName)
    Else
      ReturnValue = ""
    End If
  Else
    ReturnValue = ""
  End If
End Function
 
JHB , Thank you really a LOT for the handy answer !
You got me out of the hell . :)
I hope that answer will help also other beginners .
Best regards !
 
:) Hahaha yep it was hot - indeed - for the moment I am beginner in programing at all ( well - back in the schoolyears I studied C++ but that is another story ) - so everyday staying infront my computer and trying to add any new idea to my Access project I turn back to hell :) .
Thanks for the wishes , you also have GL with your job .
 

Users who are viewing this thread

Back
Top Bottom