Results 1 to 12 of 12
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    DCount Methods with primary form and sub form

    Good morning all,
    I have a primary form (frmAccountHolder) and a subform (sFrmAccount) and When I Untick the IsActive checkbox Making it not active, I want to Untick the IsActive checkbox(s) if more than one in the subform which is a continous form.
    It may have one or more accounts in it.
    I think it is a DCount method but I have tried so many varancies of it and just cannot figure it out.
    Here is current code.

    Code:
    Private Sub ChkIsActive_Click()
        If ChkIsActive = True Then
        'If DCount("AccountHolderID", "tblAccount", "IsActive=" & Forms!frmAccountHolder.sFrmAccount.Form!ChkIsActive) > 0 Then
            Forms!frmAccountHolder.sFrmAccount.Form!ChkIsActive = True
            Me.ChkRecordLock = False
        Else
            Forms!frmAccountHolder.sFrmAccount.Form!ChkIsActive = False
            Me.ChkRecordLock = True
        End If
         'End If
        Call Form_Current
    End Sub
    So, If ChkIsActive = False on primary form, I want to untick all of the ChkIsActive in subform.
    I want it to count all the IsActives that have the same AccountHolderId and untick them all.
    Current code works with not using the DCount part but will only untick one account in the subform, not all of them.
    I would really appriciate some assistance on this please


    Thanks
    Dave

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Isn't this just a matter of running a sql statement or stored query against the table for the subform when the checkbox is changed? Like
    If me.AcccountID = 0 then << this pertains to the check box control so I probably should have named it something more intuitive
    UPDATE tblSubform SET IsActive = 0 WHERE tblSubform.AccountID = Forms!MainFormName.txtAccountID

    then refresh the subform. Could use False instead of zero. You'd use your own object names of course, and delimit text values - I assumed id is a number data type.
    Last edited by Micron; 03-01-2024 at 12:26 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I think Micron's suggestion would work (with some changes as I think the trigger Me.AccountID=0 should be modified to refer to the IsActive field).

    But the main point is why have two IsActive fields? This (usually) goes against normalization rules, can't you replace the control source of the IsActive checkbox on the main form with a calculated expression (=IIF(DCount("*","tblAccount","AccountHolderID=" & Me.AccountHolderID)=0,False,True) to avoid the posibility of data integrity issues with two fields.

    You can still have code (maybe a button or added to the double click event of the current checkbox) to uncheck all with an Update query (or looping through the subform's recordset as an alternative), but the main form's IsActive should now always be accurate regardless where the changes to the field in the tblAccount are done (this form, other form, query, table, etc.)

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I think the trigger Me.AccountID=0 should be modified to refer to the IsActive field
    I probably edited my post while you were replying because I noticed I was not clear about that (and maybe still am not). I took it that there really aren't 2 IsActive fields, just 2 controls and they are the Parent/Child controls. However, I don't think that makes sense and the whole post is not clear to me.

    Are the accounts listed in the subform? Then what about the main form could be inactive - the member/customer? Then they could be active but one of their accounts may not be, in which case the two controls/fields are not dependent as I interpreted it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Then they could be active but one of their accounts may not be, in which case the two controls/fields are not dependent as I interpreted it
    Yes, that would still account for an active account holder, but when all their accounts are inactive then the account holder itself becomes inactive.
    Just my take....
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi all and thanks for the info.
    The reason I wanted this was I can check an account inactive in my sub form and still have other accounts active but if I uncheck the account holder in the primary form I wanted all the accounts associated with it to be not active. The IsActive control or check box on each the form and subform are to trigger a record lock so no info can be changed. Hope I explained that. I will give some more tries to this.
    Thank you all
    Dave

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Hi Dave,
    What I suggested is in no way hindering your goal here, it just ensures that the status of the account holder is always reflective of the sum of statuses of all their accounts. We don't know your app but I saw cases where users have access to the tblAccounts in different objects, being forms or queries, and changes in there will not trigger updates at the parent level.
    You can easily add the update code to the existing checkbox (double-click) or to a button besides it:
    Code:
    Dim sSQl as String
    if MsgBox ("Make account inactive?",vbYesNo)=vbNo then Exit Sub
    sSQL="UPDATE tblAccount SET IsActive = 0 WHERE tblAccount .AccountHolderID = " & Me.AccountHolderID 
    CurrentDB.Execute ssql, dbFailOnError
    Me.Refresh
    All records in subform should be clear and the main form check-box should be unchecked.
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    account inactive in my sub form and still have other accounts active
    You're using terminology that is subject to interpretation. Maybe if I/we saw a pic or you were explicit in your description of things we would not be guessing - which I think we still are.

    I am an investment account holder. I have 3 accounts. Am I the account, or are my holdings defined as 3 accounts? If the latter, then what am I as the holder of 3 accounts? It all depends on the rules of the business. I could be the account and my 3 holdings are defined by their actual investment type names. Or I could be a client and my 3 holdings are "accounts". I still don't think this is clear, but I admit to being a black and white guy (things have specific meanings and when not specific I don't like to guess).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Click image for larger version. 

Name:	WriteConflict.jpg 
Views:	18 
Size:	40.8 KB 
ID:	51564

    Hi all and again thank you,
    I have tried both these methods with the first method It works except I get this Write Conflict?
    With second Method the FirstsSQL= I get a debug two few parameters, expected 1
    The second sSQL works, The Third and 4th sSQL I get the same Write Conflict?
    Posted Coade below and photo of write conflicts

    First Method
    Code:
    Private Sub ChkIsActive_Click()
        If ChkIsActive = False Then
        DoCmd.SetWarnings False
            DoCmd.RunSQL "UPDATE tblAccount SET IsActive = 0  WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID
            DoCmd.RunSQL "UPDATE tblAccount SET RecordLock = True  WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID
            DoCmd.RunSQL "UPDATE tblAccountDetail SET RecordLock = True  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID
            DoCmd.RunSQL "UPDATE tblAccountDetail SET AccountClosed = Now()  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID
            DoCmd.SetWarnings True
            Me.ChkRecordLock = True
            Me.TxtAccountHolder.SetFocus
        Else
            Me.ChkRecordLock = False
            Me.TxtAccountHolder.SetFocus
        End If
       
        Call Form_Current
    End Sub
    Second Method
    Code:
    Private Sub ChkIsActive_Click()
    
    
     Dim sSQl As String
    If ChkIsActive = 0 Then
    If MsgBox("Make account inactive?", vbYesNo) = vbNo Then Exit Sub
        sSQl = "UPDATE tblAccount SET IsActive = 0 WHERE tblAccount.AccountHolderID=" & Me.AccountHolderID
        'sSQl = "UPDATE tblAccount SET RecordLock = True  WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID
        'sSQl = "UPDATE tblAccountDetail SET RecordLock = True  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID
        'sSQl = "UPDATE tblAccountDetail SET AccountClosed = Now()  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID
    CurrentDb.Execute sSQl, dbFailOnError
    Me.Refresh
    End If
            Me.ChkRecordLock = True
            Me.TxtAccountHolder.SetFocus
      
        Call Form_Current
    End Sub
    I dont know why this wont work?
    Dave

  10. #10
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    HI all again,
    OK, found the error with parameters! My Bad
    So, with this I got this to work (Below) just as it should!

    Code:
    Private Sub ChkIsActive_Click()
    
    
     Dim sSQl As String
    If ChkIsActive = 0 Then
    If MsgBox("Make account inactive?", vbYesNo) = vbNo Then Exit Sub
        sSQl = "UPDATE tblAccount SET IsActive = 0 WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID
        'sSQl = "UPDATE tblAccount SET RecordLock = True  WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID
    CurrentDb.Execute sSQl, dbFailOnError
    Me.Refresh
    End If
            Me.ChkRecordLock = True
            Me.TxtAccountHolder.SetFocus
      
        Call Form_Current
    End Sub
    So, If I add one or both of the (Below) to this, then I get the write conflict and the above two do not work any longer?
    Why would that be?
    Code:
        'sSQl = "UPDATE tblAccountDetail SET RecordLock = True  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID
        'sSQl = "UPDATE tblAccountDetail SET AccountClosed = Now()  WHERE tblAccountDetail.AccountHolderID=" & Me.AccountHolderID

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Dave,
    You are leaving out a bit too many pieces of the puzzle.... Now you want to update two tables, but we don't know how they re related, which form (main\subform) is bound to which. To update multiple fields you simply combine them into one update SQL statement.
    In your example I think the first update will update the underlying table and because the subform is also based on that you get the write conflict.

    Code:
    Private Sub ChkIsActive_Click()
    
    
    Dim sSQl As String
    If ChkIsActive = 0 Then
    If MsgBox("Make account inactive?", vbYesNo) = vbNo Then Exit Sub
    'update tblAccount
    sSQl = "UPDATE tblAccount SET IsActive = 0, RecordLock = True WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID    
    CurrentDb.Execute sSQl, dbFailOnError
    'requery the subform
    Forms!frmAccountHolder.sFrmAccount.Form.Requery
    'update tblAccountDetail
    sSQl = "UPDATE tblAccountDetail SET AccountClosed = Now(), RecordLock = True WHERE tblAccount.AcctHolderID=" & Me.AccountHolderID    
    CurrentDb.Execute sSQl, dbFailOnError
    
    
    Me.Refresh
    End If
            Me.ChkRecordLock = True
            Me.TxtAccountHolder.SetFocus
      
        Call Form_Current
    End Sub
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    thank you so much! This works
    Youre the best!
    Dave

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

Similar Threads

  1. Use a popup form to select a record in the primary form
    By bashtonmcse in forum Programming
    Replies: 2
    Last Post: 07-28-2015, 12:58 PM
  2. How to Use Dcount in Form
    By alhareri in forum Forms
    Replies: 3
    Last Post: 12-30-2014, 04:42 PM
  3. Replies: 4
    Last Post: 11-25-2013, 04:47 PM
  4. DCount in a Form
    By killermonkey in forum Forms
    Replies: 5
    Last Post: 10-29-2013, 11:45 AM
  5. Customer Search Form different methods
    By pkstormy in forum Code Repository
    Replies: 0
    Last Post: 08-28-2010, 07:38 PM

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