Convert date time (dd/MM/yyyy) - vb.net

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.

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") & "#"

date + time from vb6 to SQL

I have this code in VB6
Dim datahorS As String
datahorS = Text21.text & " " & Text22.text
Label2.Caption = datahorS
SQL = " insert into TabNFe_x ( data_hora_ent) values " _
& "(" & "'" & datahorS & "'" & ")"
datahorS = 20/10/2016 13:54
and the command
insert into TabNFe_x ( data_hora_ent) values ('20/10/2016 13:54')
erro from SQL
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
what's i m making wrong.
Convert your string into a datetime
Select convert(datetime, '20/10/2016 13:54', 103)
Returns
2016-10-20 13:54:00.000

Excel VBA Script Format Date MM-DD-YYYY

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.

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'"

Conversion from type 'DBNull' to type 'Date' is not valid

I am getting this exception from the following VB.NET code for only certain months:
System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
It happens on the line:
If CDate(dRow("CompleteDate")).ToString("d") = arrWeekYear(i, 1).ToString("d") Then
If I understand this correctly, then the problem is I am trying to compare some NULL values for CompleteDate to a non-NULL value.
How do I fix this? For some dates, this code above runs perfectly. However for some of the more recent records, it does not. But I am able to get output from the T-SQL query with same date range and there are no errors; it runs quickly also. And I examined this T-SQL query for both "Date_Completed" and "Review_Date" NULL values, but either way, "CompleteDate" was always = NON-Null value. So I do not understand how this is happening.
Here is my VB.NET query:
commandstring = "SELECT Batch_Records.Part_Number, Batch_Records.Lot_Number, Batch_Records.Date_Received, " & _
"IsNull([Date_Completed], [Review_Date]) AS [CompleteDate], Batch_Records.Error, " & _
"Batch_Records.[Group], Batch_Records.MFG, Batch_Records.MFG2, Batch_Records.QC, Batch_Records.QC2, " & _
"QC_CODES.CODE_DESC, DATEADD(DD, 1 - DATEPART(DW, Batch_Records.Date_Received), Batch_Records.Date_Received) AS SundayDate " & _
"FROM EXCEL.Batch_Records LEFT JOIN EXCEL.QC_CODES ON Batch_Records.Part_Number = QC_CODES.CODE_ID " & _
"WHERE (Batch_Records.[Group]" & TheGroup & " AND Batch_Records.Date_Received > '" & FirstWeek & "' AND Batch_Records.Date_Received < dateadd(Day, 1, '" & LastWeek & "'))" & _
"ORDER BY Batch_Records.Date_Received"
When I add this line above the error-causing line above, my report times out for these months. So what to do?
If Not dRow("CompleteDate") Is System.DBNull.Value Then
check for null with dRow.IsNull("CompleteDate") before you ask for the value.
Or if this is 'supposed to be' impossible, change your query to never return rows where it can be null.
I use a sub (different for each datatype) to get data from a Datareader, using the index instead of the name though:
#If Access Then
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Friend Function GetDbStringValue(ByVal Dr As OleDbDataReader, ByVal nr As Integer) As String
#Else
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Friend Function GetDbStringValue(ByVal Dr As MySqlDataReader, ByVal nr As Integer) As String
#End If
If IsDBNull(Dr.Item(nr)) Then
Return ""
Else
Return Dr.GetString(nr).TrimEnd
End If
End Function