Can anybody help me how to create a code in VBA that if day is Monday popup message "Your name is Jhon"
if day is Wednesday popup message "your name is Tom"

Thanks in advance
Andy

Recommended Answers

All 11 Replies

I added the following code on the Form1_Load Event:

'Ran Into Some Issues, New Code Soon!

Hope this helps and happy coding!

You can do it this way:

Dim date As DateTime = DateTime.Now
If (date.DayOfWeek = DayOfWeek.Monday) Then
    MessageBox.Show("Your name is John.")
ElseIf (date.DayOfWeek = DayOfWeek.Wednesday) Then
    MessageBox.Show("Your name is Tom.")
End If

Exceeded my Edit Quota so I have to start a new reply.

Steps Before Coding:
1.) Add a DateTimePicker

I added the following code on the Form1_Load Event:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim d As Date
        Dim dt As String
        d = DateValue(DateTimePicker1.Value)
        dt = Weekday(d)

        If dt = 2 Then
            MessageBox.Show("Your name is Jhon", "Today is Monday!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf dt = 4 Then
            MessageBox.Show("Your name is Tom", "Today is Wednesday!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ElseIf dt = 6 Then
            MessageBox.Show("Friday, Friday, Gotta Get Down on Friday!", "Rebecca Black = Awesome! :)", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            'Do Nothing
        End If
    End Sub
End Class

Explanation:
The reason I have numbers for the "dt = 2", "dt = 4", "dt = 6" is because the DateTimePicker assigns the day of the week a number:

Sunday = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7


Hope this helps and happy coding!

You can do it this way:

Dim date As DateTime = DateTime.Now
If (date.DayOfWeek = DayOfWeek.Monday) Then
    MessageBox.Show("Your name is John.")
ElseIf (date.DayOfWeek = DayOfWeek.Wednesday) Then
    MessageBox.Show("Your name is Tom.")
End If

Ughhh... I tried this but never got it to work! My code was exactly like yours, minus the parenthesis, and it didn't work. So, I inserted a DateTimePicker and just grabbed the integer from it :P

can you tell me how do i add datetime picker?

i'm using ms access 2003

Mitja,
i paste your code in my ms access 2003 into the on open form event,
when i run i get the following error:
compile error: expected identifier
and the date is highlighted.

When did Access come into play? You were just asking for the MessageBox display according to the day...

When did Access come into play? You were just asking for the MessageBox display according to the day...

You should have guessed it, after all you are replying to the VB.NET thread :D

On the OPs defense anchamal is asking for a VBA code...

You should have guessed it, after all you are replying to the VB.NET thread :D

On the OPs defense anchamal is asking for a VBA code...

Just because it is a VB.NET forum doesn't mean it is dealing with Access :icon_neutral:

Just because it is a VB.NET forum doesn't mean it is dealing with Access :icon_neutral:

Sheldon Couper, is that you? :D
I'm sorry for the confusion NetJunkie, I was being sarcastic. The fact that we are in the VB.Net part of the forum means that you were correct in your assumptions.

Back to the OP's request:

Private Sub Form_Load()
If Weekday(Now()) = 1 Then
MsgBox ("It's Sunday")
ElseIf Weekday(Now()) = 2 Then
MsgBox ("It's Monday")
ElseIf Weekday(Now()) = 3 Then
MsgBox ("It's Tuesday")
ElseIf Weekday(Now()) = 4 Then
MsgBox ("It's Wednesday")
ElseIf Weekday(Now()) = 5 Then
MsgBox ("It's Thursday")
ElseIf Weekday(Now()) = 6 Then
MsgBox ("It's Friday")
ElseIf Weekday(Now()) = 7 Then
MsgBox ("It's Saturday")
End If

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.