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: - Private Sub OkCal_Click()
-
-
target = Me.Calendar9.Value
-
target.SetFocus
-
DoCmd.Close , , acSaveNo
-
' [what goes here?]
-
-
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.
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.
-
Public Sub OpenCalendar(ByRef strSQL As String, ByRef txtDate As TextBox)
-
-
Dim frmCalendar As Form
-
-
If IsNull(txtDate) Then
-
txtDate.Value = Date
-
Else
-
DoCmd.OpenForm "frmCalendar"
-
Set frmCalendar = Forms!frmCalendar
-
With frmCalendar
-
.RecordSource = strSQL
-
.Controls!axcCalendar.ControlSource = txtDate.ControlSource
-
End With
-
Set frmCalendar = Nothing
-
End If
-
-
End Sub
-
- 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. -
Private Sub dteDate_DblClick(Cancel As Integer)
-
OpenCalendar "SELECT dteDate FROM tblTable WHERE keyID=" _
-
& Me.keyID & ";", Me.dteDate
-
End Sub
-
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.
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.
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. =(
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 -
-
'Global object of class CalendarField declared with events
-
Dim WithEvents objDate As CalendarField
-
-
Private Sub Form_Open(Cancel As Integer)
-
Set objDate = New CalendarField
-
Set objDate.TextField = Me.dteDate
-
End Sub
-
-
Private Sub Form_Close()
-
Set objDate = Nothing
-
End Sub
-
-
'let us say the calendar form is opened at DblClick on a date textbox
-
'BTW the following proc is actually not needed as soon as CalendarField class
-
'can itself catch events of the textbox passed to it via TextField property
-
Private Sub dteDate_DblClick
-
objDate.Show
-
End Sub
-
-
Private Sub objDate_AfterUpdate
-
'here you can place any code you like
-
End Sub
-
-
Does this make a sense?
Events:
AfterUpdate - event raising when the TextField was updated, here you can put code you needed -
Private Sub objDate_AfterUpdate
-
'here you can place any code you like
-
End Sub
-
-
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?
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: -
Event DateSubmitted(ByVal dteInput As Date)
-
-
Private Sub btnOk_Click()
-
RaiseEvent DateSubmitted(Me.axcCalendar.Value)
-
DoCmd.Close
-
End Sub
-
[frmSomeForm] module: -
Dim WithEvents frmCal As Form_frmCalendar
-
-
Private Sub btnOpenCalendar_Click()
-
DoCmd.OpenForm "frmCalendar"
-
Set frmCal = Forms!frmCalendar
-
frmCal.Modal = True
-
End Sub
-
-
Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
-
MsgBox dteInput
-
Set frmCal = Nothing
-
End Sub
-
Good luck.
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: - Option Compare Database
-
Option Explicit
-
-
Public target As TextBox
-
Public formName As Form
-
-
Public Function linkCal(place As TextBox, inForm As Form, Optional identify As String)
-
Set target = place
-
Set formName = inForm
-
DoCmd.OpenForm "Calendar", windowmode:=acDialog, OpenArgs:=identify
-
End Function
Then in the calendar form itself: - Private Sub OkCal_Click()
-
target = Me.Calendar9.Value
-
-
target.SetFocus
-
DoCmd.Close , , acSaveNo
-
End Sub
Why isn't this tripping AfterUpdate after it fills in a date?
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
- ? 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: -
' declare event just like procedure
-
Event DateSubmitted(ByVal dteInput As Date)
-
-
Private Sub btnOk_Click()
-
' fire event when [Ok] button was clicked
-
RaiseEvent DateSubmitted(Me.axcCalendar.Value)
-
DoCmd.Close
-
End Sub
-
- now you can open ObjectBrowser and see that Form_frmCalendar class has event DateSubmitted
[frmSomeForm] module: -
' to handle the Form_frmCalendar event we need to declare global variable
-
' in the following manner
-
Dim WithEvents frmCal As Form_frmCalendar
-
' now friendly VBA IDE allows you to choose frmCal from combobox
-
' where all other event sources (Form, controls) are located
-
-
Private Sub btnOpenCalendar_Click()
-
'opens calendar form in normal window mode to let the code execution to proceed
-
DoCmd.OpenForm "frmCalendar"
-
' set reference to opened form, from now frmCal will trap events fired in frmCalendar
-
Set frmCal = Forms!frmCalendar
-
' set frmCalendar window mode to Dialog
-
frmCal.Modal = True
-
End Sub
-
-
' this is the handler of DateSubmitted event
-
' you can write here your useful code
-
Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
-
MsgBox dteInput
-
Set frmCal = Nothing
-
End Sub
-
Hope this makes sence.
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!
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: -
Private txbTargetTextBox As Access.TextBox
-
-
Event DateSubmitted(ByVal dteInput As Date)
-
-
Private Sub btnOk_Click()
-
Me.TargetTextBox.Value = Me.axcCalendar.Value
-
RaiseEvent DateSubmitted(Me.axcCalendar.Value)
-
DoCmd.Close
-
End Sub
-
-
Public Property Get TargetTextBox() As Access.TextBox
-
Set TargetTextBox = txbTargetTextBox
-
End Property
-
-
Public Property Set TargetTextBox(ByRef txbNewValue As Access.TextBox)
-
Set txbTargetTextBox = txbNewValue
-
End Property
-
-
Private Sub Form_Close()
-
Set txbTargetTextBox = Nothing
-
End Sub
-
[frmSomeForm] module:
NB: note frmCal object is declared as private unlike the previous version to avoid accidental referencing from other module -
Private WithEvents frmCal As Form_frmCalendar
-
-
Private Sub dteDate1_DblClick(Cancel As Integer)
-
Set frmCal = ShowCalendar(Me.dteDate1)
-
End Sub
-
-
Private Sub dteDate2_DblClick(Cancel As Integer)
-
Set frmCal = ShowCalendar(Me.dteDate2)
-
End Sub
-
-
Private Sub frmCal_DateSubmitted(ByVal dteInput As Date)
-
MsgBox "Date put to " & frmCal.TargetTextBox.Name
-
Set frmCal = Nothing
-
End Sub
-
Some public module (let us say [mdlCalendar]): -
Public Function ShowCalendar(ByRef txbTargetTextBox As Access.TextBox) _
-
As Form_frmCalendar
-
-
DoCmd.OpenForm "frmCalendar"
-
Set ShowCalendar = Forms!frmCalendar
-
With ShowCalendar
-
Set .TargetTextBox = txbTargetTextBox
-
.Modal = True
-
End With
-
-
End Function
-
I hope this will cover all your needs.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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'...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |