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.
Microsoft AccessVBA

Avatar of undefined
Last Comment
Dustin Stanley

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dale Fye

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Anders Ebro (Microsoft MVP)

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

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.....
Anders Ebro (Microsoft MVP)

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Dale Fye

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
Dustin Stanley

ASKER
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?????
Dustin Stanley

ASKER
Thank you both for the help. I am interested in finding out if there is more to the sub that I understand. Love learning.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.