Click on list Item and open a Detail form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Everebody,

I need help withe the following: I have a form that has a listbox that gets
its data through a query, the list box only show two colums: Company and
ProcessDate, also the query retrieves more fields but I set the width of the
columns to zero.
I also have a command button "CmdViewDetail" that on_click should open a new
form that will contain the rest of the information for the record I selected
on the list box.
How should I do this? I'm not very experienced with vba, could somebody help
me?
 
hi Sara,
I also have a command button "CmdViewDetail" that on_click should open a new
form that will contain the rest of the information for the record I selected
on the list box.
How should I do this? I'm not very experienced with vba, could somebody help
me?
Use the click event of the listbox:

Private Sub lbCompanies_Click()

Dim Condition As String

Condition = "ID=" & lbCompanies.Column(1)
DoCmd.OpenForm "yourForm",,, Condition

End Sub

Where Condition is a WHERE clause without the where. The index in the
..Column() function points to the column holding the primary key.


mfG
--> stefan <--
 
Sara,

Make sure the listbox has the keycolumn defined for the recordinfo you want
to show on the form. Usually this is the first column which columnwidth is
then set to 0 (zero).

Now put the following code behind the onclick from the "CmdViewDetail"
button:

docmd.openform "yourformname here",,"keyfield= " & me.listbox

Change the yourformname to the name of the form you want to open.
Change the keyfield to the name of the keyfield from the recordsource for
that form
Change the name of the listbox in me.listbox to the name of your listbox.

If the keyfield is a string you should type the following:
docmd.openform "yourformname here",,"keyfield= '" & me.listbox & "'"

That is single quote and a double qoute after the = sign and a double quote,
single quote and a double quote after the last &

hth
 
Thanks a lot Maurice and Stefan!!!

Maurice said:
Sara,

Make sure the listbox has the keycolumn defined for the recordinfo you want
to show on the form. Usually this is the first column which columnwidth is
then set to 0 (zero).

Now put the following code behind the onclick from the "CmdViewDetail"
button:

docmd.openform "yourformname here",,"keyfield= " & me.listbox

Change the yourformname to the name of the form you want to open.
Change the keyfield to the name of the keyfield from the recordsource for
that form
Change the name of the listbox in me.listbox to the name of your listbox.

If the keyfield is a string you should type the following:
docmd.openform "yourformname here",,"keyfield= '" & me.listbox & "'"

That is single quote and a double qoute after the = sign and a double quote,
single quote and a double quote after the last &

hth
 
Sara,

You might also want to consider disabling the command button unless an item
is selected in the list. You can do this by disabling the command button in
design view, then add some code in the Click event of the listbox to enable
the button.

Dale
 
Back
Top