Displaying A Date in a Forms Text Box shows time for the first date

Strike_Eagle

Registered User.
Local time
, 19:13
Joined
Oct 20, 2011
Messages
48
Hello everyone and thank you for taking the time to view this posting.

I am using Access 2010 and have created a randomizer for dates that store in an array and then display those dates in a text box. It works great except that when it displays the results a time is shown then the correct number of dates that were randomized. How do I get the time to stop displaying on the text box display? Following is the code and output, please enjoy pointing out my, probably obvious, mistake!

Code:
Public Function GetRandomDate(lowerDate As Date, upperDate As Date) As Date
 
Randomize
 
GetRandomDate = Int((upperDate - lowerDate + 1) * Rnd + lowerDate)
 
Debug.Print GetRandomDate
 
End Function

Code:
Private Sub rButton_Click()
 
Dim lowerDate As Date
Dim upperDate As Date
Dim randomDate As Date
Dim dateArray() As Date
Dim dateCount As Integer
Dim i As Integer
Dim d As Integer
 
lowerDate = Me.startDate.Value
upperDate = Me.endDate.Value
dateCount = Me.numberOfDaysToRandomize.Value
 
For i = 1 To dateCount
 
randomDate = GetRandomDate(lowerDate, upperDate)
 
ReDim Preserve dateArray(i)
dateArray(i) = randomDate
Next i
 
Me.displayDatesTextBox.Value = ""
 
For d = 0 To dateCount
 
Me.displayDatesTextBox.Value = Me.displayDatesTextBox.Value & dateArray(d) & vbNewLine
 
Next d
 
End Sub

Display:
12:00:00 AM
4/26/2013
4/9/2013
4/23/2013
4/27/2013
4/24/2013
4/1/2013
4/3/2013
4/12/2013
4/24/2013
 
Last edited:
By the way, this was the input:

Date From: 4/1/13 to 4/30/13
9 days to randomize

It correctly gives 9 random days, but adds that time at the begining...
 
"It" does exactly what you told it to do. You are printing a zero-based array, from 0 to DateCount, But you have only put values in from 1 to DateCount. So the first one corresponds to 0, which is midnight 30 dec 1899 or so, which correctly gives 12 AM.
 
So I take it I simply mistyped and need to ignore my first ( '0' ) array element and display 1 to dateCount rather than 0 to datecount, correct?

And that seems to have done the trick... I knew it was something simple, much appreciated!
 
I don't use arrays much, but I note that the i variable increments from 1, but the d variable increments from 0.

My son-in-law (Strike Eagle pilot) is supposed to deploy this month.
 
Duh! This is what happens when you get a call while typing.
 
My best wishes to your son-in-law! Do you know where he will be deploying to? I will pray for a safe return.

You are correct. I simply missed my incrememnt. I needed to initialize and use from the same point. I intentially skipped the '0' element on the first For Loop, but forgot to on the second.
 
Always appreciate the prayers! I think UAE, but that may have changed.

I'll try to contribute to the thread by questioning the array usage. Why bother with it? Why not just populate the textbox in the first loop and skip the array?
 
Hopefully it won't change and things go well. My prayers will always be offered, I know I appreciated them while on active duty.

I am using the text box to verify my code. this is one part for what the total usage will be.

I am creating a random record generator that will generate first, a random number of days within a given time period, then random days within that time period (so these three days in this period, next usage might be 12 days within this period, etc etc) then a random number of records within each of those days.

Each variable is random and instead of using the immediate window, I used the text box for better viewing. Additionally, it will store these random records and then go to another form for processing.

This is phase 1 unfortunately, and little errors like that have been tripping me up since I haven't done coding for a while.
 

Users who are viewing this thread

Back
Top Bottom