Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 3rd, 2004, 04:02 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Norwich, Norfolk, United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old May 3rd, 2004, 04:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Connersville, Indiana, USA.
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!
Reply With Quote
  #3 (permalink)  
Old May 4th, 2004, 01:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Norwich, Norfolk, United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old May 4th, 2004, 11:48 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #5 (permalink)  
Old May 4th, 2004, 04:32 PM
Authorized User
 
Join Date: Feb 2004
Location: Edmonton, Alberta, Canada.
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
jwelz@hotmail.com
Reply With Quote
  #6 (permalink)  
Old May 5th, 2004, 01:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Norwich, Norfolk, United Kingdom.
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
Reply With Quote
  #7 (permalink)  
Old May 5th, 2004, 09:13 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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
Reply With Quote
  #8 (permalink)  
Old May 5th, 2004, 11:07 AM
Authorized User
 
Join Date: Feb 2004
Location: Edmonton, Alberta, Canada.
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
jwelz@hotmail.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:13 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.