Results 1 to 5 of 5
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Form that opens with no value breaks vba

    I've got a form that opens with a date as the parameter, null date opens fine but if the form opens with an area that has no null date (which means there is no records with a null date and the form opens blank) then I get a vba error for this code



    Function DaylightSwitch()





    ElseIf Forms!frmSchools!State = "QLD" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 1, Time())




    ElseIf Forms!frmSchools!State = "SA" Then
    Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())




    ElseIf Forms!frmSchools!State = "WA" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())




    ElseIf Forms!frmSchools!State = "NT" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())


    Else: Forms!frmSchools!TxtTime = Time()


    End If


    End Function

    I guess since the form hasn't come up with any results for "QLD" that breaks the vba - how do I get around this? Just make it ignore the error?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I guess since the form hasn't come up with any results for "QLD" that breaks the vba
    You are missing the first part of the IF() statement.

    Code:
    Function DaylightSwitch()
    
         'delede the "Else"
    '    ElseIf Forms!frmSchools!State = "QLD" Then
    
        If Forms!frmSchools!State = "QLD" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 1, Time())
    
        ElseIf Forms!frmSchools!State = "SA" Then
            Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())
    
        ElseIf Forms!frmSchools!State = "WA" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())
    
        ElseIf Forms!frmSchools!State = "NT" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())
    
        Else     ' don't use the colon (:) in the multi-line form of the IF() function.
            Forms!frmSchools!TxtTime = Time()
    
        End If
    
    End Function
    I would write the function as
    Code:
    Function DaylightSwitch(sState As String)
    
        If sState = "SA" Then
            Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())
    
        ElseIf sState = "WA" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())
    
        ElseIf sState = "NT" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())
    
        Else 
            Forms!frmSchools!TxtTime = Time()
    
        End If
    
    End Function
    It would be called like:

    =DaylightSwitch(Forms!frmSchools!State)



    Where is this function - in the form module or a standard module? (form name??)
    How are you calling the function?

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Module

    The Form_Timer() calls the module.

    sorry I had an if before and I deleted it without updating my code

    This is what i have
    Code:
    Function DaylightSwitch()
    
    If Forms!frmSchools!State = "QLD" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 1, Time())
    
    
    
    
    ElseIf Forms!frmSchools!State = "SA" Then
    Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())
    
    
    
    
    ElseIf Forms!frmSchools!State = "WA" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())
    
    
    
    
    ElseIf Forms!frmSchools!State = "NT" Then
    Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())
    
    
    Else: Forms!frmSchools!TxtTime = Time()
    
    
    End If
    
    
    End Function

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    The error occurs when the form I have has no​ records because of the parameters haven't pulled up anything - because the module relies on a record value it then breaks when there isn't one.

    so if there is no record value "QLD" then it gets an error when I have pulled in an area in QLD

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The Form_Timer() calls the module.
    Strange...... I would have used the form open event, not the timer event.

    Functions return a value, even if you do not use the returned value.
    So two options...

    Option 1:
    Code:
    Function DaylightSwitch()
    
        'set a default return value
        DaylightSwitch = #12:00:01 AM#
    
        If Forms!frmSchools!State = "QLD" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 1, Time())
    
        ElseIf Forms!frmSchools!State = "SA" Then
            Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())
    
        ElseIf Forms!frmSchools!State = "WA" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())
    
        ElseIf Forms!frmSchools!State = "NT" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())
    
        Else     ' do not use the colon (:) in a multi-line IF() function.
            Forms!frmSchools!TxtTime = Time()
    
        End If
    
    End Function


    Option 2:
    Since you are not using the function to return a value, change the timer event code to call a sub...

    Code:
    Private Sub Form_Timer()
        Call DaylightSwitch
    End Sub
    Code:
    Sub DaylightSwitch()
    
        If Forms!frmSchools!State = "QLD" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 1, Time())
    
        ElseIf Forms!frmSchools!State = "SA" Then
            Forms!frmSchools!TxtTime = DateAdd("n", -30, Time())
    
        ElseIf Forms!frmSchools!State = "WA" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 4, Time())
    
        ElseIf Forms!frmSchools!State = "NT" Then
            Forms!frmSchools!TxtTime = DateAdd("h", 3, Time())
    
        Else    ' do not use the colon (:) in a multi-line IF() function.
            Forms!frmSchools!TxtTime = Time()
    
        End If
    
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Search Form Breaks
    By Zachrareth in forum Forms
    Replies: 14
    Last Post: 09-03-2013, 02:42 PM
  2. Replies: 21
    Last Post: 08-05-2013, 06:23 AM
  3. Replies: 1
    Last Post: 03-01-2012, 09:06 AM
  4. Replies: 35
    Last Post: 12-21-2011, 02:16 PM
  5. Replies: 1
    Last Post: 03-07-2011, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums