Automate MS access Testing - Call button click event code written on MS access Form in another access file (1 Viewer)

ash1983

New member
Local time
Today, 20:02
Joined
Sep 6, 2023
Messages
5
Is it possible to call the code written behind the MS access Form button of another access file?

If Yes, please share the code snippet how to call it?
Thanks in advance!!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:02
Joined
Feb 28, 2001
Messages
27,337
Look at this for the "event behind another form's control" portion.


The trick is that you have to open the other database (see "OpenDatabase") and make an object variable for it so that you provide a properly qualified context. An "unqualified" reference always points to the current database (see CurrentDB). DBOther.AllForms might be how you actually touch the form in question.


THEN in that second database, you have open the form in question, because that is how you load the form (including loading the code). If you don't load the form (by opening it), you cannot touch the code at all because it is buried somewhere on the disk in a virtual (non-resident) area.

BUT if the button's event code isn't PUBLIC in this OTHER database, you might not be able to get to it anyway since by default, control event subs are PRIVATE and thus cannot be referenced from an external context. So anything you were consider for your testing would have to be marked specially as PUBLIC.

Before you ask why every event is private by default, I'll tell you. It is because events in forms generated by relatively normal methods will share a lot of the same names - like Form_Load or Form_Current or Form_BeforeUpdate - and thus having PUBLIC names would lead to a name conflict that would ALSO require an object variable for each such opened form to be able to tell the forms apart.

I'm not going to say you can't do this. There is just a LOT of preparation in anticipation of doing such a thing, and you would have to be meticulous in your coding conventions to keep from confusing anyone who might look at this code. (That would include you yourself if you wrote it and a year later had to go in to debug something.)
 

ash1983

New member
Local time
Today, 20:02
Joined
Sep 6, 2023
Messages
5
Can you help me how can i call/refer a public function/Sub of another DB? Please share the syntax
I am able to refer the controls and related properties but calling function/sub not working.

appAccess.Forms("frmSearchIncident").controls(5).caption
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:02
Joined
May 7, 2009
Messages
19,246
I am not sure, but i am thinking you can only Call public subs on a Module?
 

Josef P.

Well-known member
Local time
Today, 12:02
Joined
Feb 2, 2023
Messages
848
Call a public method from a class (form) instance:
Code:
FormRef.ProcedureName Arg1, Arg2, ...
or
CallByName FormRef, "ProcedureName", VbMethod, Arg1, Arg2, ...

Call a public procedure from a standard codemodule:
Code:
ApplicationReference.Run "ProcedureName", Arg1, Arg2, ...

/edit:
You could make the event handler procedure public. But I think this is bad programming style.
It is better to call a public method from the event handler.
Code:
Public Sub YourButton_Click()
   Msgbox "I was pressed"
end Sub
vs.
Code:
private Sub YourButton_Click()
   ShowClickMessage
end Sub

Public Sub ShowClickMessage()
       Msgbox "I was pressed"
end Sub
=> ShowClickMessage can be reached from "outside" with: ReferenceToForm.ShowClickMessage
 
Last edited:

Josef P.

Well-known member
Local time
Today, 12:02
Joined
Feb 2, 2023
Messages
848
Another possibility (but this one may not be as stable):
Code:
Private Const WM_KEYDOWN  As Long = &H100
Private Const WM_KEYUP  As Long = &H101
Private Const VK_RETURN As Long = &HD
Private Const VK_SPACE As Long = &H20

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Test()

    DoCmd.OpenForm "TestForm"
    PressButton Forms("TestForm"), "cmdTest"
 
End Sub

Public Sub PressButton(ByVal frm As Form, ByVal ButtonName As String)

'1. set focus to commandbutton
    frm.Controls(ButtonName).SetFocus

'2. press return or space key:
    SendMessage frm.Hwnd, WM_KEYDOWN, VK_SPACE, 0
    SendMessage frm.Hwnd, WM_KEYUP, VK_SPACE, 65539

End Sub
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:02
Joined
Feb 19, 2002
Messages
43,515
Is it possible to call the code written behind the MS access Form button of another access file?

If Yes, please share the code snippet how to call it?
Thanks in advance!!
This is a pretty scary process. Perhaps there is a better solution. Tell us in words why the process is in a different database. There are ways to create add-ins so you can reuse code rather than duplicating it. Maybe this would be a safer approach.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:02
Joined
Feb 28, 2001
Messages
27,337
Pat, the answer to your question is in the OP's title... Automate MS Access Testing.

OK, just for clarity: @ash1983 - for an arbitrary and unmodified "other" database, you cannot do this with Access. Period.

For an "other" database that you are modifying specifically for testing, you can edit the form and report event entry points to make them PUBLIC rather than PRIVATE. When doing so, watch out for name overlaps, which would cause confusion by having multiple matching names within the same scope of reference. Some events will absolutely not be testable because of name overlaps that cannot practically be made PUBLIC in a database in which you plan to open forms for examination.
 

amorosik

Member
Local time
Today, 12:02
Joined
Apr 18, 2020
Messages
397
It should be remembered that the program to be started (or automated) could also be already running
And therefore I believe should be distinguished from the first which involves starting the program instance and sending the command
 

Josef P.

Well-known member
Local time
Today, 12:02
Joined
Feb 2, 2023
Messages
848
I create the test classes directly in the application to be tested so that I can also test private classes.
Before delivery, the test classes are exported and removed from the application. This is done automatically via an add-in.

If you test the application from another file, you can only test what is startable via the application reference.
Alternatively it would be to be considered whether it is simpler to bound the application which can be tested as reference into the test application, in order to receive also access to the (public) classes. For the creation then a factory would be necessary - or one changes the class to public + createable.
Testing in the respective application is easier. ;)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:02
Joined
Feb 28, 2001
Messages
27,337
Right, lost my mind. It's such a small thing. So easy to misplace.

I always worry that I've left it in my exercise shorts when my clothes get washed. Worried even more that it will shrink.
 

Users who are viewing this thread

Top Bottom