Transfer Dates from Userform to Database (Including Day Name) - excel

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

Related

preventing user from entering prior date in userform

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.

If one combobox is changed, reset the other combobox

I have two activeX comboboxes on a sheet: One for month and one for quarter.
The month combobox includes a "---" option which is representative of no month selected.
I also use the "---" character in the quarter combobox.
I would like if the user selects a month, not "---", the quarter box resets to "---".
Similarly, if the user selects a quarter, the month combobox should reset to "---".
My code in the sheet ("shDashboard" is the CodeName of the sheet. The boxes are named Quarter_Combo and Month_Combo.):
Private Sub Month_Combo_Change()
MsgBox "Month_Combo"
shDashboard.Quarter_Combo.Value = "—"
End Sub
Private Sub Quarter_Combo_Change()
MsgBox "Quarter_Combo"
shDashboard.Month_Combo.Value = shDashboard.Month_Combo.List(0)
End Sub
The message boxes are there to help troubleshoot.
When I change the month, it will update the quarter combobox which in turn will update the month combo box.
The result is that both boxes display "---".
I tried Application.EnableEvents = False.
Application.EnableEvents doesn't work in this case. Here is a workaround
Private pbDisableComboEvents As Boolean
Private Sub Month_Combo_Change()
If Not pbDisableComboEvents Then
MsgBox "Month_Combo"
pbDisableComboEvents = True
Me.Quarter_Combo.Value = "-"
pbDisableComboEvents = False
End If
End Sub
Private Sub Quarter_Combo_Change()
If Not pbDisableComboEvents Then
MsgBox "Quarter_Combo"
pbDisableComboEvents = True
Me.Month_Combo.Value = "-"
pbDisableComboEvents = False
End If
End Sub

Format of value in ComboBox is not showing correctly

So I have a ComboBox with a list of times on it, the ComboBox obtains the values from a sheet in the workbook and uses RowSource to put them on the ComboBox. (These cells are formatted as hh:mm)
Originally the ComboBox would display the values in the correct format but then when selecting one of these values it would show the selected time as a number rather than a time
For Example, 9:00 would show as 9:00 on the list of times but when selected would convert it to 0.375
To solve this issue I used the following code
Private Sub ComboBox5_Change()
ComboBox5.Value = VBA.Format$(ComboBox5.Value, "hh:mm")
End Sub
So now 9:00 is shown this way
This works perfectly for all of the times that I have on my list except for 12:00, when I select 12:00 it shows an error of
Does anybody know why this is only giving me an error when I select 12:00 as the time?
Try, please the next approach. It adds a small value (equivalent of a second), in case of 0.5...
Private Sub ComboBox5_Change()
Me.ComboBox2.Value = Format(IIf(Me.ComboBox5.Value = "0.5", _
"0.500011574074074", Me.ComboBox5.Value), "hh:mm")
End Sub
If you still have a problem, this can only be the result of the second time Change event being triggered by event itself code.
Since Application.EnableEvents = False does not work for form controls, we must build our own way of making the event to be triggered only once:
Create a Private variable on top of the form module:
Private NoEvents As Boolean
Adapt the Change event in the next way:
Private Sub ComboBox5_Change()
If Not NoEvents Then
NoEvents = True
Me.ComboBox5.Value = Format(IIf(Me.ComboBox5.Value = "0.5", _
"0.500011574074074", Me.ComboBox5.Value), "hh:mm")
End If
End Sub
Make the boolean variable false in an event being triggered after the Change one:
Private Sub ComboBox5_Click()
NoEvents = False
End Sub

Validate TextBoxes on a specific MultiPage page in an Excel UserForm

So I am trying to make sure that all TextBoxes are filled out. However, I only want this script to check a certain page on a MultiPage page on an Excel UserForm. Below is the code I'm using but it is checking every TextBox within the user form rather than the ones on the specific page. Also is a picture to show you. I have circled the Page named "Box" which is also Page 3 which I want to reference when validating the TextBoxes and the TextBoxes of which I would like to validate.
Snapshot of UserForm
Private Sub CommandButton2_Click()
Dim Ctrl As Control
Dim Answer1 As VbMsgBoxResult
' Checks to make sure data has been entered into all fields on the UserForm Page
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
If Ctrl.Value = vbNullString Then
MsgBox "All fields must be completed before the information can be updated.", vbOkay + vbExclamation, "Missing Information"
Exit Sub
End If
End If
Next
' Displays box with Yes or No asking if they are sure they want to save and close document
Answer1 = MsgBox("Doing this will overwrite the previous data that was entered. Are you sure you want to do this?", vbYesNo + vbExclamation, "Update Cabinet Information")
' Writes the new data entered into the workbook
If Answer1 = vbYes Then
Sheet5.Range("B1").Value = TextBox1.Value
If IsDate(TextBox2.Value) Then
Sheet5.Range("B2").Value = Format(TextBox2.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("B3").Value = TextBox3.Value
Sheet5.Range("G1").Value = TextBox4.Value
If IsDate(TextBox5.Value) Then
Sheet5.Range("G2").Value = Format(TextBox5.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("G3").Value = TextBox6.Value
Sheet5.Range("J1").Value = TextBox7.Value
If IsDate(TextBox8.Value) Then
Sheet5.Range("J2").Value = Format(TextBox8.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("J3").Value = TextBox9.Value
Sheet5.Range("M1").Value = TextBox10.Value
If IsDate(TextBox11.Value) Then
Sheet5.Range("M2").Value = Format(TextBox11.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("M3").Value = TextBox12.Value
Sheet5.Range("P1").Value = TextBox13.Value
If IsDate(TextBox14.Value) Then
Sheet5.Range("P2").Value = Format(TextBox14.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("P3").Value = TextBox15.Value
Sheet5.Range("S1").Value = TextBox16.Value
If IsDate(TextBox17.Value) Then
Sheet5.Range("S2").Value = Format(TextBox17.Value, "mm/dd/yyyy")
Else
MsgBox "One or more fields have an incorrect date format. Dates must be entered in this formay mm/dd/yyyy."
Exit Sub
End If
Sheet5.Range("S3").Value = TextBox18.Value
End If
End Sub
You'll need to refer to the Pages collection of the MultiPage object. Therefore, let's say that the multipage control is named MultiPage1, you'll need to amend the line as follows...
For Each Ctrl In Me.MultiPage1.Pages(2).Controls
Note that the index for the Pages collection is 0-based.

Excel 2013 64 bit - VBA Userform - Add 28 days to date of textbox and display in another textbox

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.

Resources