Solved Auto-choosing first in list in null list (1 Viewer)

DalGal

Registered User.
Local time
Yesterday, 20:20
Joined
Oct 23, 2018
Messages
23
I have a list box on a form which pulls loans based on the center chosen in the first field on the form.
It automatically chooses the first loan to prevent a non-record chosen.
I am getting an error when I have a center with no loans.
This is the code (Event Procedure) to choose the first-when not null, but I'm still getting the error. I am a novice with Access, so what am I missing? The list box is named Loanlists.
Private Sub LoanLists_GotFocus()
If Not IsNull(Me.LoanLists) Then
Me.LoanLists = Me.LoanLists.ItemData(0)
End If
End Sub
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:20
Joined
Mar 14, 2017
Messages
8,738
Check the listbox's bound column, and make sure that's the one that's potentially null.
Otherwise, specific the column of the listbox.
 

June7

AWF VIP
Local time
Yesterday, 17:20
Joined
Mar 9, 2014
Messages
5,423
If Not IsNull(Me.LoanLists) Then works for me.

If listbox is bound to field, listbox can have value regardless if there are no items in list.

What is exact error message?

Is listbox bound to field? What is field name? What is RowSource?

If you want to provide db for analysis, follow instructions at bottom of my post.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:20
Joined
May 7, 2009
Messages
19,169
I am getting an error when I have a center with no loans.
check the LoanLists.ListCount. a count of 1 means it has no items.
Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And IsNull(lst) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Sub
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:20
Joined
Mar 14, 2017
Messages
8,738
check the LoanLists.ListCount. a count of 1 means it has no items.
Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And IsNull(lst) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Sub
That is, if you have headers
 

DalGal

Registered User.
Local time
Yesterday, 20:20
Joined
Oct 23, 2018
Messages
23
check the LoanLists.ListCount. a count of 1 means it has no items.
Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And IsNull(lst) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Sub


Woohoo!!! Thank you-It worked!!!
 

DalGal

Registered User.
Local time
Yesterday, 20:20
Joined
Oct 23, 2018
Messages
23
check the LoanLists.ListCount. a count of 1 means it has no items.
Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And IsNull(lst) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Sub

But now, it is not automatically choosing the first in list when the center does have loans.
 

Isaac

Lifelong Learner
Local time
Yesterday, 18:20
Joined
Mar 14, 2017
Messages
8,738
Does this work:

Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And (IsNull(lst)=false) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Su
 

moke123

AWF VIP
Local time
Yesterday, 21:20
Joined
Jan 11, 2013
Messages
3,852
What exactly is the error message?

Are you sure you want the gotfocus event?
 

DalGal

Registered User.
Local time
Yesterday, 20:20
Joined
Oct 23, 2018
Messages
23
Does this work:

Code:
Private Sub LoanLists_GotFocus()
Dim lst As Listbox
Set lst = Me.LoanLists
If lst.ListCount > 1 And (IsNull(lst)=false) Then
    lst = lst.ItemData(0)
End If
Set lst = Nothing
End Su
nope still doesn't automatically choose the first in the list
 

moke123

AWF VIP
Local time
Yesterday, 21:20
Joined
Jan 11, 2013
Messages
3,852
Using the gotfocus event is not going to do anything until you tab to the list or click on it.
 

Cronk

Registered User.
Local time
Today, 12:20
Joined
Jul 4, 2013
Messages
2,770
From #1
I have a list box on a form which pulls loans based on the center chosen in the first field on the form.
It automatically chooses the first loan to prevent a non-record chosen
Why not simply exclude any "non-record" from the row source of the list box?
 

Users who are viewing this thread

Top Bottom