Split form or Subform - which one and how to use

illusionek

Registered User.
Local time
Yesterday, 23:10
Joined
Dec 31, 2013
Messages
92
Hello

I am about to design my first form in Access and I think I am trying to bite more than I can chew ;(


So I would really appreciate any help I can get.
Basically I need to design form that looks very similar to either Split Form or Subform. I have attached a print screen of what I roughly need.


The form is split into two parts. In the first/top part there are some List Box/Date fields that act as filters to the bottom form, so the bottom form displays only records that match values in red.



The second part of the top form has some field that require input from user and then these records are added to the bottom form.
I was trying to use both Split Form and Subform and none of it is working, so I would really appreciate any help/tips.


I have attached an example of database and this form would be based on data from Query1.







Regards!
 

Attachments

  • Database - sample.accdb
    Database - sample.accdb
    480 KB · Views: 103
  • print screen.JPG
    print screen.JPG
    32 KB · Views: 120
Hi,

Would be really grateful for any help, I do not even know where to start :D

Many thanks!
 
Wish I could, but I a) can't download things at work, and b) can't access accdb files at work due to only having Access 2003 here.

So what I think you're saying is that you want the form to show data, but have it limited by what filters have been selected? Sort of what is going on in the photo I've attached?
 

Attachments

  • FeeSchedLogFilters_Cleaned.JPG
    FeeSchedLogFilters_Cleaned.JPG
    71.1 KB · Views: 146
Hello!

Yes, this looks like potentially something I would be looking for but how to arrive there ? :banghead:
 
Okay, once I get back to work in the morning, I'll post the actual filter code.

As to the rest, on the filter tab, the top part is simply an option group allowing the user to choose between all dates, a set of pre-determined date ranges, or a custom date range. Choosing an option other than 'All' unlocks the controls next to that option.

Below that are a pair of combo boxes with lists constructed via VBA (so that "All Users" and "All Actions" appear and are at the top).

The 'Apply Filters' button executes VBA that checks the filter selections, and if any are not at the default setting (the ones visible in the image), creates a custom WHERE clause. It then appends that WHERE clause to the SQL statement used to populate the list box on the left and refreshes the list.

The 'Show All' button just sets the list box's row source to the default SQL statement and refreshes the control.
 
Okay, here are the pertinent parts of the module for the form I showed you:

Code:
Option Compare Database
Option Explicit
Option Base 0
 
'Constant declarations.
Private Const CAPTION_DEFAULT = "Please select an action to review."
Private Const ACTION_DEFAULT = "All Actions"
Private Const USER_DEFAULT = "All Users"
Private Const SQL_SELECT = "SELECT dbo_tblActivityLog.ActivityID, " & _
                                  "dbo_tblActivityLog.ActionID, " & _
                                  "dbo_tblActivityLog.UserID, " & _
                                  "Format([ActivityDate],'Short Date') AS [Date], " & _
                                  "Format([ActivityDate],'h:mm AMPM') AS [Time], " & _
                                  "dbo_tblActions.ActionName "
Private Const SQL_FROM = "FROM dbo_tblActions " & _
                         "INNER JOIN dbo_tblActivityLog ON dbo_tblActions.ActionID = dbo_tblActivityLog.ActionID "
Private Const SQL_ORDERBY = "ORDER BY dbo_tblActivityLog.ActivityDate DESC;"
 
Private Sub cmdFilter_Click()
 
Dim SQL As String
Dim sqlWhere As String
 
    'Determine if a filter is actually needed.
    If Me.optDateRange = 1 And Me.cboActions = 0 And Me.cboUsers = USER_DEFAULT Then
        MsgBox "Filter values must be set before a filter can be applied.", vbInformation, AppTitle
    'Determine if txtFromDate is greater than txtToDate.
    ElseIf Me.txtFromDate > Me.txtToDate Then
        MsgBox "From date must be less than or equal to the To date.", vbInformation, AppTitle
    Else
        'Determine if the user selected the 'Date Range' option in the Date Range option group.
        If Me.optDateRange.Value = 2 Then
            'Determine if either txtFromDate or txtToDate were left blank.
            If IsNull(Me.txtFromDate.Value) Or IsNull(Me.txtToDate.Value) Then
                'Notify the user to either enter both dates or select a different option.
                MsgBox "Please enter both a start and a finish date, or select another date range option.", vbInformation, AppTitle
                'Abort the procedure.
                Exit Sub
            End If
        End If
        'Execute CreateWhere to generate the WHERE statement needed for the selected filter.
        sqlWhere = CreateWhere()
        'Assemble the SQL statement that will be the new recordsource for lstActivityLog.
        SQL = SQL_SELECT & SQL_FROM & sqlWhere & SQL_ORDERBY
        'Assign the string in SQL to be the new recordsource of lstActionLog.
        Me.lstActionLog.RowSource = SQL
        'Show lblFilter.
        Me.lblFilter.Visible = True
    End If
End Sub

Private Function CreateWhere() As String
' ************************************************************
' Created by       : Scott L. Prince
' Parameters       : None
' Results          : Determines the necessary WHERE clause to limit lstActionList to user-selected parameters
' Returns          : String containing SQL WHERE clause
' Date             : 4-11-2014
' Remarks          :
' Changes          :
' ************************************************************
On Error GoTo CreateWhere_Err
 
Dim ActionID As Long
Dim DateFrom As Date
Dim DateTo As Date
Dim RangeType As Long
Dim TempWhere As String
Dim User As String
 
    'Pull filter selections.
    User = Me.cboUsers.Value
    ActionID = Me.cboActions.Value
    RangeType = Me.optDateRange.Value
    
    'Begin the WHERE statement.
    TempWhere = "WHERE ("
    
    'If a specific user has been selected, add them to the WHERE statement.
    If User <> USER_DEFAULT Then TempWhere = TempWhere & "((dbo_tblActivityLog.UserID) = '" & User & "') "
    
    'Determine if the user is filtering on cboActions.
    If ActionID <> 0 Then
        'If TempWhere has changed from "WHERE ", add "AND ".
        If TempWhere <> "WHERE (" Then TempWhere = TempWhere & "AND "
        'Add the selected ActionID to the WHERE criteria.
        TempWhere = TempWhere & "((dbo_tblActivityLog.ActionID) = " & ActionID & ") "
    End If
    
    'Determine DateFrom and DateTo based on value of rangetype.
    Select Case RangeType
        Case 1                  'All dates
            'Strip the space at the end of TempWhere and append ") " to close out the WHERE clause.
            TempWhere = RTrim(TempWhere) & ") "
        Case 2                  'Specified range
            'Set DateTo to tomorrow.  (Access SQL doesn't include the final argument of a BETWEEN statement.)
            DateTo = DateAdd("d", 1, Date)
            'Determine which range was selected in order to determine DateFrom.
                Select Case Me.cboDateRange.Value
                    Case 1      '30 days
                        DateFrom = DateAdd("d", -30, Date)
                    Case 2      '90 days
                        DateFrom = DateAdd("d", -90, Date)
                    Case 3      '6 months
                        DateFrom = DateAdd("m", -6, Date)
                    Case 4      '1 year
                        DateFrom = DateAdd("yyyy", -1, Date)
                End Select
            'If TempWhere has changed from "WHERE ", add "AND ".
            If TempWhere <> "WHERE (" Then TempWhere = TempWhere & "AND "
            'Add the selected date range to TempWhere.
            TempWhere = TempWhere & "((dbo_tblActivityLog.ActivityDate) Between #" & DateFrom & "# And #" & DateTo & "#)) "
        Case 3                  'Provided dates
            'Pull the values for DateFrom and DateTo from the user-entered values in txtFromDate and txtToDate.
            'One day is added to DateTo because Access SQL excludes the second argument of BETWEEN.
            DateFrom = Me.txtFromDate.Value
            DateTo = DateAdd("d", 1, Me.txtToDate.Value)
            'If TempWhere has changed from "WHERE ", add "AND ".
            If TempWhere <> "WHERE (" Then TempWhere = TempWhere & "AND "
            'Add the selected date range to TempWhere.
            TempWhere = TempWhere & "((dbo_tblActivityLog.ActivityDate) Between #" & DateFrom & "# And #" & DateTo & "#)) "
    End Select
    
    'Return the WHERE string.
    CreateWhere = TempWhere
    
CreateWhere_Exit:
    Exit Function
    
CreateWhere_Err:
    MsgBox "Error occurred" & vbCrLf & vbCrLf & _
    "In Function:" & vbTab & "CreateWhere" & vbCrLf & _
    "Err Number: " & vbTab & Err.Number & vbCrLf & _
    "Description: " & vbTab & Err.Description, vbCritical, AppTitle
    Resume CreateWhere_Exit
End Function

You won't be able to copy and paste this straight into your database, but it should give you ideas on how to go about adapting the same idea to your forms. I snipped out the code involved in building the rows for the filter drop downs, determining which controls lock and unlock when, and some data display code for the data tab, but if you need, I can post those as well. I can't guarantee that this is the most efficient way to go about this, but I can guarantee it works. :)

I know you're going to have questions, so ask away. Hopefully the form, table, and control references are self-explanatory - I tried to make them so, anyway!
 

Users who are viewing this thread

Back
Top Bottom