Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does dirty not equal true? 1

Status
Not open for further replies.

spruceni

Technical User
May 18, 2007
72
GB
I have a form which has a date field which has a corresponding calendar button. If I type in a date then I can force a save by using me.dirty = false. ie reseting dirty. However if I use the calendar button to enter the date in the field, dirty is not set to true and I can not force a save by reseting dirty.
A typical button event is

Me.RaisedDate.SetFocus
varReturn = GetDateOCX(Me.RaisedDate, True)

This is causing erratic behaviour because the user sometimes types in the date or uses the calendar.

Any thoughts on this?
 
Can you have the calendar control set the form to dirty?

ck1999
 
Hi
I have a large number of forms that call this calendar routine and I do not know how to identify which form called it so that I can make it dirty.

If other data is added to the form it becomes dirty. But once a calendar button is used me.dirty is set to false.

 
Just curious what version of excel are you using. FYI Access 07 has its own date selector that is automatically placed beside a textbox with a date format. I tried changing this and the form.dirty became true.


Is the textbox linked to the underlining table

ck1999
 
I am using access 2003. Is there a built in control that I can use as a calendar button?
 
What is the code you are using to call your calander control and to send the date back to your form

ck1999
 
I copied this all out of a book I have :)
So I am not expert in what it is doing.
On the other hand I have just found DTPicker3 in the other active controls and wonder if this would be better to use?



The code for the click control beside the date field is

Private Sub CmdRaisedDate_Click()
On Error GoTo Err_CmdRaisedDate_Click
Dim varReturn As Variant
' Clicked the calendar icon asking for graphical help
' Put the focus on the control to be updated
Me.RaisedDate.SetFocus

' Call the get a date function
varReturn = GetDateOCX(Me.RaisedDate, True)

Exit_CmdRaisedDate_Click:
Exit Sub

Err_CmdRaisedDate_Click:
MsgBox Err.Description
Resume Exit_CmdRaisedDate_Click

End Sub

This calls the following

Function GetDateOCX(ctlToUpdate As Control, Optional intDateOnly As Integer = 0)
'-----------------------------------------------------------
' Inputs: A Control object containing a date/time value
' Optional "date only" (no time value) flag
' Outputs: Sets the Control to the value returned by frmCalendar
' Created By: JLV 11/15/02
' Last Revised: JLV 11/15/02
'-----------------------------------------------------------

' Set an error trap
On Error GoTo ProcErr

' Open the OCX calendar form by setting a new object
' NOTE: Uses a module variable in the Declarations section
' so that the form doesn't go away when this code exits

Set frmCalOCX = New Form_frmCalendarOCX
' Call the calendar form's public method to
' pass it the control to update and the "date only" flag
Set frmCalOCX.ctlToUpdate(intDateOnly) = ctlToUpdate
' Put the focus on the OCX calendar form
frmCalOCX.SetFocus

ProcExit:
' Done
Exit Function

ProcErr:
MsgBox "An error has occured in GetDateOCX. " _
& "Error number " & Err.Number & ": " & Err.Description _
& vbCrLf & vbCrLf & "If this problem persists, note the error message and " _
& "call your programmer.", , "Ooops . . . (unexpected error)", vbExclamation, gstrAppTitle
Resume ProcExit
End Function

Which calls this form
__________________________________________________


Option Compare Database
Option Explicit

' This form demonstrates both using a custom control (MSCal.OCX)
' and manipulating a Class via Property Set
' See also the GetDateOCX function that activates this form/module.

' Place to save the "date only" indicator
Dim intDateOnly As Integer
' Variable for the Property Set
Dim ctlThisControl As Control
' Optional variable for the Property Set
Dim intSet As Integer
' Place to save the date value
Dim varDate As Variant

Public Sub cmdCancel_Click()
' Close without saving
DoCmd.Close acForm, Me.Name
End Sub

Public Sub cmdSave_Click()
' Saves the changed value back in the calling control

' Do some error trapping here in case the calling control can't
' accept a date/time value.
On Error GoTo Save_Error

' Make sure we got a valid control to point to
If intSet Then
' OK - save the value
If (intDateOnly = -1) Then
' Passing back date only
ctlThisControl.Value = Me.Calendar1.Value
Else
' Do date and time
ctlThisControl.Value = Me.Calendar1.Value + TimeValue(Me.txtHour & ":" & Me.txtMinute)
End If
End If

Save_Exit:
DoCmd.Close acForm, Me.Name
Exit Sub

Save_Error:
MsgBox "An error occured attempting to save the date value.", vbExclamation, gstrAppTitle
'ErrorLog "frmCalendarOCX_Save", Err, Error
Resume Save_Exit

End Sub

Private Sub Form_Load()
' Hide myself until properties are set
Me.Visible = False
End Sub

Public Property Set ctlToUpdate(Optional intD As Integer = 0, ctl As Control)
' This procedure is called as a property of the Class Module
' GetDateOCX opens this form by creating a new instance of the class
' and then sets the required properties via a SET statement.

' First, validate the kind of control passed
Select Case ctl.ControlType
' Text box, combo box, and list box are OK
Case acTextBox, acListBox, acComboBox
Case Else
MsgBox "Invalid control passed to the Calendar.", vbExclamation, gstrAppTitle
DoCmd.Close acForm, Me.Name
End Select

' Save the pointer to the control to update
Set ctlThisControl = ctl

' Save the date only value
intDateOnly = intD
' If "date only"
If (intDateOnly = -1) Then
' Resize my window
DoCmd.MoveSize , , , 3935
' Hide some stuff just to be sure
Me.txtHour.Visible = False
Me.txtMinute.Visible = False
Me.lblColon.Visible = False
Me.lblTimeInstruct.Visible = False
Me.SetFocus
End If

' Set the flag to indicate we got the pointer
intSet = True
' Save the "current" value of the control
varDate = ctlThisControl.Value
' Make sure we got a valid date value
If Not IsDate(varDate) Then
' If not, set the default to today
varDate = Now
Me.Calendar1.Value = Date
Me.txtHour = Format(Hour(varDate), "00")
Me.txtMinute = Format(Minute(varDate), "00")
Else
' Otherwise, set the date/time to the one in the control
' Make sure we have a Date data type, not just text
varDate = CDate(varDate)
Me.Calendar1.Value = varDate
Me.txtHour = Format(Hour(varDate), "00")
Me.txtMinute = Format(Minute(varDate), "00")
End If

End Property

Private Sub txtHour_KeyPress(KeyAscii As Integer)
Dim intHour As Integer
' Trapping key presses in the Hour box
If KeyAscii = 43 Or KeyAscii = 61 Then ' Plus sign key - add one to hour
KeyAscii = 0 ' Swallow the Plus key
' Should have a value, but if not, set to 1
If IsNothing(Me.txtHour) Then
intHour = 1
Else
intHour = Me.txtHour + 1
End If
' If we've wrapped to 24, then reset to zero
If intHour = 24 Then intHour = 0
' Update the text box
Me.txtHour = intHour
' Done
Exit Sub
End If

If KeyAscii = 45 Or KeyAscii = 95 Then ' Minus sign key - subtract one
KeyAscii = 0 ' Swallow the Minus key
' Should have a value, but if not, set to zero
If IsNothing(Me.txtHour) Then
intHour = 0
Else
intHour = Me.txtHour
End If
intHour = intHour - 1
' If we've gone below zero, the wrap to 23
If intHour = -1 Then intHour = 23
' Update the text box
Me.txtHour = intHour
' Done
Exit Sub
End If
' All other key codes pass inspection
' The Input Mask allows only numbers and +/-
End Sub

Private Sub txtMinute_KeyPress(KeyAscii As Integer)
Dim intMinute As Integer
' Trapping key presses in the Minute box
If KeyAscii = 43 Or KeyAscii = 61 Then ' Plus sign key - add one to minute
KeyAscii = 0 ' Swallow the Plus key
' Should have a value, but if not, set to 1
If IsNothing(Me.txtMinute) Then
intMinute = 1
Else
intMinute = Me.txtMinute + 1
End If
' If we've wrapped to 60, then reset to zero
If intMinute = 60 Then intMinute = 0
' Update the text box
Me.txtMinute = intMinute
' Done
Exit Sub
End If

If KeyAscii = 45 Or KeyAscii = 95 Then ' Minus sign key - subtract one
KeyAscii = 0 ' Swallow the Minus key
' Should have a value, but if not, set to zero
If IsNothing(Me.txtMinute) Then
intMinute = 0
Else
intMinute = Me.txtMinute
End If
intMinute = intMinute - 1
' If we've gone below zero, the wrap to 59
If intMinute = -1 Then intMinute = 59
' Update the text box
Me.txtMinute = intMinute
' Done
Exit Sub
End If
' All other key codes pass inspection
' The Input Mask allows only numbers and +/-
End Sub

Private Sub Calendar1_DblClick()

' If they double-click the calendar, act as though they clicked Save
cmdSave_Click

End Sub
 
I have experomented with the Date time picker control.
How do you force it to update the field it is bound to. I have tried to refresh the form on the update and exit events of the DTPicker but the only time the date is inserted in the field is when I click on another date field on the form.

 
I have a form I call frmCalendar using the calendar control this is the vba for it
Code:
Private Sub Calendar0_DblClick()
    c = Calendar0.Value
    DoCmd.Close
    Screen.ActiveControl = c
End Sub

Private Sub Calendar0_Updated(Code As Integer)

End Sub

then the other forms call it by
Code:
Private Sub Command10_Click()
On Error GoTo Err_Command10_Click

    Dim stDocName As String
    Dim stLinkCriteria As String

    stDocName = "frmCalendar"
    DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command10_Click:
    Exit Sub

Err_Command10_Click:
    MsgBox Err.Description
    Resume Exit_Command10_Click
    
End Sub

This returns a me.dirty of true when activated.

Are you sure the txtbox you are updating is part of the recordset?

can you add
Private Sub CmdRaisedDate_Click()
On Error GoTo Err_CmdRaisedDate_Click
Dim varReturn As Variant
' Clicked the calendar icon asking for graphical help
' Put the focus on the control to be updated
Me.RaisedDate.SetFocus

' Call the get a date function
varReturn = GetDateOCX(Me.RaisedDate, True)


me.dirty = true ' DOES THIS HAVE ANY EFFECT

Exit_CmdRaisedDate_Click:
Exit Sub

Err_CmdRaisedDate_Click:
MsgBox Err.Description
Resume Exit_CmdRaisedDate_Click

End Sub


ck1999
 
Thanks for your code on your frmCalendar. I will try it later I am off to bed as it is 11:30 here and I have work in the morning.

I did try
me.dirty = true ' DOES THIS HAVE ANY EFFECT
before and it does not have any effect. The code seems to skip it and exit the sub :(

Thanks for the help so far
Regards
Spruce
 
Hi CK1999

I used your code to generate the calendar. I can get the date transferred into my form.

Private Sub Calendar0_DblClick()
c = Calendar0.Value
DoCmd.Close
Screen.ActiveControl = c
End Sub

Where the date field is the focus. It made the form dirty. :))

Is is possible to work the other way? If there is a date already in the date field can this be transferred into the calender when calling the calendar form?

I tried Screen.ActiveControl = Calendar0.Value in the calling code which probably tells you more about my lack of knowledge as it generated an error :( I also used

forms!frmcalendar.calendar0.value =
forms!frmImprovDetailsNew.RaisedDate

which did not work either.


regards
Spruce
 
Delete the old docmd statement and add this to your command click (on origional form) where you call the calendar form

Code:
Dim vvar
    vvar = ""
    If Nz(Me.txtraiseddate, "") <> "" Then
        vvar = Me.txtraiseddate
    End If
    stDocName = "frmCalendar"
    DoCmd.OpenForm stDocName, , , , , , vvar

to the calendar form add

Code:
Private Sub Form_Activate()
    Me.Calendar0.Value = Me.OpenArgs
    
End Sub

this should do it

ck1999
 
Thanks a million it works so all I have to do now is change the code on the rest of the form ( 9 dates) :))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top