how to assign User defined date in vba excel - vba

I need to assign a date for the user to give manual date as input and make use of input at later stage. when the user inserted manual date , it has to be used for the naming conventions for the file name. Ex football 20160601.xlsx
I have gone through some website but could not able to find any answers as per my requirement.
My code:
Sub date()
date_test = Now()
Range("A1") = Format(date_test, "mm.dd.yy") ' how to aassign this date as a user defined date( as manual date) something like msg box but not msgbox.
End date

Dim inputDate as Date
Date = Format(InputBox("Enter a Date", "Input"),"yyyy-mm-dd")
Something like this?
If you want a Datepicker to select the desired date, then create a new userform and add the "Microsoft Date and Time Picker Control" (Tools->Additional Controls) to the Form. Then you can use it's value like this: Format(DatePicker1.Value,"yyyymmdd")

Related

MS Access form date range of single date yields no results

The form uses begin date and end date text boxes formatted as "General Date" to filter results. They are generally filled using the calendar date picker tool as format m/d/yyyy. ODBC SQL table that it's pulling the date field from is also showing format "General Date" in Design View but the field includes date and time as format m/d/yyyy H:MM:SS AM/PM.
When the same date is entered in the form begin and end date it shows no results, but I am told by my team in the past it has shown results. I am assuming that this is due to the form date picker including no time and defaulting to 12 AM in both boxes when the query runs. If that is/may be the case, is there a way to edit the end date text box to default to 11:59:59 PM of the date selected?
10/28 EDIT: I was asked to provide the SQL that refers to the end date text box. For context: the command button on the form opens query dbo_GEN_INSP_Count_Daily which is based on query dbo_GEN_INSP_Count_Monthly where the text box is mentioned. See below.
query dbo_GEN_INSP_Count_Daily
SELECT DateValue([INSP_DTE]) AS Insp_Date, Count([INSP_DTE]) AS DailyCount
FROM dbo_GEN_INSP_Count_Monthly
GROUP BY DateValue([INSP_DTE]);
query dbo_GEN_INSP_Count_Monthly
SELECT dbo_VEHICLE.DMV_VIN_NUM, dbo_INSPECTION_GENERAL.INSP_DTE, dbo_INSPECTION_GENERAL.CI_NUM, dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM, dbo_INSPECTION_GENERAL.VIP_UNIT_NUM
FROM dbo_INSPECTION_GENERAL INNER JOIN dbo_VEHICLE ON (dbo_INSPECTION_GENERAL.VIP_UNIT_NUM = dbo_VEHICLE.VIP_UNIT_NUM) AND (dbo_INSPECTION_GENERAL.DMV_FACILITY_NUM = dbo_VEHICLE.DMV_FACILITY_NUM) AND (dbo_INSPECTION_GENERAL.CI_NUM = dbo_VEHICLE.CI_NUM) AND (dbo_INSPECTION_GENERAL.INSP_DTE = dbo_VEHICLE.INSP_DTE)
WHERE (((dbo_INSPECTION_GENERAL.INSP_DTE) Between [Forms]![Form1]![StartDate] And [Forms]![Form1]![EndDate]));
I'm not sure how it would play with your ODBC connection, but in the date field in the form under the data tab in the property sheet there is a default value for the form, just like in the MS Access table. You could try to put your default time in there at 11:59:59, it will automatically put quotes around it and it should reflect in your form.
You could also create a default constraint on the database itself to reflect 11:59:59 for the end date if nothing is there. Going this route you would probably need to break up your time and date fields in the database and bring them together in MS Access with a function. That way if a date was placed there with no time it would default to 11:59:59.
When the user enters the same date for both StartDate and EndDate, you want the query to return all rows containing that date regardless of the time component stored with the date.
So, instead of your current Between ... And approach, make the endpoint target less than one day after EndDate.
WHERE
dbo_INSPECTION_GENERAL.INSP_DTE >= Forms!Form1!StartDate
And dbo_INSPECTION_GENERAL.INSP_DTE < (DateValue(Forms!Form1!EndDate) + 1)

How to display date in date field with SAP B1 SDK?

I know how to display value in field of matrix in SAP B1 SDK, but I don't know how to display date in date field.
The variable FromDate contains a date from database and I want to display it in date field.
Below VBS code failed:
mtx2.Columns.Item("C_1_5").Cells.Item(mtx2.RowCount).Specific.string = FromDate
Below VBS code displays nothing in the field:
mtx2.Columns.Item("C_1_5").Cells.Item(mtx2.RowCount).Specific.string _
= Format(FromDate, "dd/MM/yyyy")
Does anyone know how to set the value of a date field?
I did it. I converted variable instead of field of query
Thank you

Date display vs date format VBA

I think that I try to do something what is impossible. I have in range A1:A5 some dates. These cells have to have date format, because I’d like to check if they are bigger or smaller than certain date. Moreover I’d like to display these dates in a range A1:A5 in a following format: yyyy.mm. I think that there is no way to kill two birds with one stone.
Public Sub test()
Range("A1:A5").NumberFormat = "yyyy.mm"
Range("A1:A5") = Format(Date, "yyyy.mm")
If IsDate("A2") Then
Range("B2") = "OK"
End If
End Sub
Range("B1:B5").NumberFormat = "yyyy.mm" change the cells format on custom (yyyy.mm) but the dates are still displayed in unchanged form (yyyy.mm.dd)
Range (“A1:A5”) = Format(Date, “yyyy.dd”) displays the dates yyyy.mm but the year and month has been changed to 1905.07. Moreover the dates do not have Date format but Custom.
Is is really not possible to display a date like yyyy.mm but in date format (so that in formular bar the date is displayed as dd.mm.yyyy or another way around) to be able to compare them with different dates?
Best regards,
Neke
First of all, using points within dates can be a problem, because "2017.05" is interpreted by Excel as a number.
You can reformat the dates into "yyyy/mm" by using something like this:
For Each r In Range("A1:A5")
r.Value = VBA.Format(r, "yyyy/mm")
Next r

Format Date VARIABLE to short date VBA

I have a user inputted via a userform.textbox.value. General date that I want to switch to a short date. (Or try to get the time to drop so it reads 00:00). I tried the code
sDate=format(date,(d))
but that returns the current date rather than the variable date without the time. I mainly just want to drop the time so if there is an easier way to do that I would be accepting of the idea.
Please, see: Format function.
Usage:
'UserForm has a TextBox named TxtDate
Dim myDate As Date, sDate As String
myDate = Me.TxtDate 'DateSerial(2014,12,11)
sDate = Format(myDate, "Short Date")
'or
sDate = Format(myDate, "yyyy/MM/dd")

convert date in excel from dd-mm-yyyy to dd/mm/yyyy format and to check whether a date is valid or not

I'm having a hard time in converting the date from dd-mm-yyyy format to dd/mm/yyyy format.
For Example,
When i enter a date in excel as 25/02/2012 (dd/mm/yyyy), after entering the date if go in the next line it converts the date in the 25-02-2012 (dd-mm-yyyy) format.
what i want to do is that when i enter the date in (dd/mm/yyyy) format in excel it should keep it as it is and should not change it back to (dd-mm-yyyy) format when i go the next cell.
when i enter my date as the current system date my code gives me an error, i am having trouble validating the date i.e. is the date entered is a valid date or not
Sub valid_date()
' ---------------------------------------------------------------------
' Final Code - Trial
' ---------------------------------------------------------------------
Dim d1 As Variant
Dim IssueDate As Variant
Dim str As Variant
d1 = Worksheets("Sheet1").Cells(6, 1).value
MsgBox " The Issue Date format is " & d1
sysdate = Date
MsgBox "System Date is " & sysdate
If IsDateValid(d1) Then ' if date is in dd/mm/yyyy format then print this
If (d1 > sysdate) Then
MsgBox "Invalid date"
End If
End If
End Sub
Function IsDateValid(pdate) As Boolean
IsDateValid = False
Set RegExp = CreateObject("VBScript.RegExp")
' it only matches whether date is in dd/mm/yyyy format or not
'
' [1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1] ---> it allows the DATE from 01 to 31
' [1-9]|0[1-9]|1[0-2] ---> it allows the MONTH from 01 to 12
' 1[9][0-9][0-9]|2[0][0-9][0-9] ---> it allows the YEAR from 1900 to 2099
'
' below is the regular expression for checking the date in dd/mm/yyyy format
RegExp.Pattern = "^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[/]([1-9]|0[1-9]|1[0-2])[/](1[9][0-9][0-9]|2[0][0-9][0-9])$"
' check whether the date is in dd/mm/yyyy format or not....
tempdate = RegExp.Test(pdate)
If tempdate Then ' if tempdate is in dd/mm/yyyy format than proceed further
'If isdate(tempdate) Then ' if date is a valid date then proceed further
If isdate(pdate) Then
IsDateValid = True
Else
IsDateValid = False
End If
Else
IsDateValid = False
End If
End Function
i'm using the above mentioned code by using a regular expression to check whether the date is in dd/mm/yyyy format or not
but the problem which i'm facing is that it takes the date in excel as dd-mm-yyyy format whenever i enter the date in dd/mm/yyyy format.
i have updated my code a bit,
i also need one more help when i enter my date as the current system date it gives me error
for example,
when i enter my date as 09/09/2012 (suppose this your current system date) and when i check this date using IsDate method, it gives me an error
i have again edited my code,
Can anyone please help me on this
You don't need VBA/RegEx. Select the cells/columns where you input dates and create a Custom number format: dd/mm/yyyy. Now no matter how you type in a valid date (05-05-2000, 3-1-2010, 14/6-1990, etc.), it should be formatted as dd/mm/yyyy.
And, as Olle points out, you should use the Date object rather than Variant if you are going to be manipulating dates in VBA. This way you're working with the serial number and not a string with potential formatting issues.
First, I suggest you check the regional settings for dates on your computer. If you set it to use the "dd/mm/yyyy" format it will be used by Excel as well and hopefully remove the need for any RegEx VBA-code.
Second, if you do need to use VBA to reformat dates, I strongly suggest you use the Date data type instead of Variants. I also advise you to use Option Explicit at the top of your code and explicitly declare any variables in order to minimize typos and produce better quality code.
Third, I've looked through your code some more and it seems it will never work:
1. Because it is never declared, tempdate is a Variant
2. You assign tempdate to be a boolean, from the result of RegExp.Test(pdate)
3. So when you check IsDate(tempdate) it will always be false, since a boolean can never be a Date.
Again, if you use the Date data type, you can skip the RegEx... :)
I use Adobe online PDF to Excel and dates display correctly as MM/DD/YYYY but when extracting month (=Month()) it returns the DD portion. It is being interpreted as DD/MM/YYYY. I saved the file as a .csv closed and restarted excel and opened the .csv file and the dates were correct MM/DD/YYYY.