listbox focus on current date

THANOS

Registered User.
Local time
Today, 05:20
Joined
Aug 15, 2008
Messages
32
Goodevening to all!

I need again your kind assistance to my issue below:
I have a listbox on a form populated with various dates. I wondering if there is a way when I open the form to setfocus on the listbox to today's date and if current date value does not exist to popup a message asking to cancel.

Thank you in advance.
 
Thanos,

Code:
Dim i As Long
Dim Found As Boolean

Found = False

For i = 0 To Me.YourListBox.Count - 1
   If CDate(Me.ListBox.ItemData) = Date Then
      Me.YourListBox.Selected(i) = True
      Found = True
   Else
      Me.YourListBox.Selected(i) = True
   End If
   Next i

If Not Found Then
   MsgBox("Close Form, or whatever.")
End If

Wayne
 
Wayne, thanks for your prompt reply.

I tried to use the code on the open event of my form and I got various errors.

First on the line
For i = 0 To Me.YourListBox.Count - 1

says: method not found

and
If CDate(Me.ListBox.ItemData) = Date Then
If I leave Me.ListBox It says : Item not found and if I change to the name of my ListBox it says: Argument not optional.

Could you please assist on this? :confused:
 
Thanos,

Yes it has to be the name of your listbox.

Also, a type ...

Change --> CDate(Me.ListBox.ItemData)

To --> CDate(Me.ListBox.ItemData(i))

Wayne
 
nope. Still can't make it work.

Here is the issues

Dim i As Long
Dim Found As Boolean

Found = False

For i = 0 To Me.LstDate.ColumnCount - 1
If I use .Count I get an error debug saying method not found. So I had to change to .ColumnCount but I cant say if this is right or wrong. :(


Then
If CDate(Me.LstDate.ItemData(i)) Then
I got a debug message erron no 13 saying "type missmatch".

Me.LstDate.Selected(i) = True
Found = True
Else
Me.LstDate.Selected(i) = True
End If
Next i

If Not Found Then
MsgBox ("Close Form, or whatever.")
End If
End Sub

Crazy things :(
 
Thanow,

It's actually .ListCount

It's a little tougher without having it right in front of you.

hth,
Wayne
 
Wayne, thank you very much for your time spending with this.

I am that little close but still have problem. :(

I changed the code a bit (red fond) so I managed not to get debug errors but for some weird reason I cannot make it regognize the current date although it does exist in my list. Here is to code if it might help you.
Dim i As Long
Dim Found As Boolean

Found = False

For i = 0 To Me.LstDate.ListCount - 1
If CDate(Me.LstDate.ItemData(i) = "TDate") Then

Me.LstDate.Selected(i) = True
Found = True
Else
Me.LstDate.Selected(i) = False


End If
Next i

If Not Found Then
MsgBox ("Close Form, or whatever.")
End If
 
This will never be true:

If CDate(Me.LstDate.ItemData(i) = "TDate") Then

Even if TDate (the string) could match a list item, you can't convert a non-date string to a date. The words TDate cannot be converted to a date. Now, if TDate is supposed to actually be a VARIABLE then you would remove the quotes and move the variable OUTSIDE of the CDate argument:

If CDate(Me.LstDate.ItemData(i)) = TDate Then
 
Last edited:
Bob Thanks!

This seems to solve half of the problem since it does seem to recognize the field as a date field but selects the first date of the list dates and not the today's date. I enclose the database. Maybe that help you.

thanks again
 

Attachments

Well, to select the current date you would change this

If CDate(Me.lstDate.ItemData(i)) = TDate Then

to this:

If CDate(Me.lstDate.ItemData(i)) = Date Then

But, I am not sure how you would make the list box scroll down to the date so you can see it selected.
 
Thanks that works:)

As you said it doesn't scroll down but i can solve this by drag the listbox so to show more dates.

Many thanks again :):)
 

Users who are viewing this thread

Back
Top Bottom