I need help with this Code

A

Ayo

I got this code from a web site, and it look pretty simple and straight
forward but it is not working. And I can't figure out what the problem is.
Me.lblGreeting.Caption is not displaying anything even though its enabled and
visible properties are set to Yes. Help please.

Private Sub Form_Load()
Dim timeofDay as Variant, strGreString as String

If timeofDay > "00:00:00 AM" And timeofDay < "12:00:00 PM" Then strGreeting=
"Good Morning ..."
If timeofDay > "12:00:01 PM" And timeofDay < "5:00:00 PM" Then strGreeting=
"Good Afternoon ..."
If timeofDay > "5:00:00 PM" And timeofDay < "11:59:00 PM" Then strGreeting=
"Good Evening ..."

Me.lblGreeting.Caption=strGreeting

End Sub
 
B

Bob Quintal

I got this code from a web site, and it look pretty simple and
straight forward but it is not working. And I can't figure out
what the problem is. Me.lblGreeting.Caption is not displaying
anything even though its enabled and visible properties are set to
Yes. Help please.

Private Sub Form_Load()
Dim timeofDay as Variant, strGrestring as String

If timeofDay > "00:00:00 AM" And timeofDay < "12:00:00 PM" Then
strGreeting= "Good Morning ..."
If timeofDay > "12:00:01 PM" And timeofDay < "5:00:00 PM" Then
strGreeting= "Good Afternoon ..."
If timeofDay > "5:00:00 PM" And timeofDay < "11:59:00 PM" Then
strGreeting= "Good Evening ..."

Me.lblGreeting.Caption=strGreeting

End Sub
Somebody pulled your leg with this code.

Try
Select case timer / 3600
case < 12
Me.lblGreeting.Caption = "Good Morning..."
case < 17
Me.lblGreeting.Caption = "Good Afternoon..."
case else
Me.lblGreeting.Caption = "Good Evening..."
end select
 
K

Ken Snell \(MVP\)

Two problems:

1) You never set timeofDay variable to a value, so your code does not have a
value to compare against.

2) You've misspelled strGreeting in the "Dim" step.

Private Sub Form_Load()
Dim timeofDay as Variant, strGreeting as String
timeofDay = Time()
If timeofDay > "00:00:00 AM" And timeofDay < "12:00:00 PM" Then strGreeting=
"Good Morning ..."
If timeofDay > "12:00:01 PM" And timeofDay < "5:00:00 PM" Then strGreeting=
"Good Afternoon ..."
If timeofDay > "5:00:00 PM" And timeofDay < "11:59:00 PM" Then strGreeting=
"Good Evening ..."

Me.lblGreeting.Caption=strGreeting

End Sub
 
A

Ayo

Those where typos. I have it exactly the same way you wrote it and it still
isn't working.
 
A

Ayo

What is timer declared as?

Bob Quintal said:
Somebody pulled your leg with this code.

Try
Select case timer / 3600
case < 12
Me.lblGreeting.Caption = "Good Morning..."
case < 17
Me.lblGreeting.Caption = "Good Afternoon..."
case else
Me.lblGreeting.Caption = "Good Evening..."
end select
 
A

Ayo

Thanks Bob, a lot. That worked.

Bob Quintal said:
Somebody pulled your leg with this code.

Try
Select case timer / 3600
case < 12
Me.lblGreeting.Caption = "Good Morning..."
case < 17
Me.lblGreeting.Caption = "Good Afternoon..."
case else
Me.lblGreeting.Caption = "Good Evening..."
end select
 
F

fredg

Those where typos. I have it exactly the same way you wrote it and it still
isn't working.

We'll forgive you the misspelling but....
timeofDay should be declared as Date. <g>

This works for me:
Dim timeofDay As Date, strGreeting As String
timeofDay = Time()
If timeofDay > "12:00:00 AM" And timeofDay < "12:00:00 PM" Then
strGreeting = "Good Morning ..."
ElseIf timeofDay > "12:00:01 PM" And timeofDay < "5:00:00 PM" Then
strGreeting = "Good Afternoon ..."
ElseIf timeofDay > "5:00:00 PM" And timeofDay < "11:59:00 PM" Then
strGreeting = "Good Evening ..."
End If

Me.lblGreeting.Caption = strGreeting

Though a better syntax, using the date/time delimiter #, would be:

Dim timeofDay As Date, strGreeting As String
timeofDay = Time()
If timeofDay > #12:00:00 AM# And timeofDay < #12:00:00 PM# Then
strGreeting = "Good Morning ..."
ElseIf timeofDay > #12:00:01 PM# And timeofDay < #5:00:00 PM# Then
strGreeting = "Good Afternoon ..."
Else
strGreeting = "Good Evening ..."
End If

Me.lblGreeting.Caption = strGreeting
 
A

Ayo

Thanks so much Fred.

fredg said:
We'll forgive you the misspelling but....
timeofDay should be declared as Date. <g>

This works for me:
Dim timeofDay As Date, strGreeting As String
timeofDay = Time()
If timeofDay > "12:00:00 AM" And timeofDay < "12:00:00 PM" Then
strGreeting = "Good Morning ..."
ElseIf timeofDay > "12:00:01 PM" And timeofDay < "5:00:00 PM" Then
strGreeting = "Good Afternoon ..."
ElseIf timeofDay > "5:00:00 PM" And timeofDay < "11:59:00 PM" Then
strGreeting = "Good Evening ..."
End If

Me.lblGreeting.Caption = strGreeting

Though a better syntax, using the date/time delimiter #, would be:

Dim timeofDay As Date, strGreeting As String
timeofDay = Time()
If timeofDay > #12:00:00 AM# And timeofDay < #12:00:00 PM# Then
strGreeting = "Good Morning ..."
ElseIf timeofDay > #12:00:01 PM# And timeofDay < #5:00:00 PM# Then
strGreeting = "Good Afternoon ..."
Else
strGreeting = "Good Evening ..."
End If

Me.lblGreeting.Caption = strGreeting
 
B

Bob Quintal

What is timer declared as?

Timer is not declared, it's a vba function. see the help/

Q
Bob Quintal said:
Somebody pulled your leg with this code.

Try
Select case timer / 3600
case < 12
Me.lblGreeting.Caption = "Good Morning..."
case < 17
Me.lblGreeting.Caption = "Good Afternoon..."
case else
Me.lblGreeting.Caption = "Good Evening..."
end select
 
B

Bob Quintal

Thanks Bob, a lot. That worked.

You are welcome. Note I used 17 for the 5:00 PM in your code. around
here, evening starts at 6:00 PM. but is easy to fix.
Bob Quintal said:
Somebody pulled your leg with this code.

Try
Select case timer / 3600
case < 12
Me.lblGreeting.Caption = "Good Morning..."
case < 17
Me.lblGreeting.Caption = "Good Afternoon..."
case else
Me.lblGreeting.Caption = "Good Evening..."
end select
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top