473,466 Members | 4,879 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling Subroutines across Forms (non-module)

48 New Member
Really, i've looked for the answer, but I must be missing some syntax somewhere...

I have a popup calendar which is to be used across several forms. Each form will need the calendar to automatically enter the selected date into the designated textbox (this part works beautifully already) and then update the filter on a continuous form to include that date range.

Now, I have several forms which will be in need of this popup calendar, so I'm trying to make it as general as possible so it can handle all of them. I set things up with a module, allowing for the following global variables:

Public target As TextBox
Public formName As Form

In the calendar form, I have the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub OkCal_Click()
  2.  
  3.     target = Me.Calendar9.Value
  4.     target.SetFocus
  5.     DoCmd.Close , , acSaveNo
  6.     ' [what goes here?]
  7.  
  8. End Sub
So this clearly isn't right, but hopefully it will give you a sense of what I want to do in the [what goes here] space:
Call formName.afterCalendarEvents()
also doesn't work: Call formName.Form.afterCalendarEvents()

Each of several forms that will be using this popup calendar will need to have different "afterCalendarEvents" actions, and so I don't feel I can put it as a module. I'd like to try to access individual "afterCalendarEvents" subs in each individual form.

Does this make sense? What am I missing here? Any help is greatly appreciated. Thank you in advance.
Aug 22 '07 #1
11 4478
FishVal
2,653 Recognized Expert Specialist
Hi, isoquin.

Here is one possible solution. When user dblclicks on a textbox it is being filled with current date when it is empty or otherwise calendar popup is opened .
  • "frmCalendar" - form where calendar control named "axcCalendar" is placed.
  • Procedure below is placed in public module.
    Expand|Select|Wrap|Line Numbers
    1. Public Sub OpenCalendar(ByRef strSQL As String, ByRef txtDate As TextBox)
    2.  
    3.     Dim frmCalendar As Form
    4.  
    5.     If IsNull(txtDate) Then
    6.         txtDate.Value = Date
    7.     Else
    8.         DoCmd.OpenForm "frmCalendar"
    9.         Set frmCalendar = Forms!frmCalendar
    10.         With frmCalendar
    11.             .RecordSource = strSQL
    12.             .Controls!axcCalendar.ControlSource = txtDate.ControlSource
    13.         End With
    14.         Set frmCalendar = Nothing
    15.     End If
    16.  
    17. End Sub
    18.  
  • Now in a form.
    • Form.RecordSource = tblTable
    • dteDate.ControlSource = tblTable.dteDate
    • keyID is tblTable PK(Numeric)
    First argument passed into "OpenCalendar" function is SQL statement retrieving single record (filtered by PK) with one single field.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub dteDate_DblClick(Cancel As Integer)
    2.     OpenCalendar "SELECT dteDate FROM tblTable WHERE keyID=" _
    3.          & Me.keyID & ";", Me.dteDate
    4. End Sub
    5.  
Aug 22 '07 #2
isoquin
48 New Member
I don't quite think I explained well enough. I have most of that already. My calendar pops up, can take a date, places the date in the right place. Now, after the date is placed, I need the original form to run a subroutine.

Given the fact that I'm storing the originating form in a global module variable, how do I call the sub I need from that form automatically after the date is placed? There are more things needed past just record selection after this event. Unfortunately, none of the On Event possibilities seem to activate after the date is placed in the textbox.
Aug 23 '07 #3
FishVal
2,653 Recognized Expert Specialist
I don't quite think I explained well enough. I have most of that already. My calendar pops up, can take a date, places the date in the right place. Now, after the date is placed, I need the original form to run a subroutine.

Given the fact that I'm storing the originating form in a global module variable, how do I call the sub I need from that form automatically after the date is placed? There are more things needed past just record selection after this event. Unfortunately, none of the On Event possibilities seem to activate after the date is placed in the textbox.
Hi, there.

The most natural and commom way to do it, to my mind, is to write Class that raises events.
Then to instantiate in form module object of the class and handle it's events.

Hope you have some sence of object oriented programming. If no, start to learn it with this rather simple class.

Good luck.

P.S. I'll be glad to help you with coding.
Aug 23 '07 #4
isoquin
48 New Member
Sorry, I think I missed something there. What rather simple class? Is there a link I'm missing? So confused...

At the moment, I added another button entitled "Submit" which the user can click to manually call the sub. It is, however, an unecesary extra step, as it should just run after the date is placed. -_-

I'm familiar with object oriented coding from back when I took a java Data Structures course, but VBA seems to make most of my programming knowledge useless. =(
Aug 27 '07 #5
FishVal
2,653 Recognized Expert Specialist
Sorry, I think I missed something there. What rather simple class? Is there a link I'm missing? So confused...

At the moment, I added another button entitled "Submit" which the user can click to manually call the sub. It is, however, an unecesary extra step, as it should just run after the date is placed. -_-

I'm familiar with object oriented coding from back when I took a java Data Structures course, but VBA seems to make most of my programming knowledge useless. =(
Hi, isoquin.

Maybe I'm missing something.
You can call public function/sub in any module as well as form module too using the following syntax:

Call ModuleName.ProcName ....
or simply
ModuleName.ProcName ....

But you say that you want a universal code.
I guess this means you want that a procedure specific for a particular Textfield will be invoked as soon as the Textfield will be updated with Calendar form.

To my mind the most natural way to do it is to write some class implementing at least

Properties:
Textfield - target textfield r/w

Methods:
Show - opens calendar form

Events:
AfterUpdate - event raising when the TextField was updated, here you can put code you needed

Let us say it's name is "CalendarField".

Now in form module you can write something like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. 'Global object of class CalendarField declared with events
  3. Dim WithEvents objDate As CalendarField
  4.  
  5. Private Sub Form_Open(Cancel As Integer)
  6.        Set objDate = New CalendarField
  7.        Set objDate.TextField = Me.dteDate
  8. End Sub
  9.  
  10. Private Sub Form_Close()
  11.        Set objDate = Nothing
  12. End Sub
  13.  
  14. 'let us say the calendar form is opened at DblClick on a date textbox
  15. 'BTW the following proc is actually not needed as soon as CalendarField class
  16. 'can itself catch events of the textbox passed to it via TextField property
  17. Private Sub dteDate_DblClick
  18.        objDate.Show
  19. End Sub
  20.  
  21. Private Sub objDate_AfterUpdate
  22.        'here you can place any code you like
  23. End Sub
  24.  
  25.  
Does this make a sense?
Aug 27 '07 #6
isoquin
48 New Member
Events:
AfterUpdate - event raising when the TextField was updated, here you can put code you needed


Expand|Select|Wrap|Line Numbers
  1. Private Sub objDate_AfterUpdate
  2.        'here you can place any code you like
  3. End Sub
  4.  
  5.  
Does this make a sense?
Hey Fishval-

Yes it makes sense. It makes so much sense that it was the first thing I tried, but to no avail. It seems that after my popup calendar places the date I want in the textbox I want, the AfterUpdate event is *not* triggered. Rather annoying, to say the least. So, I"m trying to figure out how to trigger it, either by doing something differently, or by having my calendar module find a way to call the AfterUpdate subroutine directly, given the form of interest (keep in mind this calendar is used across several forms).

ideas?
Sep 5 '07 #7
FishVal
2,653 Recognized Expert Specialist
Hey Fishval-

Yes it makes sense. It makes so much sense that it was the first thing I tried, but to no avail. It seems that after my popup calendar places the date I want in the textbox I want, the AfterUpdate event is *not* triggered. Rather annoying, to say the least. So, I"m trying to figure out how to trigger it, either by doing something differently, or by having my calendar module find a way to call the AfterUpdate subroutine directly, given the form of interest (keep in mind this calendar is used across several forms).

ideas?
Hi, isoquin.

Not sure we are talking about the same, but let this RIP. ;)
Since my last post I've figured simple and nice solution, just was waiting for you answer to make sure you are still interested.

Let us say you have some form (let us say [frmSomeForm]) which opens calendar form (let us say [frmCalendar]).

[frmCalendar] module:
Expand|Select|Wrap|Line Numbers
  1. Event DateSubmitted(ByVal dteInput As Date)
  2.  
  3. Private Sub btnOk_Click()
  4.     RaiseEvent DateSubmitted(Me.axcCalendar.Value)
  5.     DoCmd.Close
  6. End Sub
  7.  
[frmSomeForm] module:
Expand|Select|Wrap|Line Numbers
  1. Dim WithEvents frmCal As Form_frmCalendar
  2.  
  3. Private Sub btnOpenCalendar_Click()
  4.     DoCmd.OpenForm "frmCalendar"
  5.     Set frmCal = Forms!frmCalendar
  6.     frmCal.Modal = True
  7. End Sub
  8.  
  9. Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
  10.     MsgBox dteInput
  11.     Set frmCal = Nothing
  12. End Sub
  13.  
Good luck.
Sep 5 '07 #8
isoquin
48 New Member
I don't think I'm following how that will trigger the AfterUpdate function. Sorry for being a bit slow, but could you please explain the code? If it helps at all, I can provide my own:


frmSomeForm has textBox named "SomeText" where
onClick: =linkCal([SomeText],Forms!frmSomeForm,"Start Date")

Complementary, I have a module "getDate" with the following:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public target As TextBox
  5. Public formName As Form
  6.  
  7. Public Function linkCal(place As TextBox, inForm As Form, Optional identify As String)
  8.     Set target = place
  9.     Set formName = inForm
  10.     DoCmd.OpenForm "Calendar", windowmode:=acDialog, OpenArgs:=identify
  11. End Function
Then in the calendar form itself:
Expand|Select|Wrap|Line Numbers
  1. Private Sub OkCal_Click()
  2.     target = Me.Calendar9.Value
  3.  
  4.     target.SetFocus
  5.     DoCmd.Close , , acSaveNo
  6. End Sub
Why isn't this tripping AfterUpdate after it fills in a date?
Sep 5 '07 #9
FishVal
2,653 Recognized Expert Specialist
Hi, isoquin.

I'll try to explain how does the code works.
  • First of all I'd like to point out that form modules are actually class modules. You can easily check that opening ObjectBrowser and seeing (in bold BTW ;)) classes like Form_<form name>. BTW object of [Forms] collection are instances of these classes. You can easily check that to:
    • open form e.g. [frmCalendar]
    • in VBA immediate window enter
      Expand|Select|Wrap|Line Numbers
      1. ? typename(Forms!frmCalendar)
    • you will receive Form_frmCalendar
  • Having clarified this, we should ask ourselves: why not to take advantage of OOP and to write our own Properties/Methods/Events for this class?
  • The code below shows how to declare event for particular form class

    [frmCalendar] module:
    Expand|Select|Wrap|Line Numbers
    1. ' declare event just like procedure
    2. Event DateSubmitted(ByVal dteInput As Date)
    3.  
    4. Private Sub btnOk_Click()
    5.     ' fire event when [Ok] button was clicked
    6.     RaiseEvent DateSubmitted(Me.axcCalendar.Value)
    7.     DoCmd.Close
    8. End Sub
    9.  
  • now you can open ObjectBrowser and see that Form_frmCalendar class has event DateSubmitted


    [frmSomeForm] module:
    Expand|Select|Wrap|Line Numbers
    1. ' to handle the Form_frmCalendar event we need to declare global variable
    2. ' in the following manner
    3. Dim WithEvents frmCal As Form_frmCalendar
    4. ' now friendly VBA IDE allows you to choose frmCal from combobox
    5. ' where all other event sources (Form, controls) are located 
    6.  
    7. Private Sub btnOpenCalendar_Click()
    8.     'opens calendar form in normal window mode to let the code execution to proceed
    9.     DoCmd.OpenForm "frmCalendar"
    10.     ' set reference to opened form, from now frmCal will trap events fired in frmCalendar
    11.     Set frmCal = Forms!frmCalendar
    12.     ' set frmCalendar window mode to Dialog
    13.     frmCal.Modal = True
    14. End Sub
    15.  
    16. ' this is the handler of DateSubmitted event
    17. ' you can write here your useful code
    18. Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
    19.     MsgBox dteInput
    20.     Set frmCal = Nothing
    21. End Sub
    22.  

Hope this makes sence.
Sep 5 '07 #10
isoquin
48 New Member
ah yes I see! getting much closer, but still a bit off.

so the form can snag the calendar value, AND I have free reign as to events afterwards now. However, I'm still shaky with OOP and getting the value into the right textbox (as there is both a start and end date box).

Can I just set a global variable Dim txtPlace as TextBox, and then just set it to my place of interest when the button for that box is clicked? In other words, in startDate_Click() have:
txtPlace = "myStartDateTextBox"
and then on endDate_Click() have
txtPlace = "myEndDateTextBox"
?

Is there an easier way or does that reasoning sound ok? Thanks for your expertise and all your willingness to help!
Sep 6 '07 #11
FishVal
2,653 Recognized Expert Specialist
ah yes I see! getting much closer, but still a bit off.

so the form can snag the calendar value, AND I have free reign as to events afterwards now. However, I'm still shaky with OOP and getting the value into the right textbox (as there is both a start and end date box).

Can I just set a global variable Dim txtPlace as TextBox, and then just set it to my place of interest when the button for that box is clicked? In other words, in startDate_Click() have:
txtPlace = "myStartDateTextBox"
and then on endDate_Click() have
txtPlace = "myEndDateTextBox"
?

Is there an easier way or does that reasoning sound ok? Thanks for your expertise and all your willingness to help!
Hi, isoquin.

This is the question of programming style.
Personally I would prefer to avoid global variable. Despite it seems to be the most straightforward solution it can strike back in a pretty unexpected manner. ;)

So I will proceed in terms of OOP. :)
Instead of interrogation through global variable lets pass reference to a target TextBox to frmCalendar via property.
Code looks like this

[frmCalendar] module:
Expand|Select|Wrap|Line Numbers
  1. Private txbTargetTextBox As Access.TextBox
  2.  
  3. Event DateSubmitted(ByVal dteInput As Date)
  4.  
  5. Private Sub btnOk_Click()
  6.     Me.TargetTextBox.Value = Me.axcCalendar.Value
  7.     RaiseEvent DateSubmitted(Me.axcCalendar.Value)
  8.     DoCmd.Close
  9. End Sub
  10.  
  11. Public Property Get TargetTextBox() As Access.TextBox
  12.     Set TargetTextBox = txbTargetTextBox
  13. End Property
  14.  
  15. Public Property Set TargetTextBox(ByRef txbNewValue As Access.TextBox)
  16.     Set txbTargetTextBox = txbNewValue
  17. End Property
  18.  
  19. Private Sub Form_Close()
  20.     Set txbTargetTextBox = Nothing
  21. End Sub
  22.  
[frmSomeForm] module:
NB: note frmCal object is declared as private unlike the previous version to avoid accidental referencing from other module
Expand|Select|Wrap|Line Numbers
  1. Private WithEvents frmCal As Form_frmCalendar
  2.  
  3. Private Sub dteDate1_DblClick(Cancel As Integer)
  4.     Set frmCal = ShowCalendar(Me.dteDate1)
  5. End Sub
  6.  
  7. Private Sub dteDate2_DblClick(Cancel As Integer)
  8.     Set frmCal = ShowCalendar(Me.dteDate2)
  9. End Sub
  10.  
  11. Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
  12.     MsgBox "Date put to " & frmCal.TargetTextBox.Name
  13.     Set frmCal = Nothing
  14. End Sub
  15.  
Some public module (let us say [mdlCalendar]):
Expand|Select|Wrap|Line Numbers
  1. Public Function ShowCalendar(ByRef txbTargetTextBox As Access.TextBox) _
  2.     As Form_frmCalendar
  3.  
  4.     DoCmd.OpenForm "frmCalendar"
  5.     Set ShowCalendar = Forms!frmCalendar
  6.     With ShowCalendar
  7.         Set .TargetTextBox = txbTargetTextBox
  8.         .Modal = True
  9.     End With
  10.  
  11. End Function
  12.  
I hope this will cover all your needs.
Sep 6 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Steven Bethard | last post by:
I'm playing around with some threading stuff right now, and I'm having a little trouble calling a function from one thread that affects another. Here's my setup: py> import os, threading, time...
2
by: Evan | last post by:
Hi, What is the best method to share objects (such as a SqlConnection) across many forms. I am currently passing the SqlConnection in to other forms from the main form through the other forms'...
2
by: Denis_dh | last post by:
Hi, I have a set of Forms and wish to have a varible used by more than one of them but can't seem to figure where to initialise it or how to acess it if I initialise it in a different form...
0
by: Rich | last post by:
I've got a tabcontrol with a number of databound textboxes on a tabpage (Form1). I've got a button that the user clicks that opens a dialog (Form2) and does a lookup from a database. The user...
7
by: gordon | last post by:
Hi, I have a series of forms that collect information from a user. When I write out my information to a text file after the last file, the values of the previous forms seem to be blank. I would...
1
devonknows
by: devonknows | last post by:
Hi, im having trouble carrying variables across a form, ive looked on here and other sites but cant find anything that helps me, or i might not be searching for the right terms, so i though i would...
8
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below....
11
by: isoquin | last post by:
I have a gigantic database that I had to split across two tables (I'd normally use one, but it hits the max number of items in a table). I built input forms for data entry, which are similarly split...
7
by: mirandacascade | last post by:
Note: I'm not sure if the subject line of this post uses the correct terminology, so apologies if the subject line turns out to be misleading. I think it's probably easier to provide a trivial...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.