Problem with Regional Settings Date, Time Format - Visual Basic 6.0

When the regional format date separator, time separator is set to ":" and further when tried to do the following it gives the runtime error 13 type mismatch

Public Function RndTime() As String
    Dim VTime As Date
    VTime = "11:" & Minute(Time) & ":" & Second(Time) & " PM"
    VTime = DateAdd("h", Int(Rnd * 7), CTime)
    RndTime = Format(VTime, "hh:mm AMPM")
End Function

Note: the purpose of the function is to return a time between 11 PM and 6 AM

where as when the separator is different for date, time it works fine

What could be the solution to this?
LVL 4
kenshawAsked:
Who is Participating?
 
Ryan ChongCommented:
Tried my example posted above, kenshaw? it works fine for me...

regards
0
 
GrahamSkanRetiredCommented:
It works OK for me.
I've topped & tailed it like this

Dim CTime As Date
Dim RndTime As String
CTime = Now
    Dim VTime As Date
    VTime = "11:" & Minute(Time) & ":" & Second(Time) & " PM"
    VTime = DateAdd("h", Int(Rnd * 7), CTime)
    RndTime = Format(VTime, "hh:mm AMPM")
MsgBox RndTime
0
 
Ryan ChongCommented:
try this:

Private Sub Form_Load()
    For I = 1 To 20
        Debug.Print I & ": " & RndTime
    Next I
End Sub

Public Function RndTime() As String
    Dim VTime As Date, rndSecond  As Long
    Randomize
    rndSecond = Int(Rnd * Abs(DateDiff("s", DateAdd("h", 18, Date), DateAdd("h", 11, Date))))
   
    RndTime = Format$(DateAdd("s", rndSecond, DateAdd("h", 11, Date)), "hh:mm AMPM")
End Function
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
kenshawAuthor Commented:
in reply to GrahamSkan's comment:

still it gives the runtime error
0
 
kenshawAuthor Commented:
in reply to ryancys's comment

this code works without runtime error but the logic of 11:00 PM to 6:00 AM doesn't work
0
 
GrahamSkanRetiredCommented:
Which line give the error?
0
 
Ryan ChongCommented:
sorry for the last reply....

i think i mess up with the time on previous example, for the logic of 11:00 PM to 6:00 AM, try this:

Public Function RndTime() As String
    Dim VTime As Date, rndSecond  As Long
    Randomize
    rndSecond = Int(Rnd * Abs(DateDiff("s", DateAdd("h", -1, Date), DateAdd("h", 6, Date))))
   
    RndTime = Format$(DateAdd("s", rndSecond, DateAdd("h", -1, Date)), "hh:mm AMPM")
End Function

cheers
0
 
kenshawAuthor Commented:
hi GrahamSkan,

the following line is causing the runtime error.
  VTime = "11:" & Minute(Time) & ":" & Second(Time) & " PM"

  note: VTime = DateAdd("h", Int(Rnd * 7), CTime) in this line instead of CTime=>VTime should be used

the problem with the ":" date separator affects not only this function but in various places whereever the data assignments are used.
0
 
GrahamSkanRetiredCommented:
I still can't reproduce the problem. I even changed my (UK) regional setting from ':' to ';', and it still doesn't fail. If I change to ';' in the code, it fails if ';' is not the setting. Changing the setting and the code to a letter always fails, though displays will actually show the letter.

It sounds as if it is particular to your system. Where in the world are you?

ryancys, can you reproduce it in Singapore?

Meanwhile this is a getround:

 VTime = TimeSerial(23, Minute(Time), Second(Time))
0
 
Ryan ChongCommented:
>>ryancys, can you reproduce it in Singapore?

I tried this:

Sub test()
    Dim CTime As Date
    Dim RndTime As String
    CTime = Now
    Dim VTime As Date
    VTime = "11:" & Minute(Time) & ":" & Second(Time) & " PM"
    VTime = DateAdd("h", Int(Rnd * 7), CTime)
    RndTime = Format(VTime, "hh:mm AMPM")
MsgBox RndTime
End Sub

The RndTime always returns minutes part as "59", not error prompt out during my testing.

My regional setting is "English (United States), and the Date format is set as "dd/MM/yyyy"

regards
0
 
kenshawAuthor Commented:
>> It sounds as if it is particular to your system. Where in the world are you?

i have tested in more than 5 systems(XP/W2K), this problem occurs when date separator is used as ":", also when any separator such as ":" or "/" or "." is used in both date separator and time separator i am not able to assign the date values to the date picker control as it gives the runtime error.

my regional settings is English[United States]
date setting is m:d:yyyy
time setting is hh:mm:ss tt

regards
0
 
GrahamSkanRetiredCommented:
It's rather unusual to have the date and time separator to be the same.

Yes I can reproduce it on my system if I set them the same.
It seems that VB tries to make a date from the string, but it is out of range.

So this doesn't give an error:
VTime = "5:" & 4 & ":" & 2005

but this does:
VTime = "99:" & 99 & ":" & 2005
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.