Results 1 to 10 of 10
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    Two Option Groups on one form


    Hi to all,

    I have main form with a subform. The main form has an option group to show books for a certain person based on the selection: (Borrwed, Returned or Both). I have a Public Function to requery the subform in the AfterUpdate event of the option group. Here is the VBA code I am using.

    Code:
    Public Function RequerySubform()
    On Error GoTo ProcError
    
    Dim strSQL As String
    Dim varReturned As Variant
    
    
        If Len(Me.cboSelectedPerson & vbNullString) > 0 Then
    
            Select Case fraBookStatus
                Case 1 ' Borrowed books
                    varReturned = "IS NULL"
                Case 2 ' Returned books
                    varReturned = "IS NOT NULL"
                Case 3 ' All books borrowed and returned by a given member.
                    varReturned = Null
            End Select
           
        
            ' mstrWHERE = "fkMemberID = " & Me.cboSelectedPerson & (" AND [DateReturned] " + varReturned)
              mstrWHERE = "fkMemberID = " & Me.cboSelectedPerson & (" AND [DateReturned] " + varReturned) & (" AND [DateBorrowed] " + "Is Not Null")
              
        
            strSQL = "Select * from qryBorrowingBooks where " & mstrWHERE
            Me.cntBorrowedBooksSubForm.Form.RecordSource = strSQL
        End If
    
    ExitProc:
        Exit Function
    ProcError:
        MsgBox "Error " & Err.Number & ": " & Err.Description, , _
                   "Error in RequerySubform event procedure..."
        Resume ExitProc
    End Function
    I want to add a second Option group (fraDates) to take the selected records based on (fraBookStatus) and sort them by dates: Date borrowed, Date Due or Date Returned selected from the second option group.

    Any ideas of how to do it?

    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Just test the option group and add to the mstrWhere, just as you have with the first option group.

    I am suprised that even works as the brackets are outside the quotes?

    You should always Debug.Print your sql string and see if it is what you need, not what you think you have.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi,
    Thank you for the reply
    I added the following line to have another Select Case in the same AfterUpdate event:
    Code:
     Select Case fraDates
                Case 1 ' Date Borrowed
                    varDates = "1"
                Case 2 ' Date Due
                    varDates = "2"
                Case 3 ' Date Returned
                    varDates = "3"
            End Select
    I tried to add the Order By but was not successful.
    Code:
    mstrWHERE = "fkMemberID = " & Me.cboSelectedPerson & (" AND [DateReturned] " + varReturned) & (" AND [DateBorrowed] " + "Is Not Null") &  ORDER BY fraDates
    I got an "End of statement" error.

    Thanks

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    As I said 'Use Debug.Print' to see your error.
    Your concatenation is incorrect.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is online now Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,422
    To add to that, when you eliminate your error message(s) and try to apply sorting, what kind of date sort is a 1 or 2 or 3 (the value of fraDates)?
    Also looks to me like you're going to need to use some other event/trigger since
    - you'll be applying a form recordsource and then a sort (inefficient) or
    - applying a sort then losing it when a recordsource is applied (because the operations were done backwards)
    Probably a button click event would be better and apply the sort as part of the recordsource sql.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Using an ampersand "&" and a plus "+" when concatenating do 2 separate things especially when there is a null involved.

    I would probably simplify the book status Opt group code

    Code:
           Select Case fraBookStatus
    
                Case 1 ' Borrowed books
                    varReturned = " AND [DateReturned] is null"
    
                Case 2 ' Returned books
                    varReturned = " AND [DateReturned] is not null"
    
                Case 3 ' All books borrowed and returned by a given member.
                    varReturned = ""
    
            End Select
    
           debug.print varReturned
    Same with the dates

    Code:
    Select Case fraDates
    
                Case 1 ' Date Borrowed
                    varDates = " Order by DateBorrowed "
    
                Case 2 ' Date Due
                    varDates = " Order by DateDue "
    
                Case 3 ' Date Returned 
                   varDates = " Order by DateReturned "
    
            End Select
    If this applies to a subform, I may be more inclined to use a filter.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Thank you Moke123
    I used the Debug.Print to follow up with the mstrWHERE and strSQL.

    One more logical issue is that when Books are not returned yet to the library the date returned value is null and I cannot sort by that option.

    In other words: optActive from fraBooksStatus (Borrowed books) is selected, Date returned is null because books are not returned yet.
    How will I disable the optReturned Button (Date Returned) in fraDtes?

    I tried this code unsuccessfuly.
    Code:
     If fraBookStatus = 1 Then
                OptReturned.Locked = True
            Else
                ' do nothing
            End If
    I put the above code after the Select Case fraBookStatus and before the Select Case fraDates

    Any advise?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    You could use the NZ() function to put in a spurious date? like 30/12/2099
    Increase the year, if you think your app is still going to be running by then.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi to all,
    Resolved it with the following:
    Code:
    Private Sub optActive_GotFocus()
    
            Me.optActive.Enabled = True
            Me.OptReturned.Enabled = False
    
    End Sub
    
    Private Sub optBoth_GotFocus()
    
            Me.optBoth.Enabled = True
            Me.OptReturned.Enabled = True
    
    End Sub
    
    Private Sub optInactive_GotFocus()
    
            Me.optInactive.Enabled = True
            Me.OptReturned.Enabled = True
    
    End Sub
    Thank you all
    Khalil

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    What happens if they choose the wrong option? They can't choose another if not enabled.

    Option groups only allow one option to be selected at a time so no need to disable an option.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 12
    Last Post: 02-08-2023, 03:05 PM
  2. Replies: 3
    Last Post: 07-22-2017, 12:03 PM
  3. Replies: 16
    Last Post: 02-01-2016, 05:42 PM
  4. Form option groups and display styles
    By DarthZ in forum Forms
    Replies: 5
    Last Post: 01-31-2012, 01:08 PM
  5. Replies: 3
    Last Post: 11-03-2010, 09:53 AM

Tags for this Thread

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