Issue with Combo Box

BittenApple
BronzeLounger
Posts: 1498
Joined: 01 Mar 2015, 02:03

Issue with Combo Box

Post by BittenApple »

Hello team,
What is the problem? When I fill out the control source for the combo box, I can not select anything from it.
Please see attached.
Regards,
tttttt
You do not have the required permissions to view the files attached to this post.

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

Re: Issue with Combo Box

Post by HansV »

The database that you attached contains a form with a single combo box. This form is bound to the table tbl_Hos. When you set the Control Source of the combo box to HosID, the primary key of tbl_Hos, the combo box will simply display the corresponding name. You can't select another item from the combo box since that would change the primary key of the table and cause the value to be duplicate. So there is no point in using a combo box on this form.
Best wishes,
Hans

BittenApple
BronzeLounger
Posts: 1498
Joined: 01 Mar 2015, 02:03

Re: Issue with Combo Box

Post by BittenApple »

Hello Hans,

In the attached database, I have filled the row source for the combo box, I had a case which I originally created the post for, I was not able to add anything to the row source of the combo box - as you said it created duplicate primary keys/ or wants to fill out the pk of the table which has already existing values in it. I want to know what is the difference when I am able to add value to row source of combo box and sometimes I am not be able to add anything to row source of a combo box.


Post 202294:
Hans:" Having a combo box bound to a table does not make the form bound to that table. The Record Source of your main form should be blank."


Post 201893:
Hans:"The database that you attached contains a form with a single combo box. This form is bound to the table tbl_Hos. When you set the Control Source of the combo box to HosID, the primary key of tbl_Hos, the combo box will simply display the corresponding name. You can't select another item from the combo box since that would change the primary key of the table and cause the value to be duplicate. So there is no point in using a combo box on this form."

Does a combo box make a form bound to a table or not?

Regards,
tttttt
You do not have the required permissions to view the files attached to this post.

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

Re: Issue with Combo Box

Post by HansV »

In the attached version, I have changed the combo box in the Detail section of the form to a text box. This control is bound to the HosID field; it should not be used to select the hospital.

I have added an unbound combo box cboHos in the Form Header section. The Control Source of this combo box is empty (that's why it's called unbound). Its Row Source is a query based on the table that sorts the records by HosName.
I have added code in the After Update event of this combo box to jump to the record of the selected hospital:

Code: Select all

Private Sub cboHos_AfterUpdate()
    With Me.RecordsetClone
        .FindFirst "HosID=" & Nz(Me.cboHos, 0)
        If .NoMatch Then
            Beep
        Else
            Me.Bookmark = .Bookmark
        End If
    End With
End Sub
I have also added a command button cmbNew to the form header. Clicking this button will jump to a new record; you can enter the name of a new hospital here.

Code: Select all

Private Sub cmbNew_Click()
    On Error Resume Next
    RunCommand acCmdRecordsGoToNew
    If Err Then Beep
End Sub
Finally, to make sure that a new hospital is displayed in this combo box, I added an After Insert event procedure to the form:

Code: Select all

Private Sub Form_AfterInsert()
    Me.cmbNew.Requery
End Sub
AddDataFB.zip
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

BittenApple
BronzeLounger
Posts: 1498
Joined: 01 Mar 2015, 02:03

Re: Issue with Combo Box

Post by BittenApple »

Hans,
Thanks a lot for the response.
I have to study it thoroughly.
Regards,
tttttt