Results 1 to 5 of 5
  1. #1
    BigJohn89 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    37

    Counter with empty textboxes

    Hi,
    I have a userform with 60 textboxes. What I am trying to do is to count the number of non-blank textboxes and put the quantity in another label. So that every time data is entered into a textbox, the label would automatically update with the total of non-blank textboxes.

    I have something like that and it almost works:

    Private Sub Form_AfterUpdate()
    Dim x As Integer
    Dim intC As Integer
    intC = 0
    For x = 1 To 60
    If IsNull(Me("slot_" & x)) Or (Me("slot_" & x)) = "" Then
    intC = intC + 1
    txtCount = intC
    End If


    Next
    End Sub

    But, when I assign number to the last empty textbox - counter is still showing "1" even though all textboxes are fill with the data.
    What I'm doing wrong?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That is only running when you leave the form or update it (not when you update each control). For that to work you'd probably have to force a save in every textbox. I'd probably make that a function and call it from the after update event of all the textboxes.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Is txtCount an UNBOUND textbox?

    After the code runs, are you on next record?

    You probably need to use the AfterUpdate event of each textbox, not the form AfterUpdate.

    EDIT: And I now see Paul has basically the same suggestion.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Kind of confusing...
    You say
    Quote Originally Posted by BigJohn89 View Post
    What I am trying to do is to count the number of non-blank textboxes and put the quantity in another label.
    The the code is
    Code:
    <snip>
        For x = 1 To 60
            ' If IsNull(Me("slot_" & x)) Or (Me("slot_" & x)) = "" Then
            If (Me("slot_" & x) & "") = "" Then  ' If slot is empty  (does the same thing as the line above)
                intC = intC + 1                  ' Add 1 to the variable  (increase the count of empty 'slots')
                txtCount = intC                  ' is "txtCount" a LABEL or a text box control???
            End If
        Next
    End Sub
    You say you want to count the NON-BLANK 'slots', but you are counting the BLANK 'slots'.
    If you want to put the quantity into a Label, you would need to update the Caption property of a Label.


    This updates the NON-BLANK slot count (I tested only using two slots)
    Code:
    Private Sub Slot_1_AfterUpdate()
        Call CountSlotsFull
    End Sub
    
    
    Private Sub Slot_2_AfterUpdate()
        Call CountSlotsFull
    End Sub
    
    
    Private Sub CountSlotsFull()
        Dim x As Integer
        Dim intC As Integer
        intC = 0
        For x = 1 To 2
            If Len(Trim(Me("slot_" & x) & "")) > 0 Then
                intC = intC + 1
            End If
        Next
    
        Me.txtCount = intC   '<< in this case, txtCount is a control, not a label
    
    End Sub

  5. #5
    BigJohn89 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Dec 2017
    Posts
    37
    Sorry guys - it was late hour.

    txtCount is an unbound textbox control and my code is counting blank textboxes (slots) as you already noticed
    I have this code behind Form_AfterUpdate(), Form_Current(), Form_Load()

    ssanfu - I'm not sure where I have to paste this code "Private Sub CountSlotsFull()"

    edit:
    Ok, I got it.
    I've used snafu's code but I needed to change it a little:

    Private Sub Form_AfterUpdate()
    Dim x As Integer
    Dim intC As Integer
    intC = 40
    For x = 1 To 40
    If Len(Trim(Me("slot_" & x) & "")) > 0 Then
    intC = intC + (-1)
    txtCount = intC
    End If
    Next
    End Sub

    After that my txtInput was showing blank control while form opening. I used:

    Me.txtCount.DefaultValue = 40

    In Form_Current() and Form_Load()

    So, many thanks guys for help Next step further to finish my project.

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

Similar Threads

  1. Replies: 27
    Last Post: 12-02-2015, 06:09 PM
  2. Counter
    By Skroof in forum Access
    Replies: 3
    Last Post: 05-14-2012, 08:25 AM
  3. Replies: 2
    Last Post: 04-23-2012, 10:13 PM
  4. Replies: 4
    Last Post: 11-20-2011, 01:08 PM
  5. Replies: 6
    Last Post: 07-01-2011, 02:11 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