Im tying to prevent a user from entering an effective date that is prior to todays date in a text box up at the top of the userform.
the logic here seems fit, but im getting the msgbox even if the date is after todays date.
Private Sub txtEffective_Date_Change()
If IsDate(txtEffective_Date) Then
If cdate(txtEffective_Date) < Date Then
MsgBox "Date chosen is prior to today's date"
End If
End If
End Sub
I figure it has something to do with the cdate vs. date, but not entirely sure what's going wrong here.
For any one trying to do it on change event. You need to first ensure that your date is correct before doing comparison. e.g. 5/5 input becomes 5th may 2020 on CDate. Wait for the input to be correct and then compare.
Private Sub TextBox1_Change()
Dim sDate As String
If TextBox1.Text Like "??[/-]??[/-]????" Or _
TextBox1.Text Like "?[/-]?[/-]????" Or _
TextBox1.Text Like "?[/-]??[/-]????" Or _
TextBox1.Text Like "??[/-]?[/-]????" Then
sDate = Format(CDate(TextBox1.Text), "dd/MMM/YYYY")
Else
Exit Sub
End If
If IsDate(sDate) Then
If CDate(sDate) < Date Then
MsgBox "Previous date is not allowed...." + sDate
End If
End If
End Sub
The change event is firing for every keystroke, causing the issue you are experiencing. Simply move this code to another event, such as LostFocus or Validate, and you will be fine.
Edit: since this is VBA and not VB6, try the Exit event.
Related
In my excel code I want to add the date after information is entered into the form. I don't want to manually enter the date. It should just go in automatically once and should not be updated unless manually.
You can change the text to the most recently used date every time textbox is opened (or whatever trigger works best for you) like this:
Private Sub UserForm_Initialize()
Me.TextBox1.Value = Sheet1.Range("D" & Rows.Count).End(xlUp).Value
End Sub
And it comes out looking like this:
::
Additional References/Images:
Sub Rectangle1_Click()
UserForm1.Show
End Sub
This may or may not be possible but i have two textboxes (textBox20 and Textbox21... yes, i will change their names soon) and after i click a button, a third textbox displays the difference between both dates on days format. (All good so far.)
What i want is to be able to insert inside the database or... in another textbox and send it later to the same database, the day name which corresponds to each of both (textbox20 and textbox21), for example, if the day is: 20/02/2020, in the data base, it should show: thursday, 20/02/2020 or thursday, 20 of feb of 2020. If i do this in the same box, i can´t use the datediff function, and its a mess... I want something simple to transfer to the database, so later i can search for it and display it on another textbox later on... (im doing a little database)
Private Sub TextBox20_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox20
If IsDate(TextBox20.Text) Then 'Format as desired.
TextBox20.Text = Format(TextBox20.Text, "dd/mm/yyyy")
Else
TextBox20.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox21_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox21
If IsDate(TextBox21.Text) Then 'Format as desired.
TextBox21.Text = Format(TextBox21.Text, "dd/mm/yyyy")
Else
TextBox21.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub cmdCal_Click()
TextBox22.Value = DateDiff("d", (TextBox20.Text), (TextBox21.Text))
End Sub
I need to store a date as a global variable. I thought I was doing this but the watch shows it resetting after the sub ends. I've looked at countless global variable articles and responses to questions, nothing seemed to work.
So, just how do I stop this from happening in my VBA code as I need to call the variable in later subs?
Option Explicit
Public strDate As Variant
Sub SelectDate()
strDate = InputBox("Select Report Date (mm/dd/yy)", "Report Date", Format(Now() - 1, "mm/dd/yy"))
If IsDate(strDate) Then
strDate = Format(CDate(strDate), "mm/dd/yy")
Else
MsgBox "Valid Date Format Required"
End If
End Sub
This seems to be a simplistic task to get the actual date to display in the combobox versus 43466 (a number). The code that everyone seems to be using is the following with
.Text:
Private Sub ComboBox20_Change()
ComboBox20.Text = format(ComboBox20.Text, "dd/mm/yyyy")
End Sub
OR . Value
Private Sub ComboBox20_Change()
ComboBox20.Value = format(ComboBox20.Value, "dd/mm/yyyy")
End Sub
I have even tried with .text and .value. Either one keeps giving me the following error:
Compile Error:
Wrong number of arguments or invalid property assignment
Any thoughts on what I must be missing, having a brain meltdown on the most mundane and seemingly easy task.
Problem approach
The problem seems to be when you are attempting to change the value in the combobox, is there a value to begin with? If it is empty, it is taking the default version of the date.
Solution approach
Try to set the value before it goes into the Change event.
Sample code and demonstration
Private Sub ComboBox1_Change()
ComboBox1.Value = Format(ComboBox1.Value, "dd/mm/yyyy")
End Sub
Private Sub UserForm_Activate()
Dim CounterDate As Long
Dim TxtDate As String
For CounterDate = 1 To 2
TxtDate = DateAdd("d", CounterDate, Now)
ComboBox1.AddItem (TxtDate)
ComboBox1.Value = TxtDate
Next CounterDate
End Sub
Further comments
As you may see as soon as you change it, it enters again the "Change" event, you may turn off the events as soon as you enter the cycle and then turn them on again if it is causing erratic behaviour on your pc -sometimes it does-.
In my Userform I have a textbox "ADD_Inc_DATE_TXT" which the user manually enters the date of the incident.
I have another textbox "TxT_SWIRL_DueDate" which should display the result of the adding of 28 days to the date entered in the "ADD_Inc_DATE_TXT" textbox or even just add 1 month to the date of the incident.
I have tried this in a module named "Mod_SWIRL_Due_Date":
Sub SWIRL_ExpiryDate()
TxT_SWIRL_DueDate = CDate(ADD_Inc_DATE_TXT)
ADD_Inc_DATE_TXT = DateAdd("m", 1, ADD_Inc_DATE_TXT)
End Sub
But this doesn't seem to do anything at all.
I would like the "TxT_SWIRL_DueDate" to display the expiry date when the "ADD_INC_Time_TXT" textbox is selected.
And in anticipation of the User amending the Incident Date, I'd like it to update the TxT_SWIRL_DueDate with the amended date.
Further Information:
I have included below all the code I have relating to dates. I have 4 Date Pickers/Popup Calendars ... and none of the dates come up in the dd/mm/yyyy format. (having some problem with putting the code in the 'code sample' )
Private Sub Calendar1_Click()
ADD_Inc_DATE_TXT.value = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.LBL_Inc_Day_Type.Caption = Format(ADD_Inc_DATE_TXT.Text, "ddd")
End If
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Inc_DATE_TXT.Text = Format(ADD_Inc_DATE_TXT.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar2_Click()
TXT_AssetMgr_DATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_AssetMgr_DATE.Text = Format(TXT_AssetMgr_DATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar3_Click()
TXT_LastUserDATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_LastUserDATE.Text = Format(TXT_LastUserDATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar4_Click()
ADD_Date_ServiceJobLogged_TXT = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Date_ServiceJobLogged_TXT.Text = Format(ADD_Date_ServiceJobLogged_TXT.Text, "dd/mm/yyyy")
End If
End Sub
‘Under Private Sub UserForm_Initialize() I have the following date related code:
Private Sub UserForm_Initialize()
Me.ADD_Date_Recorded_TXT.value = Format(Now, "dd/mm/yyyy") ‘ this works perfectly (correct format is returned)
Me.ADD_Time_Recorded_TXT.Text = Format(Now(), "HH:mm")
‘***Note: my system date is dd/mm/yyyy and in the spreadsheet cells, the format is set to dd/mm/yyyy
Use this code in the form's code module (right click the text box, "View code"):
Private Sub ADD_Inc_DATE_TXT_AfterUpdate()
TxT_SWIRL_DueDate.Value = Format(DateAdd("m", 1, CDate(ADD_Inc_DATE_TXT.Value)),"dd/mm/yyyy")
End Sub
I recommend useing the AfterUpdate event, as the Change event fires every time the value in the textbox gets changed - eg also while typing. This only fires when the user has finished typing, and moved to the next tiem on the form (clicks somewhere else).
Also consider using some form of date control instead of textbox control. There are quite a few out there, and depending on the Excel version you are using, you may only be able to use a select few of those.