Using vba variables in forms commands

MSACCESSIT

New member
Local time
Yesterday, 20:45
Joined
Mar 14, 2012
Messages
2
Hello,
I'm new to the forum.
I've been banging my head against the wall trying to get this to work:

I am using vba within MS ACCESS
I want to use passable parameters within a form command:

Here is my function code:
Function TESTFUN(FN, TB As String)
DoCmd.OPENFORM (FN), , , , , acDialog
Forms(FN).Text(TB).SetFocus
End Function

Here is my MACRO which calls the function:
TESTFUN("PARTA","010")

The PARTA form does open up OK, but the forms command does not work.
I'm getting a Run-time error '2450', Microsoft Access cannot find the
referenced form 'PARTA'.

Any help would be appreciated.
Thanks in advance.
 
Code:
Function TESTFUN(FN, TB As String)
DoCmd.OPENFORM (FN), , , , , [B]acDialog[/B]
Forms(FN).[COLOR="Red"]Text[/COLOR](TB).SetFocus
End Function

acDialog mode halts further code execution until the form has closed so it won't get to the next line of code so when you close the form and the code advances you will get an error because the form is no longer in the forms collection.

the .Text marked in red ?? that should be .Form instead if you want to set focus to a named control on that form.

Try this:

Code:
Function TESTFUN(FN As String, TB As String)
DoCmd.OpenForm (FN)
Forms(FN).Form(TB).SetFocus
End Function

JR
 
JR:
Thanks for your help.

Now, the form opens up oK, but I'm getting an error on the setfocus.

I've tried passing "010" to Text(TB), and I've tried passing the whole
Text010 as TB. Both do not work. I also tried putting brackets around
TB, like so [TB].
 

Users who are viewing this thread

Back
Top Bottom