Reputation: 43
Is possible to compare a text with the name of a field in a table ?
Ex.: I have a table with fields (p300, p600, P1200, P1800, etc.), these fields are of type True or False, and my application will come a parameter which will be a text (p300, p600, P1200, P1800 etc.) then had to return only the values where the field is true, but first needed to compare what field I will check.
Say my parameter is p600, would then have to verify the p600 column, however as I have several speakers wondered if he had somehow first find the columnar to then check if the value of that column is true or false.
I thought of using somethig like this, UtilizaVariavelGlobal ( ' Plano' ) is my variable that has the same name as the table field .
Select * from Table Where Table.UtilizaVariavelGlobal ( ' Plano' ) = True
Instead of p600 would like to use a variable that has the same name of the field ? Thank you !
Upvotes: 0
Views: 86
Reputation: 55816
In SQL you can concatenate a string:
FieldName = "P600"
SQL = "Select * From YourTable Where [" & FieldName & "] = True"
or with a function:
SQL = "Select * From YourTable Where [" & UtilizaVariavelGlobal("Plano") & "] = True"
With a recordset it is easy too:
Criteria = FieldName & " = True"
rst.FindFirst Criteria
Even DLookup:
FoundId = DLookup("ID", "YourTable", "" & FieldName & " = True")
Upvotes: 1