Link to home
Create AccountLog in
Avatar of Dustin Stanley
Dustin Stanley

asked on

MS Access Refer To Form With String Value

I am needing to use the same Code for multiple situations but I can not figure out one part. Here is a example:

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'LETS SEE WHICH CHECKOUT FORM IS OPEN SO WE CAN FILL IT OUT PROPERLY
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Dim CheckoutFormName As String
 Dim Receipt As String
 
If CurrentProject.AllForms("frmNewOrdersGuestCheckout").IsLoaded = True Then ' Guest Checkout Form is Open
CheckoutFormName = "frmNewOrdersGuestCheckout"
Receipt = "RptqryGuestCheckoutReceipt"
End If

If CurrentProject.AllForms("frmNewOrder").IsLoaded = True Then   ' Customer with account Form is Open
CheckoutFormName = "frmNewOrder"
Receipt = "RptqryCheckoutReceipt"
End If

Forms!CheckoutFormName!OrderStatusID = "7"

Open in new window



Now for the line Forms!CheckoutFormName!OrderStatusID = "7" how would I set it up to get Access to find and accept the the String (CheckoutFormName) as a form?

Thank you for the help.
ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
I don't know what the calling point of your code is. But note that you can pass a form object to your code, to ensure that you always have the right reference.
E.g.
Call SomeCode(me)

Public sub SomeCode(frm as form)
  'Some other stuff might go here
  frm!OrderStatusID!value=7
end Sub

Open in new window

Avatar of Dustin Stanley
Dustin Stanley

ASKER

Dale that works Perfect!

Anders Can you please explain better. The form that is open sometimes is different (Varying). So how can calling to another sub be beneficial?

Wouldn't I have to have a varying call if that makes sense.....
What is the starting point of your code? What triggers it? What scenario are you trying to solve. Then I could better put the explanation of the technique in context of your situation.
Dustin,

Sometimes it is advantageous to send a form reference to a subroutine of function.  a simple function would be something like:

Public Function FormControlCount(frm as form) as integer

    FormControlCount = frm.controls.Count

End Function

Open in new window

This is not a very useful example, but gives you an idea of what Anders is talking about.

Dale
Ok I still don't understand.

In my example:
Dim CheckoutFormName As String
 Dim Receipt As String
 
If CurrentProject.AllForms("frmNewOrdersGuestCheckout").IsLoaded = True Then ' Guest Checkout Form is Open
CheckoutFormName = "frmNewOrdersGuestCheckout"
Receipt = "RptqryGuestCheckoutReceipt"
End If

If CurrentProject.AllForms("frmNewOrder").IsLoaded = True Then   ' Customer with account Form is Open
CheckoutFormName = "frmNewOrder"
Receipt = "RptqryCheckoutReceipt"
End If

Forms!CheckoutFormName!OrderStatusID = "7"

Open in new window



If frmNewOrdersGuestCheckout is open then wouldn't I need a sub that would say:
Forms!frmNewOrdersGuestCheckout !OrderStatusID = "7"

Open in new window



If frmNewOrder is open then wouldn't I need a sub that would say:
Forms!frmNewOrder!OrderStatusID = "7"

Open in new window



Wouldn't that defeat the purpose?????
Thank you both for the help. I am interested in finding out if there is more to the sub that I understand. Love learning.