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.
LVL 1
Dustin StanleyEntrepreneurAsked:
Who is Participating?
 
Dale FyeConnect With a Mentor Commented:
Forms(strFormName).OrderStatusID
0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
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

0
 
Dustin StanleyEntrepreneurAuthor Commented:
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.....
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
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.
0
 
Dale FyeCommented:
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
0
 
Dustin StanleyEntrepreneurAuthor Commented:
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?????
0
 
Dustin StanleyEntrepreneurAuthor Commented:
Thank you both for the help. I am interested in finding out if there is more to the sub that I understand. Love learning.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.