Go to Record when select on listbox (1 Viewer)

Akai90

Member
Local time
Tomorrow, 03:25
Joined
Feb 8, 2022
Messages
65
hi,

forms record will show data when select on listbox

When i select data on listbox record will show on forms.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:25
Joined
May 7, 2009
Messages
19,230
the listbox should not be Bound to a field in a table.
add code to it's AfterUpdate:

DoCmd.SearchForRecord , , , "id=" & Me.Listbox1
 

Akai90

Member
Local time
Tomorrow, 03:25
Joined
Feb 8, 2022
Messages
65
the listbox should not be Bound to a field in a table.
add code to it's AfterUpdate:

DoCmd.SearchForRecord , , , "id=" & Me.Listbox1
"id=" is my table primary key ? or my first column query ?
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:25
Joined
May 7, 2009
Messages
19,230
your primary key, the First Column (bound column) of your Listbox.
 

Akai90

Member
Local time
Tomorrow, 03:25
Joined
Feb 8, 2022
Messages
65
C:
Private Sub List71_AfterUpdate()
DoCmd.SearchForRecord , , , "noid=" & Me.List71
End Sub

this my code
it will go to record i select for second. but it will back on last record.

my forms open code
C:
Private Sub Form_Open(Cancel As Integer)
Me.List71.SetFocus
DoCmd.GoToRecord , , acLast
End Sub

my datasource
1655361586847.png
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:25
Joined
Feb 28, 2001
Messages
27,163
First, a warning: acLast for any table is unreliable (as is acFirst). A table has no inherent order. A query with an ORDER BY has an order only because you defined the order. Don't bet on going to the first or last record to see a specific record. Use Max or Min .

Next, if you are going to try this, the USUAL way (not the only way, certainly) is to recognize that your form has two recordsets. One is the main .RecordSource that defines form.Recordset; the second is the .RecordSetClone, which is a recordset based on the same set defined for your main .Recordset, except you can manipulate it without messing up the form.

So you do something similar to

Code:
Me.RecordsetClone.MoveFirst
Me.RecordsetClone.FindFirst "field = value"
IF Me.RecordsetClone.NoMatch then
    'something wrong here - take action like a warning or something
Else
    Me.Recordset.Bookmark = Me.RecordsetClone.Bookmark
End If

You just have to supply a value to be matched against a particular field. That .FindFirst argument might have to involve concatenation depending on the nature of the field in the list box. The one thing you might wonder about is that .MoveFirst, which will let you search from the first record in the table. This is the ONE time that acFirst has value, because it lets you search the whole table.
 

Users who are viewing this thread

Top Bottom