auto select a record in a list box.

shutzy

Registered User.
Local time
Today, 22:48
Joined
Sep 14, 2011
Messages
775
i would like to auto select a record in a list box. in the list box there will always be only one item(record).

if anyone can help that would be great.

thanks
 
Try this code in the On Current event of your form to select the first Item in your listbox:
Code:
If IsNull(Me.NameOfYourtBox) Then   
      Me.NameOfYourtBox = Me.NameOfYourtBox.ItemData(0) 
End If
Just change the "NameOfYourtBox" to the actual name of your listbox.
 
cheers mr. b. this is actually me trying to do the sample you sent me, sampleselectmany in my own way. im getting there and i havnt used any vba yet.
 
Is your list box a multi-select list box? In other words, do you have the "Multi Select" property of your listbox set to anything but "None"?
 
no. the multi select option is 'none'
 
Ok. Try copying the code I provided in the post above and pasting that code in the "On Current" event of your form.

If you are not familiar with doing this, First, open your form in design view. Then be sure you have the form selected (not the listbox or any other control). In design view there is a square just below the title bar and above the ruller bar on the left. Click that square to be sure the form is selected. A small black square should appear when you have it selected.

Next, open the Properties window and click on the "Event" tab. The On Current event should be the first event listed. Double click in the space at the right of the "On current". You should see "[Event Procedure]" appear in that space. Then click on the Button with the 3 dots at the right end of this property.

The VBA code window will be presented with the following in it:

Option Compare Database
Option Explicit

Private Sub Form_Current()

End Sub

Just paste the code in the space between the "Private Sub Form_Current" and the "End Sub" lines.

When you have pasted the code, the VBA code window should look like this:

Option Compare Database
Option Explicit

Private Sub Form_Current()
If IsNull (Me.NameOfYourTextBox) then
Me.NameOfYourTextBox = Me.NameOfYourTextBox.Itemdata(0)
End If
End Sub

Now, just replace the "NameOfYourTextBox" with the actual name of your listbox control.

Open your form in form view and see if the first item is now selected.
 
thanks mr. b. i already know how to input vba but i dont really know how to write vba from scratch thats really why i have tried to stay away from it so far. i have a book on vba. its 1000+ pages long. it will take some reading but hopefully when i finish i will have a greater understanding and maybe be able to write my own code.
 
Sorry, for being so detailed with something that you already knew. I was not getting a clear picture of where you were coming from.

Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom