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

Dani AI

Generated

This thread mixes VB.NET (WinForms) answers with an Access/VBA question. and gave WinForms solutions (MessageBox.Show, DateTimePicker), which are fine for VB.NET but not directly usable in Access 2003. ’s Weekday approach is the closest VBA-style idea already in the thread, but the OP’s "compile error: expected identifier" points to typical Access/VBA issues (reserved names and event signatures).

The compile error with Date highlighted is usually caused by using a reserved word as a variable name. In Access VBA avoid naming variables Date, Time, Left, Right, etc. Also confirm the code is placed in the form’s event procedure correctly: open the form in Design view → Property Sheet → Event tab → set On Open to [Event Procedure] and click the “...” to edit. For startup behavior use the Form_Open(Cancel As Integer) event (runs before Form_Load).

A compact, Access-specific example to put in the form’s Open event (does not reuse the code already posted above):

Private Sub Form_Open(Cancel As Integer)
    Dim todayName As String
    todayName = LCase$(Format(Date, "dddd"))

    Select Case todayName
        Case "monday"
            MsgBox "Your name is Jhon", vbInformation, "Reminder"
        Case "wednesday"
            MsgBox "Your name is Tom", vbInformation, "Reminder"
    End Select
End Sub

Troubleshooting tips: add Option Explicit to force declarations, run Debug → Compile to catch name/reference problems, and check Tools → References for broken libraries. For non-English installs prefer Weekday(Date) with vbMonday/vbWednesday constants instead of string day names. If the popup must run when the database opens, either make this form the startup form or call it from an AutoExec macro.

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.