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'"
Related
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") & "#"
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 &") ...
I need to get the records that will be ended after two days
but always I got an empty datagridview.
I have tried this code:
Dim after2days As Date = Today.Date.AddDays(3)
Dim sqlstr As String = "SELECT * FROM tblvac where vend between " & Today.Date & " and " & after2days & " "
Dim da As New OleDbDataAdapter(sqlstr, Conn)
ds.Reset()
da = New OleDbDataAdapter(sqlstr, Conn)
da.Fill(ds)
dgv.DataSource = ds
Conn.Close()
Why is this happening and how can I fix it?
Access SQL includes functions, Date() and DateAdd(), which can give you the date range you want for your query.
Dim sqlstr As String = "SELECT * FROM tblvac where vend between Date() and DateAdd('d', 3, Date());"
If you prefer to pass date values from your VB.Net code to the db engine, use a parameter query so that you needn't bother about date format and delimiters. Just supply valid Date/Time values for the parameters.
You are building your query as a string and thus need to use the convert to date function... for MS Access its DateValue see http://www.techonthenet.com/access/functions/date/datevalue.php
Try
Dim sqlstr As String = "SELECT * FROM tblvac where vend between
DateValue('" & Today.Date & "') and DateValue('" & after2days & "') "
As commented by HansUp... this solution needs to have the date format as mm/dd/yyyy or yyyy-mm-dd
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.
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.