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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time form cancel 1

Status
Not open for further replies.

vttech

Technical User
Jan 28, 2006
297
US
I have a form called frmGetTime that contains three combo boxes

1.cboHour
Row Source Type = Value List
Row Source = 1;2;3;4;5;6;7;8;9;10;11;12

2. cboMinutes
Row Source Type = Value List
Row Source = 00;15;30;45

3. cboAMPM
Row Source Type = Value List
Row Source = AM;PM

and two command buttons

1.cmdCanel
2.cmdOK


The problem I am having is that when the user press cancel
Code:
Private Sub cmdExit_Click()
DoCmd.Close acForm, "frmGetTime"
End Sub

it should close the form, which is does; but then it also places 12:00 AM in the text box
why is access placing 12:00 AM in the text box? what can I do to solve it?

Listed below is the full code



DisplayTime module

Code:
Option Compare Database
Option Explicit
Public CompleteTime As Date


Public Function GetTime(txtbox As Control) As Date

DoCmd.OpenForm "frmGetTime", , , , , acDialog

txtbox = Format(CompleteTime, "hh:mm AM/PM")

End Function



frmGetTime

Code:
Option Compare Database
Option Explicit

Private Sub cmdExit_Click()
'CompleteTime = ""
DoCmd.Close acForm, "frmGetTime"
End Sub

Private Sub cmdOK_Click()
Dim ctl As Control
Dim strRequired As String

For Each ctl In Form.Controls
    If TypeOf ctl Is ComboBox Then
        If IsNull(ctl) Or ctl = "" Then
            strRequired = strRequired + ctl.Name & ", "
        Else
            
        End If
    End If
Next

If strRequired = "" Or IsNull(strRequired) Then
    CompleteTime = Forms!frmGetTime!cboHour & ":" _
    & Forms!frmGetTime!cboMinutes & Forms!frmGetTime!cboAMPM
    
    DoCmd.Close acForm, "frmGetTime"
Else
    MsgBox strRequired & "is a required field"
End If

End Sub






Newbie in search of knowledge
 
OK my problem lies in

Code:
txtbox = Format(CompleteTime, "h:mm AMPM")

so I change the code by adding Format(CompleteTime, "hh:mm AM/PM") here and erasing it from the public function

Code:
Private Sub cmdOK_Click()
Dim ctl As Control
Dim strRequired As String
For Each ctl In Form.Controls
    If TypeOf ctl Is ComboBox Then
        If IsNull(ctl) Or ctl = "" Then
            strRequired = strRequired + ctl.Name & ", "
        Else
            
        End If
    End If
Next
If strRequired = "" Or IsNull(strRequired) Then
    CompleteTime = Forms!frmGetTime!cboHour & ":" _
    & Forms!frmGetTime!cboMinutes & Forms!frmGetTime!cboAMPM

   [b] Forms!frmTestTime!txtTime = Format(CompleteTime, "hh:mm AM/PM")[/b]
    
    DoCmd.Close acForm, "frmGetTime"
Else
    MsgBox strRequired & "is a required field"
End If
End Sub




how would I pass the name of the Form and Control via the Function?? I use this form in 10 different other forms

Thus I would rather pass the name of the control and the form instead of manually entering then like I did
Forms!frmTestTime!txtTime
any ideas?

thanks




Newbie in search of knowledge
 
How are ya vttech . . .

A date data type is a floating point number where the integer is the date and the decimal portion is the time, where a [blue]decimal of zero represents 12:00:00 AM[/blue].

In your code:
Code:
[blue]Public CompleteTime As Date[/blue]
Initializes [blue]CompleteTime[/blue] to zero! Hence when you click the button to exit the form [blue], CompleteTime[/blue] remains unchanged with its initialized value of zero!

To fix this you need a Public variable that you set whenever you click your exit button:
Code:
[blue]Option Compare Database
Option Explicit

Public CompleteTime As Date, [purple][b]flg[/b][/purple] As Boolean


Public Function GetTime(txtbox As Control) As Date

   DoCmd.OpenForm "frmGetTime", , , , , acDialog
   
   If [purple][b]flg[/b][/purple] Then
      txtbox = ""
      [purple][b]flg[/b][/purple] = False
   Else
      txtbox = Format(CompleteTime, "hh:mm AM/PM")
   End If

End Function[/blue]
. . . and in your exit button:
Code:
[blue]Private Sub cmdExit_Click()
   [green]'CompleteTime = ""[/green]
   [purple][b]flg[/b][/purple] = True
   DoCmd.Close acForm, "frmGetTime"
End Sub[/blue]
BTW: you never set the return value of your function (you only set txtbox), so it always returns zero as well! . . . AKA [purple]12:00:00 AM[/purple]!

[blue]Your Thoughts! . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top