Excel VBA Script Format Date MM-DD-YYYY - vba

I'm need to convert the value of two cells from dd/mm/yyyy to mm-dd-yyyy and append the values to a stored procedure call. My code:
Dim dtInicio As String
Dim dtFim As String
Set stDate = CStr(MWS.Cells(1, "E").Value)
Set enDate = CStr(MWS.Cells(2, "E").Value)
I need something like this after the above code:
wstring = "exec sp_accessFile '"+stDate+"', '"+enDate +"'

You don't need to Set a date. Value assignment with an = is sufficient. Ampersands (e.g. &), not plus signs (e.g. +) are used for string concatenation.
The VBA Format function should be good at returning the correct string.
Dim dtInicio As String, dtFim As String
stDate = Format(MWS.Cells(1, "E").Value, "mm-dd-yyyy")
enDate = Format(MWS.Cells(2, "E").Value, "mm-dd-yyyy")
wstring = "exec sp_accessFile '" & stDate & "', '" & enDate & "'"
The wstring should look like,
exec sp_accessFile '07-19-2015', '08-25-2015'
The above assumes that E1:E2 are actual dates.

Related

Access List Form - filter by date - SQL database

SQL database with dates. I have a list form. At the top of the form, I am trying to filter the data by Date Received. When it brings it into Access, DateReceived shows in yyyy-MM-dd format so I have the SQL data invisible and I have a formatted text box (txtDateReceived) which takes the SQL date and formats it into mm/dd/yyyy which is visible. The user will enter the date in the mm/dd/yyyy format OR pick from the calendar. I tried to apply the filter to DateReceived and to txtDateReceived, and can't get either to work. Here is my code (includes everything I tried):
Private Sub TextDateR_AfterUpdate()
'declare variables
Dim sFilter As String
'in this case, the ID is text so the ID value
'needs to be wrapped in single quotes.
sFilter = "[DateReceived]= " & Format(Me.TextDateR, "yyyy-MM-dd")
'sFilter = "DateReceived = #" & Me.TextDateR & "#"
'sFilter = "txtDateReceived =" & Me.TextDateR
'assign the filter value,and turn filtering on
Me.Filter = sFilter
Me.FilterOn = True
Me.Recalc
Forms!frmSurplusList.Requery
Forms!frmSurplusList.Repaint
sFilter = ""
End Sub
Figured it out! sFilter = "[DateReceived]= '" & Format(Me.TextDateR, "yyyy-MM-dd") & "'"
You should create not a text date (that will be casted) but a date value expression using octothorpes:
sFilter = "[DateReceived] = #" & Format(Me.TextDateR, "yyyy-MM-dd") & "#"

Excel VBA SQL Query to Access to return records between dates

Morning all,
I am hoping this is a quick one. I have a query in Excel that requests records between two date points. It returns without errors but also returns unexpected values, seemingly because the date has been swapped around. I am working with the UK date format (dd/mm/yyy).
I have tried passing the date as a date type and a string type, as a datevalue and just the text. I have also tried passing it as yyyymmdd.
In the below example sFromDate is 06/04/2016 (6th April) and sToDate is 12/04/16 (12th April). Dates between June and December are being returned.
sSelectedArea = "ThisTMRacf"
sFromDate = DateValue(Sht_SpecificView.Range("D5"))
sToDate = DateValue(Sht_SpecificView.Range("F5"))
Set rex = db.OpenRecordset("SELECT eGain.RecordDate FROM [eGain], [Staff]
WHERE eGain.Racf = Staff.Racf AND (Staff.TeamManager = '" & sSelectedArea & "'
OR Staff.CSM = '" & sSelectedArea & "')
AND eGain.RecordDate BETWEEN #" & sFromDate & "# AND #" & sToDate & "# ;")
Is there another way to pass the date so that it doesn't convert?
Formatting the dates as yyyy/mm/dd worked. Example below.
sFromDate = Format(Sht_Dashboard.Range("D5"), "yyyy/mm/dd")
sToDate = Format(Sht_Dashboard.Range("F5"), "yyyy/mm/dd")
Might have to be ruthless with this and break the dates down and rebuild...
sFromDateY = Year(DateValue(Sht_SpecificView.Range("D5")))
sFromDateM = Month(DateValue(Sht_SpecificView.Range("D5")))
sFromDateD = Day(DateValue(Sht_SpecificView.Range("D5")))
Then use
AND eGain.RecordDate BETWEEN DateSerial(" & sFromDateY &", "& sFromDateM &", "&sFromDateD &") ...

VBA variable in SQL date query

I'm trying to query the SQL database for all lines where the date is after a date given through user input. I've run into a variety of errors from "incorrect syntax near" when I surround my date with "#" to "arithmetic overflow error converting expression to". My current code looks like this:
inputdate = InputBox("Please enter the starting date (mm/dd/yyyy)")
Debug.Print inputdate
querydate = "(DTG > " & Format(inputdate, "MMDDYYYY") & ")"
select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"
DTG is the column name for the date-time group in the SQL database. Any idea where I am going wrong? I've tried every solution I could find over the past few days without luck. Thank you in advance.
The primary issue is that dates must be enclosed in single-quotes. Here is a complete working example (minus a valid connection string) that should explain how to achieve your objective. Note that you will also want to switch to the ISO date format, in which the order is Year-Month-Day (YYYY-MM-DD).
Sub UpdateRecords()
Dim connection As New ADODB.connection
Dim recordset As New ADODB.recordset
Dim connectionString As String
Dim query As String
Dim inputdate As Date
Dim querydate As String
inputdate = InputBox("Please enter the starting date (mm/dd/yyyy)")
querydate = "(DTG > '" & Format(inputdate, "yyyy-mm-dd") & "')"
query = "select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"
connectionString = "..."
connection.Open connectionString
recordset.Open query, connection
ActiveSheet.Range("A1").CopyFromRecordset recordset
End Sub
Dates for SQL Server should be formatted as a date or date/time, qualified with single-quotes:
Date in ISO unseparated date format
querydate = "(DTG > '" & Format(inputdate, "yyyymmdd") & "')"
Date/Time in ISO 8601 format
querydate = "(DTG > '" & Format(inputdate, "yyyy-mm-ddThh:mm:ss.000") & "')"
Date format for MySQL is YYYY-MM-DD.
Also, as DTG is datetime, you'll need DATE(DTG) > DATE(NOW()) for example - DATE() is a MySQL function that checks only the date portion of a datetime stamp.
Your query should look like this:
querydate = "(DATE(DTG) > " & Format(userinput, "YYYY-MM-DD") & ")"
select StationID, DTG, CeilingFeet from SurfaceOb where " & querydate & " and StationID = 'NZWD'"

ASP Classic, update sql datetime

I'm trying to do this update to my SQL table but no luck so far.
The datetime in my SQL is: mm/dd/yyyy hh:mm:ss AM/PM.
When I use now(), the format is dd/mm/yyyy hh:mm:ss AM/PM.
So I change it to match:
new_now = month(now())&"/"&day(now())&"/"&year(now())&" "&time()
and here is my update SQL:
"UPDATE tblpo_po set purchase_status = '2', purchased_at = "&new_now&" where po_id = '"&request("po_number")&"' "
No luck so far.
error:
Incorrect_syntax_near_'10' (which is not relevant at all anyway)
Any idea?
found it, here is how: (http://p2p.wrox.com/classic-asp-basics/32475-asp-sql-server-datetime-field.html)
FUNCTION amDate(varDate)
IF isNull(varDate) OR Trim(varDate) = "" OR varDate = "Null" THEN
amDate = "Null"
ELSE
amDate = "'" & Month(DateValue(varDate)) & "/" & Day(DateValue(varDate)) & "/" & Year(DateValue(varDate)) & " " & TimeValue(varDate) & "'"
END IF
END FUNCTION
Try this (single quotes not neccessary, the function puts these in there, its a good practice to always use triling semi colons):
"Update LOGS SET LastActionBy = " & amDate(NOW()) & " WHERE ID = 5;"
then it works.
ref: http://p2p.wrox.com/classic-asp-basics/32475-asp-sql-server-datetime-field.html
Just need single quotes around your original date value. The "10" error was probably the hour of the time.
I would also suggest using parameters instead of sql string concatenation.

Convert date time (dd/MM/yyyy)

I have a column (datetime) in a Datagridview called DBO (date of birth) and it is converted with MM/dd/yyyy and I want to change it to this style (dd/MM/yyyy). I made this code and I get a error like " The conversion of the string " dd / mm / yyyy" for the ' Integer' type is not valid.
this is the sql string
myCommand = "UPDATE DoctorBasic SET " & _
"DOB = convert(datetime, '" & Convert_Null(DGV1(9, 0).Value.ToString("dd/MM/yyyy"), #1/1/1900#) & "', 103) " & _
"WHERE DoctorId = " & DGV1(0, 0).Value
DGV1 = datagriview
convert_null is a sub that I created to not get a error when the field is null and I want to made some changes and save it.
I don't understand what is wrong.. and what is that integer type in the error.