VBA can't select a control

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

VBA can't select a control

Post by Jeff H »

The following code has been working perfectly, but suddenly I'm getting an error that focus can't be set on the PID control.

Code: Select all

    Me.PID.SetFocus
    DoCmd.FindRecord Me.SelectPID
    Me!SelectPID.SetFocus
    Me.SelectPID = Null
    Me.FirstName.SetFocus
SelectPID is a combo box. It displays all the PIDs alphabetically, with the patient names shown and the key field hidden. The bound field is PID and PIDs are indexed with no duplicates, so all are unique values. PID is a visible field set to not enabled and not locked.

I'm thinking there should be a better way to call the record, like maybe a recordset clone, but I'm posting to see if I can get a properly written, reliable procedure.

This applies to frmPatients and frmVolunteers in the same way.

- Jeff

User avatar
HansV
Administrator
Posts: 69488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA can't select a control

Post by HansV »

It works correctly in the sample database that I have...
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: VBA can't select a control

Post by Jeff H »

Are you sure? The one I sent you has no names. I checked the most recent one I sent you and also the one you returned to me and they aren't working. When you select a PID or VID you should see the chosen PID or VID text box on the left side of the form.

User avatar
HansV
Administrator
Posts: 69488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA can't select a control

Post by HansV »

Could you send it again? Thanks!

Found the latest version again.
Regards,
Hans

User avatar
HansV
Administrator
Posts: 69488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA can't select a control

Post by HansV »

You mentioned the problem yourself: the PID control has Enabled set to No so you cannot set focus to it.
If you want to use this code, you'll have to set Enabled to Yes.
An alternative that does not require PID to be enabled is:

Code: Select all

Private Sub SelectPID_AfterUpdate()
    With Me.RecordsetClone
        .FindFirst "PID='" & Me.SelectPID & "'"
        If Not .NoMatch Then
            Me.Bookmark = .Bookmark
        End If
    End With
    Me.SelectPID = Null
    Me.FirstName.SetFocus
End Sub
Regards,
Hans

Jeff H
3StarLounger
Posts: 316
Joined: 31 Oct 2017, 20:07

Re: VBA can't select a control

Post by Jeff H »

Yes, that's just what I was looking for.

I want PID disabled because I don't want anyone changing a PID accidentally. A while ago I had found a snippet similar to what you posted in Michael Alexander, but I wasn't adapting it correctly, so I just kept my old code. Apparently I changed the Enable property later and just hadn't tested the operation until now. I mistakenly thought that property applied to user interaction but not to programmatic access.

Your code is great. Thank you.

- Jeff