Hiding/Unhiding fields in a form using a pick list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry911
    New Member
    • Nov 2007
    • 10

    Hiding/Unhiding fields in a form using a pick list

    I have a form that uses a pick list to select a specified date/time range based on the case value. Case 1-7 have pre-defined date/time functions, but case 8 is for a custom date/time range. I only want to show the custom fields when the custom date range option is selected. When the form loads I have all the fields hidden.

    [code=vb]Private Sub Form_Load()
    Me!CustomDateBo x.Visible = False
    Me!dtpStartDate .Value = Date
    Me!dtpEndDate.V alue = Date
    Me!dtpStartDate .Visible = False
    Me!cstStartTime .Visible = False
    Me!dtpEndDate.V isible = False
    Me!cstEndTime.V isible = False
    Me!lblCstDateRa nge.Visible = False
    Me!lblFrom.Visi ble = False
    Me!lblTo.Visibl e = False

    End Sub
    [/code]
    When case "8" is selected the same fields are set to true and the fields are visible and all works well. What I want to try and accomplish is a cleaner way in my code to hide these fields whenever one of the other options is reselected.

    I have placed the above code in each case statement and that works, but it seams to me that I should be able to simly make an If-Then-Else type statement so that I only have to enter the code 1 time.

    I have tried several variations of the following but I get syntax errors or compile errors.

    Any thoughts?

    [code=vb]DateSelectorVal ue = Me!DateSelector

    Select Case DateSelectorVal ue

    If DateSelectorVal ue Is = 8 Then
    Me!CustomDateBo x.Visible = True
    Me!dtpStartDate .Visible = True
    Me!cstStartTime .Visible = True
    Me!dtpEndDate.V isible = True
    Me!cstEndTime.V isible = True
    Me!lblCstDateRa nge.Visible = True
    Me!lblFrom.Visi ble = True
    Me!lblTo.Visibl e = True
    Else
    Me!CustomDateBo x.Visible = False
    Me!dtpStartDate .Visible = False
    Me!cstStartTime .Visible = False
    Me!dtpEndDate.V isible = False
    Me!cstEndTime.V isible = False
    Me!lblCstDateRa nge.Visible = False
    Me!lblFrom.Visi ble = False
    Me!lblTo.Visibl e = False
    End If


    Case "1" 'Today
    Me!START = DateString(Now( )) & "000000"
    Me!END = DateString(Now( )) & "235959"
    Me!END2 = DateString(Date + 1) & "013000"

    Case "2" 'Yesterday
    Me!START = DateString(Date - 1) & "000000"
    Me!END = DateString(Date - 1) & "235959"
    Me!END2 = DateString(Date ) & "013000"

    Case "3" 'Week-To-Date
    Me!START = DateString(Date - Weekday(Date) + 1) & "000000"
    Me!END = DateString(Date ) & "235959"
    Me!END2 = DateString(Date + 1) & "013000"

    Case "4" 'Last Week
    Me!START = DateString(Date - Weekday(Date) - 6) & "000000"
    Me!END = DateString(Date - Weekday(Date) - 7 + 7) & "235959"
    Me!END2 = DateString(Date - Weekday(Date) - 7 + 8) & "013000"

    Case "5" 'Month-To-Date
    Me!START = DateString(Date Serial(YEAR(Dat e), MONTH(Date), 1)) & "000000"
    Me!END = DateString(Date ) & "235959"
    Me!END2 = DateString(Date + 1) & "013000"

    Case "6" 'Last Month
    Me!START = DateString(Date Serial(YEAR(Dat e), MONTH(Date) - 1, 1)) & "000000"
    Me!END = DateString(Date Serial(YEAR(Dat e), MONTH(Date), 0)) & "235959"
    Me!END2 = DateString(Date Serial(YEAR(Dat e), MONTH(Date), 1)) & "013000"

    Case "7" 'Last 20 Weeks
    Me!START = DateString(([L20W]) - Weekday([L20W]) + 1) & "000000"
    Me!END = DateString(Date - Weekday(Date) - 7 + 7) & "235959"
    Me!END2 = DateString(Date - Weekday(Date) - 7 + 8) & "013000"

    Case "8" 'Custom
    Me!START = DateString([dtpStartDate]) & TimeString([cstStartTime])
    Me!END = DateString([dtpEndDate]) & TimeString([cstEndTime])
    Me!END2 = GetDateString([cEND2])

    End Select

    End Sub
    [/code]
    Last edited by Stewart Ross; Mar 15 '08, 08:04 PM. Reason: added code tags around vb
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi Jerry911. Your IF test has a syntax error in it; it should just be IF ... = 8, not Is = 8. You are also enclosing this within the scope of the SELECT CASE statement, which is incorrect and is another syntax error.

    With the addition of one boolean variable you can do away with the IF..THEN..ELSE, as shown below. I have assumed that the dateselectorval ue is a string, as that is how you are referring to it in your Case statements.
    [code=vb]Dim MakeVisible as Boolean

    DateSelectorVal ue = Me!DateSelector

    MakeVisible = DateSelector = "8"

    Me!CustomDateBo x.Visible = MakeVisible
    Me!dtpStartDate .Visible = MakeVisible
    Me!cstStartTime .Visible = MakeVisible
    Me!dtpEndDate.V isible = MakeVisible
    Me!cstEndTime.V isible = MakeVisible
    Me!lblCstDateRa nge.Visible = MakeVisible
    Me!lblFrom.Visi ble = MakeVisible
    Me!lblTo.Visibl e = MakeVisible

    Select Case DateSelectorVal ue

    Case "1" 'Today
    Me!START = DateString(Now( )) & "000000"
    Me!END = DateString(Now( )) & "235959"
    Me!END2 = DateString(Date + 1) & "013000"
    '... and all other cases as before[/code]
    -Stewart

    Comment

    • Jerry911
      New Member
      • Nov 2007
      • 10

      #3
      Thanks for the response!

      I just had to make one adjustment to your code. You had: MakeVisible = DateSelector = "8" and it needed to be: MakeVisible = DateSelectorVal ue = "8" . Worked like a champ after that. Only took me an hour of fiddling with it...

      Thanks again!

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        Hi Jerry. Glad you got it working after my accidental shortening of the variable name...

        If you include the statement
        [code=vb]Option Explicit[/code]
        at the top of your code the compiler itself will throw an error if a variable name does not match a DIM statement, or if there is no DIM in the first place. This would have caught my incorrect version of your variable without you having to spend an hour tring to work out what was wrong...

        Option explicit is usually included by default as the very top line in a code module.

        Cheers

        Stewart

        Comment

        Working...