Home > Software engineering >  VBA code for recalling the SelHeight Property Value of a subform is not working - syntax error?
VBA code for recalling the SelHeight Property Value of a subform is not working - syntax error?

Time:05-21

My aim: 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

Now the problem is that the Form.SelHeight Property keeps returning the Value 0 no matter how many records I select when I run the code.

I must be doing something wrong. , but the I'm not getting an error.

CodePudding user response:

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
  • Related