Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2004, 04:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default ListBox SetFocus

I have a form with a ListBox on it. When the form opens I should like the first item in the ListBox to be selected.

Clive Astley
__________________
Clive Astley
 
Old May 3rd, 2004, 04:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 126
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Try this

Private Sub Form_Open(Cancel As Integer)
listbox.setfocus
End Sub

Only change 'listbox' to the name of your list box.

Regards,
Laura

The only thing standing between you and your goal is doubt. Quit doubting yourself and you'll be able to accomplish anything!
 
Old May 4th, 2004, 01:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Laura. I had already tried that before posting the question. That sets the focus to the listbox. But what I want is the first item in the list box to be selected.

Clive Astley
 
Old May 4th, 2004, 11:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Code:
If Me.lstMyListBox.ListCount > 0 Then
   Me.lstMyListBox.DefaultValue = Me.lstMyListBox.Column(0,0)
End If
This will make the default value the first item on the list IF there are data on the list.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old May 4th, 2004, 04:32 PM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just a couple of additional notes:

Greg's solution makes a few assumptions. The first is that the column headers are not visible since they are row 0 of the list data when they are visible and the first data row is then row 1. The second is that the first column of a multi column list box is the bound column. The list may be bound to a different column or to 0 (which returns the rowindex of the selected item(s)).

If you are working with a multiselect listbox, you have the option of setting the selected property of a list control row.

Me.lstName.Selected(0) = True

would select the first item if the column headers are not displayed. To check if they are displayed, check the value of Me.lstName.ColumnHeads. If true, add one to the offset of the desired selected row of the list.



Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]
 
Old May 5th, 2004, 01:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Many thanks Greg and Jurgen. That's just what I needed.

Best wishes,
Clive

Clive Astley
 
Old May 5th, 2004, 09:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Jürgen, good points about the assumptions. I have a question. If you do something like Me.lstName.Selected(0) = True, does that actually set the ball rolling and start a new record in the recordset? If someone goes to a new record and then navigates out without typing, does the record start up and have to be undone?

The reason why I set the default value instead is because this does NOT set a new record going. So he or she goes to a new record, the combobox will have a value but the record will NOT have data yet. If the user navigates away from the new record without doing anything, there's no harm done and no record to undo.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old May 5th, 2004, 11:07 AM
Authorized User
 
Join Date: Feb 2004
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Setting the selected property true/false does not work with anything but a multiselect listbox. It is read only with a single select list.

Your question pertains to a databound list (which is necessarily single select) or a databound combo (does not have a multiselect property option) and neither of these situations has the option of setting the selected property of a row as it is read only at runtime.

I believe the solution you presented is the only manner in which to set the value for a bound single select list or combo. Your specific question about whether there is a record insert is moot as it is a situation that cannot arise with an unbound control.

Ciao
Jürgen Welz
Edmonton AB Canada
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
this.SetFocus(Control) //with AJAX swifty_programmer ASP.NET 1.0 and 1.1 Professional 0 May 22nd, 2007 06:11 AM
Setfocus on textbox rtr1900 Classic ASP Basics 2 July 5th, 2006 05:40 AM
Continuous form / SetFocus help dbartelt Access 5 February 13th, 2006 03:59 PM
Setfocus from button on Commandbar simmy Access 2 December 29th, 2003 11:31 AM
Subform Setfocus Steven Access 4 June 11th, 2003 12:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.