Hi Ayo,
Here's some code from a combo box in one of my apps that does what you want.
As you can see, it's located in the after_update event of the combo box.
___________________________________________
Private Sub cmbQuickFile_AfterUpdate()
Dim rs As Object
On Error GoTo cmbQuickFile_AfterUpdate_Error
If Me.Dirty Then
If MsgBox("Save changes to current record before moving to new
record?", vbYesNo, _
"Save changes?") = vbYes Then
Me.Dirty = False
Else
Me.Undo
End If
End If
Set rs = Forms!Files.Recordset.Clone
rs.FindFirst "[ID] = " & Me![cmbQuickFile]
If Not rs.EOF Then Forms!Files.Bookmark = rs.Bookmark
Exit_cmbQuickFile_AfterUpdate:
Exit Sub
cmbQuickFile_AfterUpdate_Error:
Call LogError(Err.Number, Err.Description, "cmbQuickFile_AfterUpdate",
Forms!Files.Name, , True)
End Sub
__________________________________________________
The msgbox line in the early part of the code is really one line. It
shouldn't have returned like it did above. If you make it one line in your
code, remove that underscore from it.
The above code includes a check to see if the record has been modified, and
if so, asks the user if they want to save it.
The rowsource for the combo box is a select query that, most importantly,
includes the primary key from the query that populates the form (IssuesQuery)
in question. In addition, it has a couple of fields that my users use to
identify the records (since they will not see the primary key field in the
combo box and wouldn't know how to associate it with a record even if they
did). The select query looks something like this:
Select CaseID, CaseName, CaseNumber from IssuesQuery Order by CaseName,
CaseNumber; In my case, users type in the caseName into the combo box to
find the records. I'm not altogether pleased with that arrangement (since
two cases can have the same name), but since not all cases have case
numbers.....I didn't see a better alternative.
You need to set the column count in the properties window for the combo box
to the appropriate number (3 in my case), and also set the column widths so
that the primary key (CaseID in my example) is not viewable, but the other
fields are. In my example, the column widths are: 0;1";1"
That code should work for you as long as you change all the names to reflect
your application.
I hope that helps,
CW
Ayo said:
I have a form and on the form there is a combo box. The form also contains
lots of text boxes. What I want to do is select a value in the combo box and
have the form display the record of the selection in the combo box. I was
using this code:
DoCmd.ShowAllRecords
Me!txtSiteNumber.SetFocus
DoCmd.FindRecord Me!cboSiteNumber
in the AfterUpdate event of the combo box, but all it does is change the
value in the txtSiteNumber. I also tried:
DoCmd.OpenForm Me.Name,,, "[Site Number]='" & Me.cmbSiteNumber.Column
(0) & "'" & ""
with the same result.
Any ideas what I am doing wrong? Thanks.