form name dim

D

David

This is an example of what I am trying to do

Sub Client_LostFocus()
Dim formname As Variant
formname = Fuel_Ticket
MsgBox Forms(the).Controls("Client")

End Sub

I cannot seem to figure out what should go in the place of Variant here.
It doesn't work if formname is a string. What should it be?

Thanks
David
 
D

David

Thank you for being prompt, though, Im afraid my question still stands.
this code:

Sub Client_LostFocus()
Dim formname As String
formname = "Fuel_Ticket"
MsgBox Forms(formname).Controls("Client")

End Sub

did not work
however when I changed it to:

Sub Client_LostFocus()
Dim formname As Variant
formname = Fuel_Ticket
MsgBox Forms(formname).Controls("Client")

End Sub

it worked. Indicating (i think) that it is not a string that it needs but
something else.
Please ignore that it seems to be using it inside the form and I could just
say Me.Client, i am just using that as an example

thanks
David
 
M

Marshall Barton

David said:
This is an example of what I am trying to do

Sub Client_LostFocus()
Dim formname As Variant
formname = Fuel_Ticket
MsgBox Forms(the).Controls("Client")

End Sub

I cannot seem to figure out what should go in the place of Variant here.
It doesn't work if formname is a string. What should it be?


I should be a String, but Variant will probably work. What
is essential is that the next line use quotes around the
name or calculate the name:

Assuming you formname to be Fuel_Ticket:
formname = "Fuel_Ticket"

OTOH, if formname is supposed to me the name of the form
that contains this code, you should use:
formname = Me.Name

All that should be fairly self evident if you compile (Debug
menu) your project. Compiling will immediately identify an
unquotes string as an undeclared vaiable. If it did not,
then you really need to add OPTION EXPLICIT as the first
line at the top of the (and every) module.
 
M

Marshall Barton

David said:
Thank you for being prompt, though, Im afraid my question still stands.
this code:

Sub Client_LostFocus()
Dim formname As String
formname = "Fuel_Ticket"
MsgBox Forms(formname).Controls("Client")

End Sub

did not work
however when I changed it to:

Sub Client_LostFocus()
Dim formname As Variant
formname = Fuel_Ticket
MsgBox Forms(formname).Controls("Client")

End Sub

it worked. Indicating (i think) that it is not a string that it needs but
something else.
Please ignore that it seems to be using it inside the form and I could just
say Me.Client, i am just using that as an example


I don't see how formname = Fuel_Ticket can work unless
Fuel_Ticket is the name of module or global variable that
contains the name you want. As I said earlier using Variant
should not make a difference other than to allow setting
formname to Null when Fuel_Ticket in an undeclared variable.

I can not overemphasize the importance of using OPTION
EXPLICIT to avoid that problem.

Another thing you must be aware of is that Forms(formname)
will only work if the form object named in formname is an
open form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top