Results 1 to 13 of 13
  1. #1
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7

    Select only fields with checked checkbox

    Good morning. In this sample, I have a "chkAll" checkbox and a "checkS" checkbox next to each record. When I check "chkAll" checks all entries and click copybutton(which has the code) copies all fields with selPh in txtCOPY.
    What I want is to check the checkS checkbox in each record and copies only those records.
    For example check test2 and test3(checkbox "checkS") and only those 2 selPh fields copy.

    Thank you
    Attached Files Attached Files

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    Run an append query,(not a copy)
    unless you are copying to paste into excel. If so, run an export.
    no need to 'copy'.

  3. #3
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,570
    Hi
    Seeing as I don't read Russian it is difficult to help
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  4. #4
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    Thank you for your answer ranman256. The problem is that append the cell phones(field selPh) on different rows not the same saperated by comma.

  5. #5
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Hi Sakis!

    Give a try to this code for the "Copy Button":
    (I have commented the unnecessary/wrong lines)
    Code:
    Private Sub copyF_Click()
        Dim rst As Object
        Dim strTemp As String
    
        'strTemp = vbNullString
        'If Nz(Form_STUDENTS.chkAll, False) Then
        'Set rst = Form_STUDENTS.RecordsetClone 'Wrong object reference!
    
        Set rst = Me!STUDENTS.Form.RecordsetClone
        With rst
            If Not .BOF Then .MoveFirst
            While Not .EOF
                If !CheckS Then
                    strTemp = strTemp & .Fields("SelPh") & ","
                End If
                .MoveNext
            Wend
        End With
        'End If
    
        Me.txtCOPY = Null
        If Len(strTemp) > 0 Then
            Me.txtCOPY = Left(strTemp, Len(strTemp) - 1)
        End If
        If Not rst Is Nothing Then Set rst = Nothing
    End Sub
    Also I suggest this simpler condition for the WHERE clause of the recordsource of the form STUDENTS:
    Code:
    WHERE (((STUDENTS.Ενεργός)=[Forms]![frmCOPY]![STUDENTS]![ChkFilter] Or (STUDENTS.Ενεργός)=-1))
    I hope it helps and welcome to accessforums!
    John

    Edit:
    But there is a better practice: You can move the whole functionality in the STUDENTS form as a Build-In, Read-Only property, making with this way much simpler the outer procedures.
    Add this Property Get method in the code module of the STUDENTS form:
    Code:
    Property Get Phones() As Variant
        Dim varTemp As Variant
        
        varTemp = Null
        With Me.RecordsetClone
            If Not .BOF Then .MoveFirst
            While Not .EOF
                If !CheckS Then
                    varTemp = varTemp & ("," + .Fields("SelPh"))
                End If
                .MoveNext
            Wend
        End With
        Phones = Mid(varTemp, 2)
    End Property
    Then, you have only one line of code to add in click event of the copy button (and any other procedure):
    Code:
    Private Sub copyF_Click()
        Me.txtCOPY = Me!STUDENTS.Form.Phones
    End Sub
    Last edited by accesstos; 12-29-2020 at 06:37 PM. Reason: Add a better practice

  6. #6
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    Perfect, both solutions work perfectly. Thank you very much.
    I wish you and everyone in the forum happy new year.

  7. #7
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    Good morning, if i want it the phone numbers not separated by comma but as if a line change is being made(as you see at image) then how will the code be configured?
    Property Get Phones() As Variant
    Dim varTemp As Variant

    varTemp = Null
    With Me.RecordsetClone
    If Not .BOF Then .MoveFirst
    While Not .EOF
    If !CheckS Then
    varTemp = varTemp & ("," + .Fields("SelPh"))
    End If
    .MoveNext
    Wend
    End With
    Phones = Mid(varTemp, 2)
    End Property
    Attached Thumbnails Attached Thumbnails new line.PNG  

  8. #8
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    I change the code like this
    " varTemp = varTemp & ("30" + .Fields("selPh") + vbNewLine) "
    but cuts the first digit of the first number from the set...
    The "30" is the prefix of country.

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,445
    because of this - the original code

    varTemp = varTemp & ("," + .Fields("SelPh"))

    the new code

    varTemp = varTemp & ("30" + .Fields("selPh") + vbNewLine)

    you are adding the new line to the end, not the beginning so when this bit of code runs

    Phones = Mid(varTemp, 2)

    you are chopping off the first character, which was a , and now is 3

  10. #10
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    I change the code like this
    " varTemp = varTemp & ("30" + .Fields("selPh") + vbNewLine) "
    but cuts the first number from the set...
    The "30" is the prefix of country.

  11. #11
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by smasak View Post
    I change the code like this
    " varTemp = varTemp & ("30" + .Fields("selPh") + vbNewLine) "
    but cuts the first number from the set...
    The "30" is the prefix of country.
    As Ajax said, you have to replace the comma with vbNewLine, which has two characters length, so, you have to start from the third character for the output.
    Code:
    '[...]
    varTemp = varTemp & (vbNewLine + "30" + .Fields("selPh"))
    '[...]
    Phones = Mid(varTemp, 3)
    End Property
    Edit:
    But, maybe you want to change the Property Get method with a function with an optional argument for the separator.That will give more control to outer procedures:

    (in STUDENTS form)
    Code:
    Function Phones(Optional strSep As String = vbNewLine) As Variant
        Dim varTemp As Variant
        
        varTemp = Null
        With Me.RecordsetClone
            If Not .BOF Then .MoveFirst
            While Not .EOF
                If !CheckS Then
                    varTemp = varTemp & (strSep + "30" + .Fields("selPh"))
                End If
                .MoveNext
            Wend
        End With
        Phones = Mid(varTemp, Len(strSep) + 1)
    End Function
    (in frmCopy form)
    Code:
    Private Sub copyF_Click()
        Me.txtCOPY = Me!STUDENTS.Form.Phones
        'Me.lstPhones.RowSource = Me!STUDENTS.Form.Phones(";")  'Rowsource for a listbox
        'Me.lblPhones.Caption = Me!STUDENTS.Form.Phones(" ")    'Caption for a label
    End Sub

  12. #12
    smasak is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2020
    Posts
    7
    Once again thank you very much.

  13. #13
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    You are very welcome!

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

Similar Threads

  1. Replies: 4
    Last Post: 07-06-2017, 03:09 AM
  2. Checkbox checked
    By brandy in forum Programming
    Replies: 2
    Last Post: 01-11-2017, 01:07 AM
  3. Checkbox Always Shows Checked
    By jo15765 in forum Forms
    Replies: 3
    Last Post: 08-16-2016, 06:58 AM
  4. If checkbox is checked allow query to run
    By burrina in forum Forms
    Replies: 14
    Last Post: 01-29-2013, 03:56 PM
  5. VBA for testing if checkbox is checked
    By rhewitt in forum Programming
    Replies: 0
    Last Post: 09-26-2012, 07:11 AM

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