I am trying to run an strSQL within Excel VBA between two date ranges.
See below part of my code relevant to this:
Dim DateMin As String
Dim DateMax As String
DateMin = Format$(Sheets("Setup").Range("c5").Value, "mm/dd/yyyy")
DateMax = Format$(Sheets("Setup").Range("c6").Value, "mm/dd/yyyy")
strSQL = "SELECT [COSTCENTRE_CODE],[PROJECT_CODE],[HOME_VALUE],[CT_DEADLINE] FROM [AA_CST_CENTRE_TRANSACTION_SIMPLE_VIEW] where [CT_DEADLINE] between #" & DateMin & "# AND #" & DateMax & "#"
When I run this I get an "incorrect syntax nea '#'" error. The format of cells C5 + C6 are in the same mm/dd/yyyy format - am using the American date format as believe SQL only uses American dates? I have tried adjusting the dates but no luck.
When I run "debug.print strsql" I get the below:
SELECT [COSTCENTRE_CODE],[PROJECT_CODE],[HOME_VALUE],[CT_DEADLINE] FROM [AA_CST_CENTRE_TRANSACTION_SIMPLE_VIEW] where [CT_DEADLINE] between #02/01/2020# AND #03/31/2020#
I have tried removing the # around the dates and I no longer get the error, however no data shows at all - there is definitely data there for these dates.
Anyone have any ideas as believe this is an SQL issue rather than an Excel VBA issue?
Does running this SQL work? SELECT [COSTCENTRE_CODE],[PROJECT_CODE],[HOME_VALUE],[CT_DEADLINE] FROM [AA_CST_CENTRE_TRANSACTION_SIMPLE_VIEW] just to make sure the issue is in the date part and not before. If this is not running then the issue is in this part and not in the date part.
And I believe SQL is using YYYY-MM-DD hh:mm:ss.sss as date format. Check that out if you have done the first suggested test and this query runs.
And as far as I know only Access accepts # but in SQL-Server you would neet to use ' instead (which should also work for Access).
Also note that
BETWEEN '02/01/2020' AND '03/31/2020'
is actually:
BETWEEN '02/01/2020 00:00:00.000' AND '03/31/2020 00:00:00.000'
So notice that you are missing anything that happened after 12am on 03/31/2020.
Related
My computer's regional date setting is dd/mm/yyyy. I am using MS Access. I would like to insert records into a database using the SQL INSERT INTO statement. When I try to insert a date using the #dd/mm/yyyy# syntax, and view the resulting record in the table after, it turns out the record displays the date in the format mm/dd/yyyy instead, but ONLY for the first 10 days of the month; if the day is 11 onwards, the record displays dd/mm/yyyy as intended.
For example if in SQL code I input #09/02/2022#, the table will display the record with the date 02/09/2022 instead. However if my SQL code is#11/02/2022#, then the correct order 11/02/2022 is shown in the record.
Please help.
Ok, the way this works?
You don't have to care, know, or think about the users regional format settings.
So, if you drop some control on a form? Just make sure that control is set to a date type format. Your done.
BUT ONE big whopper:
IN ANY AND ALL cases, your string based date format MUST be in USA format. Or you can use ISO date format.
dim MyDate as Date
MyDate = me.InvoiceDate
So, now we have a internal format date variable. How to insert into a table?
dim strSQL as string
strSQL = "INSERT INTO tblInvoice (InvoiceNum, InvoiceDate, InvoiceAmount " & _
"VALUES (1234, " & quDate(MyDate) & ",50)"
So, you ALWAYS format the date value into USA format.
You can type that format command over and over, but that fast becomes tiring.
so, I use a little helper function:
Public Function quDate(dt As Date) As String
quDate = "#" & Format(dt, "mm\/dd\/yyyy") & "#"
End Function
Public Function quDateT(dt As Date) As String
' return formatted date with time
quDateT = "#" & Format(dt, "mm\/dd\/yyyy HH:NN:SS") & "#"
End Function
So, you don't have to care about the date and regional format, but for a in-line SQL insert command that you build in code? Yes, you MUST convert to USA format of mm/dd/yyyy.
So, you can display dates in any format. For forms, for reports - not a problem.
However, the ONLY exception here is your code that builds a insert statement. That date string format must be #mm/dd/yyyy#.
Or, ISO:
#yyyy-mm-dd#
So, either format is fine, but it is a hard and fast rule that you must conform to.
So, from a text box on a form, if not data bound, then you want to ensure that the text box is set as a date type text box (fomrat date).
then in code:
dim strSQL as string
strSQL = "INSERT INTO tblFun (BirthDate) " & _
"VALUES (#" & format(txtDate,"mm/dd/yyyy") & "#)"
currentdb.Execute strSQL
Or, if you have that helper function, then this:
strSQL = "INSERT INTO tblFun (BirthDate) " & _
"VALUES (" & qudate(txtDate) & ")"
This question is related to this one, however I thought I'd create a new post since it's not the exact same issue, and the ideas on it were just being repeated.
I have a selection formula for my Crystal Report. It's supposed to select data where the Stage field in one table is 6, and the PaymentDate field in another is less than, or equal to, the value of the DateTimePicker control.
The code that I have below is working fine for most of the dates. However, say for example I have the following data in the database:
Sales_Headers.Stage = 6
Sales_Lines.PaymentDate = 28/01/2017 (January 28th, 2017)
When choosing a date of January 26th, up to January 31st, the data is only retrieved when the date is 28th or higher. However, if I then select a date of 1st February (Or the 1st of any month to be precise), it is returning the date as 02/01/2017, or, 2nd January 2017, so the data isn't shown.
Why is it changing for only the 1st of each month? All other dates are being read correctly, as dd/MM/yyyy, but on the first, it's using the MM portion as the dd portion.
I've tried:
Dim dateTo As Date = dtpCRTo.Value.AddDays(1).Date
dateTo = Format(dateTo, "dd/MM/yyyy")
If cmbCRSupplier.Value = "" Then
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} < #" & dateTo & "#"
Then I tried this in the form_Load event, as well as in the Value_Changed event of the DateTimePicker:
Dim dateFormat As String
dt.Format = DateTimePickerFormat.Custom
dt.CustomFormat = "dd/MM/yyyy"
dateFormat = dt.Text
The final thing I tried was just to have no formatting code, and just using:
If cmbCRSupplier.Value = "" Then
selectionformula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= #" & dtpCRTo.Value.Date & "#"
But it was the same result for all of them.
As well as the method #Siva was talking about (Always a good way, that way you can create the formula there and use the syntax that it's looking for), there is the way you're trying, to do it in VB.NET.
As I just mentioned, the syntax is important, and this is what is causing your issue.
I've not seen anyone try to use DateTimes in this method before, using `#value#.
The correct way to format dates into a RecordSelectionFormula (again, this is something you'll have seen if you'd have created it in Crystal itself), is to DATE(yyyy, MM, dd).
So, the correct way to syntax this is to use:
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= DATE(" & _
dtpCRTo.Value.Date.Year & "," & dtpCRTo.Value.Date.Month & "," & dtpCRTo.Value.Date.Day & ")"
Use this method to insert your dates into selection formulas in the future, that way you can't get it wrong.
If this doesn't work, then you need to check your Region and Local Date/Time settings in Control Panel, to ensure they're set correctly.
I face a problem, the "orinal date" in Excel are in different formats, once importing into SQL Server, some will change to NULL. So I want to unify the format in Excel as the column F with a formular as shown below.
The thing is even the format has changed to "Date" in EXCEL for column F, this column is still shown as "nvarchar" after imported into SQL SERVER.
So how can I change the column to date in SQL?
I have tried to use SELECT DISTINCT CONVERT(VARCHAR(100), [Change to], 112) but as the code has many many date range criteria in the where clause, it is not very friendly to use so many convert clause for each of the where clause.
I have also tried to change the format from nvarchar to date when importing, but it reports errors.
The 2 methods don't function, so I am eager to have some advice from you.
Many thanks in advance for your help.
You can use something like this to convert to a datetime format varchar for import:
=year(e4) & "-" & right("0" & month(e4), 2) & "-" & right("0" & day(e4), 2) & "T00:00:00"
This will ensure that it uses the two digit format for month and day each time.
You can also use:
=date(year(e4), month(e4), day(e4))
This will convert it to an Excel date format.
I have the following unbound fields in a form:
txtCBDate - format "Medium Date" - My PC localization settings for date: "dd/mm/yyyy"
txtCBRemarks.
In VBA, for cmdSAVE_Click event, I have the following code:
Dim myDate as Date
Dim myRemarks as String
myDate = txtCBDate.Value
myRemarks = txtCBRemarks.Value
Sql = insert into CB (CBDate, CBRemarks) values (myDate, myRemarks)
db.Execute(SQL)
The values are inserted into the table, CB. However, the inserted date is a wrong date, i.e., not the date entered by me in the form. e.g., if I entered "20-02-2016", the date saved in the database is "11-07-1894". The other dates entered are also saved correspondingly with reference to the year 1894.
I tried "Compact and Repair Database" as well as deleting the textbox and re-designing the form, but to no avail. Where am I going wrong?
Usually there are different formats in your machine's date and time settings. For example, on my machine, short date is dd/MM/yyyy and long date is dd MMMM yyyy (I don't seem to have a medium date format visible in my settings).
I would suggest trying changing the format of the text box to short date, on the assumption that the date format in your settings is actually your short date format opposed to your medium date format.
While debugging the value of variable 'myDate', as suggested by Mark, I found that in the Insert statement, myDate was not enclosed in single quotes.
Sql = insert into CB (CBDate, CBRemarks) values (' " & myDate & " ', & " ' myRemarks & " ')
I only had the double quotes "" enclosing the variables. After enclosing the variables in single quotes as well, the issue got resolved.
I am sorry that I did not make a mention regarding the single/double quotes in my earlier post. Thanks in particular to Mark C, and, also to UberDoodles for their suggestions.
The format function in vba is changing the date. e.g for format("3/12/2009","DD/MM/YYYY"), the function returns 12/03/2009 where "3/12/2009" is what excel vba reads from a cell that has the value 12-Mar-2009 and the format as "dd-mmm-yyyy"
No it's not.
If a date-as-string is passed to the Format function, it will parse it using current regional settings. Your settings are obviously MM/DD/YYYY which is default for USA. Nothing prevents Excel from displaying a date as DD/MM/YYYY if set manually, but by default it would display MM/DD/YYYY.
To do: Stop reading dates as strings. Read them as dates.
dim d as date
d = activecell.value
Had few times problem myself where VBA in Access reades most dates as europian but some as USA:
This DOES NOT work properly:
myRs.FindFirst ("Date =#" & myDate & "#")
This works:
myRs.FindFirst ("Date =#" & Format(myDate, "Long Date") & "#")
The long date (eg 01 January 2012) clearly makes the difference between month and day