If you can't accept solutions from links in comments.
And if you will know values for all "myChoice" before invocation of function TestProg. You can easially construct it dynamically.
step 1. create empty module and save it with name "emptyModule"
step 2. create module "RunTimeVariableConstruction" put this code there
Option Compare Database
Option Explicit
Public Type myType
my_abc As Integer
my_xyz As Integer
End Type
Public Function TestProg()
Dim A As myType
A.my_(((myChoice))) = 345 ' Made up syntax to attempt assignment to A.my_xyz
Debug.Print "now A.my_(((myChoice))) is "; A.my_(((myChoice)))
A.my_(((myChoiceForSecondField))) = 678 ' Made up syntax to attempt assignment to A.my_xyz
Debug.Print "now A.my_(((myChoiceForSecondField))) is "; A.my_(((myChoiceForSecondField)))
Debug.Print "and A.my_(((myChoice))) + A.my_(((myChoiceForSecondField))) is "; A.my_(((myChoice))) + A.my_(((myChoiceForSecondField)))
End Function
Public Sub MakeAndExecute(funcName As String, moduleName As String, ParamArray Mappings())
'MakeAndExecute "TestProg","RunTimeVariableConstruction", "myChoice","xyz","myChoiceForSecondField","abc"
Dim mSrc As Module
Dim mTemp As Module
Set mSrc = Modules(moduleName)
DoCmd.OpenModule "emptyModule"
Set mTemp = Modules("emptyModule")
Dim prog As String
Dim i As Long
prog = mSrc.Lines(mSrc.ProcStartLine(funcName, vbext_pk_Proc), mSrc.ProcCountLines(funcName, vbext_pk_Proc))
prog = Replace(prog, funcName, funcName & "_RTVC")
For i = 0 To UBound(Mappings) Step 2
prog = Replace(prog, "(((" & Mappings(i) & ")))", Mappings(i + 1))
Next i
mTemp.DeleteLines 1, mTemp.CountOfLines
mTemp.InsertText prog
Eval funcName & "_RTVC()"
DoCmd.Close acModule, "emptyModule", acSaveNo
End Sub
I placed you TestProg in this module for example. Placeholder is (((SomeDynamicPartName))) in text of function.
step 3. test it by calling in intermediate window:
MakeAndExecute "TestProg","RunTimeVariableConstruction", "myChoice","xyz","myChoiceForSecondField","abc"
here "TestProg" is name of function with dynamic template, "RunTimeVariableConstruction" is module of that function. Next pairs of parameters (placeHolder, value). i.e. every occurence of (((myChoice))) in TestProg will be replaced by "xyz", and (((myChoiceForSecondField))) by "abc"
sep 4. get debug output of TestProg
now A.my_xyz is 345
now A.my_abc is 678
and A.my_xyz + A.my_abc is 1023