Listbox Selection

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

I have a form with a command button on it...when you press it a form
pops up with a listbox on it. I want to select a record in the listbox
and have the first form go to that record. How do I do this?
Thanks
DS
 
you might try the following in the listbox's AfterUpdate event, as

Dim frm As Form
Set frm = Forms!MyDataForm
frm.Recordset.FindFirst "MyPrimaryKeyField = " & Me!MyListBox
If frm.Recordset.NoMatch Then
MsgBox "No records found."
End If

substitute the name of your "first" form for MyDataForm. substitute the name
of the primary key field in your "first" form for MyPrimaryKeyField. and
substitute the real name of the listbox control in the popup form. the above
code assumes two things: the bound column of the listbox control matches
the primary key field of the "first" form, and the primary key field is a
number data type, not text.

hth
 
Make sure your form with a listbox has a Cancel button and has the following
code in the Click event:
DoCmd.Close
You don't need any other buttons on this form!
Put the following code in the AfterUpdate event of the listbox:
Me.Visible = False

Put the following code in the Click event of the first form:
DoCmd.OpenForm "NameOfListboxForm",,,,,acDialog
If IsLoaded("NameOfListboxForm") Then
Dim Rst As DAO.Recordset
Rst.FindFirst "[PrimaryKeyOfFirstForm] = " &
Forms!NameOfListBoxForm!NameOfListBox
DoCmd.Close acForm, "NameOfListboxForm"
End If

Note 1 -- You can find the IsLoaded Function in Northwind's standard modules
Note 2 -- The above FindFirst statement is for a numeric PK, use the
following for a string PK:
Rst.FindFirst "[PrimaryKeyOfFirstForm] = '" &
Forms!NameOfListBoxForm!NameOfListBox & "'"

Everything is done by one click of the button on the first form!!
 
tina said:
you might try the following in the listbox's AfterUpdate event, as

Dim frm As Form
Set frm = Forms!MyDataForm
frm.Recordset.FindFirst "MyPrimaryKeyField = " & Me!MyListBox
If frm.Recordset.NoMatch Then
MsgBox "No records found."
End If

substitute the name of your "first" form for MyDataForm. substitute the name
of the primary key field in your "first" form for MyPrimaryKeyField. and
substitute the real name of the listbox control in the popup form. the above
code assumes two things: the bound column of the listbox control matches
the primary key field of the "first" form, and the primary key field is a
number data type, not text.

hth
Thanks,
That worked!
DS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top