Search code examples
vbaformsms-access

Returning the SelHeight Property Value of a subform


I have a subform in a form and I want to pick up the value of one field in a user selected record in the subform and populate a field in my form using that value.

I am using .Selheight to determine the number of records selected in the subform. The code goes like this:

If Me.Frm_ICD10CMCodes.Form.SelHeight = 0 Then
    MsgBox "Please select one record. You have selected " & Me.Frm_ICD10CMCodes.Form.SelHeight & " records.", vbOKOnly, "Dr Talking:"
ElseIf Me.Frm_ICD10CMCodes.Form.SelHeight = 1 Then
    'Copy the diagnosis in the diagnosis box
ElseIf Me.Frm_ICD10CMCodes.Form.SelHeight > 1 Then
    MsgBox "Please select only one record.", vbOKOnly, "Dr Talking:"
End If

The Form.SelHeight property returns the value 0 no matter how many records I select when I run the code.


Solution

  • The selected records will be deselected as soon as you leave the subform.

    This works for me with a main form to display the count of selected records:

    Option Compare Database
    Option Explicit
    
    
    Private Sub Form_Current()
    
        Me!txtSelected.Value = Me.SelHeight
        
    End Sub
    
    
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        
        Me!txtSelected.Value = Me.SelHeight
    
    End Sub
    
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
        If Me.NewRecord = True Then
            Me!txtSelected.Value = 0
        Else
            Me!txtSelected.Value = Me.SelHeight
        End If
        
    End Sub
    
    
    Private Sub Form_Open(Cancel As Integer)
    
        Me.KeyPreview = True
    
    End Sub