Calendars

R

richard

Have been looking at another post and am struggling to get the calendar to
put the date selected into the field after I have clicked the date.
Below is the code I am using, all that is returned to the field is todays date

Private Sub Date_Sample_Taken_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
Calendar5.Visible = True
Calendar5.SetFocus
If Not IsNull(Date_Sample_Taken) Then
Calendar5.Value = Date_Sample_Taken.Value
Else
Calendar5.Value = Date
End If
End Sub

Then when the calendar appears I select the appropriate date and the
following code is then run

Private Sub Calendar5_Click()
Calendar5.Value = Date
Date_Sample_Taken.SetFocus
Calendar5.Visible = False
End Sub

the Calendar closes but the date selected does not appear in the form field,
just the current date.

Thanks

Richard
 
K

Ken Sheridan

Richard:

You don't seem to be assigning the value of the calendar control to the
Date_Sample_Taken control. Instead you are setting the calendar control's
value to the return value of the Date function. I think you want this:

Private Sub Calendar5_Click()

Date_Sample_Taken = Calendar5.Value
Date_Sample_Taken.SetFocus
Calendar5.Visible = False

End Sub

You might be interested in the approach I use which puts the calendar
control (ocxCalendar) on a separate form (frmCalendar), which is opened, set
to the current date, by double clicking the date control (called txtDate in
the following example) on the bound form. The code to open the calendar form
is:

Private Sub txtDate_DblClick(Cancel As Integer)

If Not OpenCalendar(Me.Name, "txtDate") Then
MsgBox "Unable to open calendar", vbExclamation, "Error"
End If

End Sub

The code for the frmCalendar form's module is:

Option Compare Database
Option Explicit

Private Sub Form_Close()

Dim intSlashPos As Integer
Dim strForm As String, strControl As String

If Len(Me.OpenArgs) > 0 Then
intSlashPos = InStr(1, Me.OpenArgs, "/")
strForm = Left$(Me.OpenArgs, intSlashPos - 1)
strControl = Mid$(Me.OpenArgs, intSlashPos + 1)
Forms(strForm).Controls(strControl) = ocxCalendar.Value
End If

End Sub


Private Sub Form_Load()

Me!ocxCalendar.Today

End Sub

Private Sub ocxCalendar_Click()

DoCmd.Close acForm, Me.Name

End Sub

And the following function is in a standard module in the database:

Public Function OpenCalendar(strCurrentForm As String, strCurrentControl As
String) As Integer

On Error GoTo ErrHandler

OpenCalendar = True

DoCmd.OpenForm "frmCalendar", _
WindowMode:=acDialog, _
OpenArgs:=strCurrentForm & "/" & strCurrentControl

ExitHere:
Exit Function

ErrHandler:
OpenCalendar = False
Resume ExitHere:

End Function

The function, by passing the names of the calling form and control to the
calendar form as the OpenArgs property, allows the calendar form to be called
from any appropriate control on any form.

Ken Sheridan
Stafford, England
 
R

richard

Ken

Made the changes you suggest below but still not working, have tried the
control with and without [ ] to cope with space between words but no success.
I have the same code working in another form, both are below, could you see
if you can see any differences as I can't

Working Code

Private Sub Analysis_Date_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)
Calendar7.Visible = True
Calendar7.SetFocus
If Not IsNull(Analysis_Date) Then
Calendar7.Value = Analysis_Date.Value
Else
Calendar7.Value = Date
End If
End Sub

Private Sub Calendar7_Click()
Analysis_Date.Value = Calendar7.Value
Analysis_Date.SetFocus
Calendar7.Visible = False
End Sub

Non working Code


Private Sub Date_Sample_Taken1_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Calendar5.Visible = True
Calendar5.SetFocus
If Not IsNull(Date_Sample_Taken1) Then
Calendar5.Value = Date_Sample_Taken1.Value
Else
Calendar5.Value = Date
End If
End Sub
Private Sub Calendar5_Click()
Date_Sample_Taken1.Value = Calendar5.Value
Date_Sample_Taken1.SetFocus
Calendar5.Visible = False
End Sub
 
W

Wayne-I-M

Hi Richard

To follow on to Ken's post - re asigning the value of the control
(Date_Sample_Taken). A simple method would be to use the LostFocus event of
Calendar5. You could alter your code

Private Sub Calendar5_Click()
Calendar5.Value = Date
Date_Sample_Taken.SetFocus
Calendar5.Visible = False
End Sub

To this

Private Sub Calendar5_LostFocus()
Date_Sample_Taken.SetFocus
Me.Date_Sample_Taken = Me.Calendar5.Value
Me.Calendar5.Visible = False
End Sub

I am not sure if you really mean to have the OnClick assing today's date (as
Ken also noted). If you "do" then alter the code to
Me.Date_Sample_Taken = Date
Or - better - simply use the control's default value =Date()

Hope this helps

--
Wayne
Manchester, England.



richard said:
Ken

Made the changes you suggest below but still not working, have tried the
control with and without [ ] to cope with space between words but no success.
I have the same code working in another form, both are below, could you see
if you can see any differences as I can't

Working Code

Private Sub Analysis_Date_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)
Calendar7.Visible = True
Calendar7.SetFocus
If Not IsNull(Analysis_Date) Then
Calendar7.Value = Analysis_Date.Value
Else
Calendar7.Value = Date
End If
End Sub

Private Sub Calendar7_Click()
Analysis_Date.Value = Calendar7.Value
Analysis_Date.SetFocus
Calendar7.Visible = False
End Sub

Non working Code


Private Sub Date_Sample_Taken1_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Calendar5.Visible = True
Calendar5.SetFocus
If Not IsNull(Date_Sample_Taken1) Then
Calendar5.Value = Date_Sample_Taken1.Value
Else
Calendar5.Value = Date
End If
End Sub
Private Sub Calendar5_Click()
Date_Sample_Taken1.Value = Calendar5.Value
Date_Sample_Taken1.SetFocus
Calendar5.Visible = False
End Sub

Ken Sheridan said:
Richard:

You don't seem to be assigning the value of the calendar control to the
Date_Sample_Taken control. Instead you are setting the calendar control's
value to the return value of the Date function. I think you want this:

Private Sub Calendar5_Click()

Date_Sample_Taken = Calendar5.Value
Date_Sample_Taken.SetFocus
Calendar5.Visible = False

End Sub

You might be interested in the approach I use which puts the calendar
control (ocxCalendar) on a separate form (frmCalendar), which is opened, set
to the current date, by double clicking the date control (called txtDate in
the following example) on the bound form. The code to open the calendar form
is:

Private Sub txtDate_DblClick(Cancel As Integer)

If Not OpenCalendar(Me.Name, "txtDate") Then
MsgBox "Unable to open calendar", vbExclamation, "Error"
End If

End Sub

The code for the frmCalendar form's module is:

Option Compare Database
Option Explicit

Private Sub Form_Close()

Dim intSlashPos As Integer
Dim strForm As String, strControl As String

If Len(Me.OpenArgs) > 0 Then
intSlashPos = InStr(1, Me.OpenArgs, "/")
strForm = Left$(Me.OpenArgs, intSlashPos - 1)
strControl = Mid$(Me.OpenArgs, intSlashPos + 1)
Forms(strForm).Controls(strControl) = ocxCalendar.Value
End If

End Sub


Private Sub Form_Load()

Me!ocxCalendar.Today

End Sub

Private Sub ocxCalendar_Click()

DoCmd.Close acForm, Me.Name

End Sub

And the following function is in a standard module in the database:

Public Function OpenCalendar(strCurrentForm As String, strCurrentControl As
String) As Integer

On Error GoTo ErrHandler

OpenCalendar = True

DoCmd.OpenForm "frmCalendar", _
WindowMode:=acDialog, _
OpenArgs:=strCurrentForm & "/" & strCurrentControl

ExitHere:
Exit Function

ErrHandler:
OpenCalendar = False
Resume ExitHere:

End Function

The function, by passing the names of the calling form and control to the
calendar form as the OpenArgs property, allows the calendar form to be called
from any appropriate control on any form.

Ken Sheridan
Stafford, England
 
I

IRS Intern

Warning; Tom Wickerath is a cry baby database pussy and you shouldn't
listen to him for anything

go and talk to someone that knows SQL Server, kids
 
K

Ken Sheridan

Richard:

I don't see any obvious differences in the code, so it’s a puzzle why it
works in one form but not in another. TW has pointed you to an alternative
approach, and there is also the method I outlined, which I've used without a
hitch since the days before Doris Day was a virgin.

Ken Sheridan
Stafford, England

richard said:
Ken

Made the changes you suggest below but still not working, have tried the
control with and without [ ] to cope with space between words but no success.
I have the same code working in another form, both are below, could you see
if you can see any differences as I can't

Working Code

Private Sub Analysis_Date_MouseDown(Button As Integer, Shift As Integer, X
As Single, Y As Single)
Calendar7.Visible = True
Calendar7.SetFocus
If Not IsNull(Analysis_Date) Then
Calendar7.Value = Analysis_Date.Value
Else
Calendar7.Value = Date
End If
End Sub

Private Sub Calendar7_Click()
Analysis_Date.Value = Calendar7.Value
Analysis_Date.SetFocus
Calendar7.Visible = False
End Sub

Non working Code


Private Sub Date_Sample_Taken1_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y As Single)
Calendar5.Visible = True
Calendar5.SetFocus
If Not IsNull(Date_Sample_Taken1) Then
Calendar5.Value = Date_Sample_Taken1.Value
Else
Calendar5.Value = Date
End If
End Sub
Private Sub Calendar5_Click()
Date_Sample_Taken1.Value = Calendar5.Value
Date_Sample_Taken1.SetFocus
Calendar5.Visible = False
End Sub

Ken Sheridan said:
Richard:

You don't seem to be assigning the value of the calendar control to the
Date_Sample_Taken control. Instead you are setting the calendar control's
value to the return value of the Date function. I think you want this:

Private Sub Calendar5_Click()

Date_Sample_Taken = Calendar5.Value
Date_Sample_Taken.SetFocus
Calendar5.Visible = False

End Sub

You might be interested in the approach I use which puts the calendar
control (ocxCalendar) on a separate form (frmCalendar), which is opened, set
to the current date, by double clicking the date control (called txtDate in
the following example) on the bound form. The code to open the calendar form
is:

Private Sub txtDate_DblClick(Cancel As Integer)

If Not OpenCalendar(Me.Name, "txtDate") Then
MsgBox "Unable to open calendar", vbExclamation, "Error"
End If

End Sub

The code for the frmCalendar form's module is:

Option Compare Database
Option Explicit

Private Sub Form_Close()

Dim intSlashPos As Integer
Dim strForm As String, strControl As String

If Len(Me.OpenArgs) > 0 Then
intSlashPos = InStr(1, Me.OpenArgs, "/")
strForm = Left$(Me.OpenArgs, intSlashPos - 1)
strControl = Mid$(Me.OpenArgs, intSlashPos + 1)
Forms(strForm).Controls(strControl) = ocxCalendar.Value
End If

End Sub


Private Sub Form_Load()

Me!ocxCalendar.Today

End Sub

Private Sub ocxCalendar_Click()

DoCmd.Close acForm, Me.Name

End Sub

And the following function is in a standard module in the database:

Public Function OpenCalendar(strCurrentForm As String, strCurrentControl As
String) As Integer

On Error GoTo ErrHandler

OpenCalendar = True

DoCmd.OpenForm "frmCalendar", _
WindowMode:=acDialog, _
OpenArgs:=strCurrentForm & "/" & strCurrentControl

ExitHere:
Exit Function

ErrHandler:
OpenCalendar = False
Resume ExitHere:

End Function

The function, by passing the names of the calling form and control to the
calendar form as the OpenArgs property, allows the calendar form to be called
from any appropriate control on any form.

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top