I am making a program in which there is a function that check the database for user that haven't been called for 2 weeks or more, and shows them in a ListView.
In this part I am checking how long ago they were called:
Dim r As Int32 = excelWS.UsedRange.Rows.Count
Dim bel As New ArrayList
For nm As Int32 = 1 To r Step 1
If Convert.ToInt32(DateDiff("ww", Date.ParseExact(excelWS.Cells(nm, 1).value(), "yyMMddhhmm", System.Globalization.DateTimeFormatInfo.InvariantInfo), Now, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)) >= My.Settings.Tijdverschil Then
bel.Add(nm)
End If
Next
I get the FormatException was unhandled at the if line.
In the error description it says (roughly translated to english):
The tokens aren't recognized at valid DateTime.
--edit--
If anyone thinks the format in excel is wrong, i copied one field over, they are all like this.
1408180736
Without additional information, I wonder if your date from Excel is coming in as an OLE Automation Date. Depending on how you read the data from Excel, it may come back in this format.
If it is, you need to parse it as a double and then as an OLEDate. Something like this:
Dim oleDate as Double
Dim result as DateTime
If Double.TryParse(excelWS.Cells(nm, 1).value(), oleDate) Then
' Handle valid OLE Automation dates...
result = DateTime.FromOADate(oleDate)
End If
The trick for this is include a datetime picker in your application set visibility to false.
Then set the string as value to the datetime picker and make use of the datetime picker to get the difference between dates.
Related
Today, I want to write day, month, and year to a datetimepicker in Visual Studio.
cmd.Parameters.AddWithValue("#geboortedag", dtp_geboortedatum.Value.Day)
cmd.Parameters.AddWithValue("#geboortemaand", dtp_geboortedatum.Value.Month)
cmd.Parameters.AddWithValue("#geboortejaar", dtp_geboortedatum.Value.Year)
These work. I save day, month and year separately to three rows in my database.
However, when I want to call these values, I can't even run the thing without getting the following:
BC30068 Visual Basic AND VB.NET Expression is a value and therefore cannot be the target of an assignment.
Here's what I tried.
dtp_geboortedatum.Value.Day = row("geboortedag").ToString
dtp_geboortedatum.Value.Month = row("geboortemaand").ToString
dtp_geboortedatum.Value.Year = row("geboortejaar").ToString
All I want is to put the day, month and year I have in separate cells into the date time picker when I open a record.
PS I also tried like the help page for the error says to write to a variable first but that does nothing to help. Perhaps I did it wrong but, I can't get it to work.
Also, I've been linked to this article but this does not fix the issue. I keep getting errors that integers are strings and cannot be converted to integers, but they're integers! They're integers when they start, they're integers when they're saved into a row for integers that saves integers, they're integers when they come out. Why aren't they integers in the end when nothing special happens to them but being inputted, saved, and called?
(Posted on behalf of the question author).
None of the material I provided in the question is relevant. Turns out I had to use dt.clear(). I'm going to be honest, I don't know how this is the thing that went wrong.
I fixed the rest with cdate() and Option Strict On.
its a very long time since touched VB.NET, but I think what you need to do is pass a DateTime type to the date picker to set its value as below;
dtp_geboortedatum.Value = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
So you will need to pass your year, month and day as integers
Just substitute your row integer values for the literals I used.
Private Sub SetDate()
Dim myDay As Integer = 27
Dim myMonth As Integer = 4
Dim myYear As Integer = 2018
DateTimePicker1.Value = New DateTime(myYear, myMonth, myDay)
End Sub
I am working on vb.net application
am getting date like this :
recevdate = rs("ITIReceiveddate")
my recevdate format is like this : 2/27/2016 month/date/year
i want to convert like this : date.month.year 27.2.2016
so i wrote code like this :
Dim dt as string = DateTime.ParseExact(recevdate, "dd.MM.yyyy", Nothing)
but its getting error ..
What is wrong with my code? how i can rectify this issue?
any help is very appreciable..Thanks
DateTime.ParseExact returns a DateTime, not a string. Your project is setup with the Option Strict set to Off and this enables this kind of automatic conversions. But it is, as usual, a trap waiting to kick on unsuspecting programmers.
To execute correctly you need
Dim recevdate = "2/27/2016"
Dim dt As DateTIme = DateTime.ParseExact(recevdate, "M/d/yyyy", Nothing)
Dim formattedString = dt.ToString("d.M.yyyy")
Console.WriteLine(formattedString)
Notice that you have an error also in your formatted mask for parsing the date. If your date has only one digit for months or one digit for days then you need just one M and one d both on the parsing and in the formatting back to string
data table contain column named as Fld_primary. this column contain value like 0.00 it is double datatype in mysql table.i want store that datatable value in double variable. am always getting error when i convert to double datatype.
my code
-------
Dim ds5 As dataset1.DTSUMDataTable = TA5.GetData(users)
Dim dttot As Double
dttot = CType(ds5("fld_primary").ToString, Double)
Error
Conversion from string "fld_primary" to type 'Integer' is not valid.
Edited # 3:01 AM with most recent screen caps.
Sometimes I find myself second guessing certain code based on people's answers, but I went ahead and took the time to check if the code that they are all using is even valid:
As you can see that code is a no go, so I used the correct code you see at the bottom there to reference a column.
However, if you wish to get a single cell use the chunk of code below that uses the foreach loop (the rest is my basic setup to show you how it works):
"Y" will equal the value of the datatable cell and you may convert it using the Double.Parse() method:
Dim y = Double.Parse(zDataRow("cat").ToString())
Be careful, if you have multiple rows you will notice that the value of y will change as it makes its way through all the rows.
you can convert it using the Convert class.
Dim dttot As Double = Convert.ToDouble(ds5("fld_primary"))
Your error is actually: ds5 expects an integer as a parameter, so using ds5("fld_primary") is not valid in your code. Perhaps you can try ds5(0)("fld_primary").
After you fixed it, use
dttot = Double.Parse(whatever_string_you_should_put_here)
If you cannot ensure your string must be a valid double, then use Double.TryParse.
You are better off using the 'Double.TryParse' way of converting as this handles any errors better and simply returns a boolean if succesful or not, using 'parse' will throw an exception which isnt anywhere near as elegant.
Dim dub as Double = 0
Double.TryParse("Your String Here", dub)
Try using Double.TryParse(text,value)
Try using this:
For a=0 to yourgridview.rows.count-1
Yourgridview.rows(a).cells(targetcolumnnumber).value=cdbl(Yourgridview.rows(a).cells(targetcolumnnumber).value)
Next
Okay so i have a macro that goes in and checks the table to determine the format of a application that is set by the user. If this format is set to be a European format then it runs a function that will go through a table and swap the date formatting from MM/dd/yyyy to dd/MM/yyyy. everything seems to be set up correctly and when it's in US formatting the macro runs correctly(which it should considering it skips over this line if the formatting is not set to European.) however, whenever the formatting is set to European, i get this run-time error that pops up:
Run-Time error '3052':
File sharing lock count exceeded. Increase MaxLocksPerFile registry entry.
Now i'm always wary whenever i get an error that states the fix is to modify something in the registy. Not to mention this is not something i would want a client to be doing just to get the application to work. However i gave it a shot just to see if that would indeed fix the problem by following these steps. Although when i did modify the default MaxLocksPerFile from it's default(9500) to (30,000) and ran the macro again i still got the same error at a slightly larger count.
Before i would get the error after running through about 12,000 rows, after the change it would come up around 15,0000 rows.
This leads me to believe that i am incorrectly closing out my updates after each row is modified.
This is my code below:
Public Function UKDateFormat() As Variant
Dim varPieces As Variant
Dim strNew As String
Dim varReturn As Variant
Dim Strsql As String
Dim db As dao.Database
Dim rstAlarmdetDateMod As dao.Recordset
Dim i As Long
Set db = CurrentDb()
Strsql = "select AlarmDate From AlarmdetDateMods;"
Set rstAlarmdetDateMod = db.OpenRecordset(Strsql, dbOpenDynaset)
If (rstAlarmdetDateMod.RecordCount > 0) Then
rstAlarmdetDateMod.MoveFirst
i = 0
While (rstAlarmdetDateMod.EOF) = False
i = i + 1
rstAlarmdetDateMod.Edit
rstAlarmdetDateMod![alarmdate] = CDate(Format(rstAlarmdetDateMod![alarmdate], "dd/MM/yyyy"))
rstAlarmdetDateMod.Update
rstAlarmdetDateMod.MoveNext
Wend
End If
rstAlarmdetDateMod.Close
db.Close
End Function
This is my Update query:
UPDATE DISTINCTROW AlarmdetDateMods SET AlarmdetDateMods.AlarmDate = CDate(Format([AlarmDate],"dd/mm/yyyy"));
What i would like to know is: What exactly i am doing wrong with my function that it causes this error to come up and how can i correct it in a way so that i don't have to go into and modify the registry to get this function to work?
Any help or suggestions are greatly appreciated.
Thanks.
In addition to your UKDateFormat function, you showed us this UPDATE statement.
UPDATE DISTINCTROW AlarmdetDateMods
SET AlarmdetDateMods.AlarmDate = CDate(Format([AlarmDate],"dd/mm/yyyy"));
However, it's not clear what's going on with that. Does it work? Does it fail with the same error as the UKDateFormat function? A different error?
Try a revised UPDATE statement. I think DISTINCTROW is not useful in this case; suggest you discard it.
Also your UPDATE seems to rely on implicit data type conversions. AlarmDate is a text value. You pass that text to the Format() function, to treat it as a Date/Time value and transform it to a different formatted string. Then you ask CDate() convert that string value to a Date/Time value. Finally the Date/Time value is stored back to the AlarmDate text field.
At a minimum I would avoid casting the formatted string back to a Date/Time value before storing it in the text field. However, I would also make the data type conversions explicit rather than implicit. And limit the UPDATE attempt to only those rows where AlarmDate holds the text representation of a valid Date/Time value.
UPDATE AlarmdetDateMods
SET AlarmDate = Format(CDate(AlarmDate),"dd/mm/yyyy")
WHERE IsDate(AlarmDate) = True;
Note this suggestion assumes changing all your stored AlarmDate text values is reasonable. I have doubts about that. Seems like maybe AlarmDate should be Date/Time rather than text. And if you need to change the format when displaying those Date/Time values, do it with the format property of a bound control on a form or with the Format() function in a query.
I seem to have a date formatting problem every day!
I am querying a table and am getting a date back in the format dd/mm/yyyy (as a string btw). Brilliant! thats what I want. But, now I want to convert that string to a date so i can do
dim dayNumber as integer = day.DayOfWeek
But when I convert it to a date it changes it to #m/dd/yyyy#. AHHHH! how can I change this?
here is my code i've tried
Dim ActivityDate As String
If dt.Rows(i)("Date") Is DBNull.Value Then
ActivityDate = ""
Else
ActivityDate = dt.Rows(i)("Date")
End If
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse(ActivityDate, ci)
Dim day As Integer = theDate.DayOfWeek
Cheers
Brilliant! thats what I want
That's not what you want. It is the worst possible format for a date because it is so horribly ambiguous. Date string formats depend on the current culture. "4/1/2010" is Unicorn day at SO, it is day in January in Europe. "#4/1/2010#" is a legacy VB6 format.
Always store dates in a DateTime in your code. Always store dates in a database column type of datetime in your dbase. There is never any ambiguity and you'll have an easy time with the DateTime members to manipulate dates.
If you convert the string to a date, you can always output it back to the original format using a custom format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The correct solution here (at least until you tell us why this isn't possible) is to update your database to use a datetime column type rather than a varchar. Now we also know that this column has no NULL values, because otherwise you'd be complaining about exceptions on your Date.Parse() call. After applying both those sentences, you can trim all that code down to a simple one-liner:
Dim day As Integer = DirectCast(dt.Rows(i)("Date"), DateTime).DayOfWeek
May I also ask why you're looping through the table row by row? I've worked in a shop where that was the norm, but since I've left there I've run in to alternatives and more and more I'm coming to find looping through a datatable as just wrong. It's an older imperative coding style, and generally you want to go for a declarative coding style.
Are you parsing it like this:
Dim newDate as DateTime = DateTime.Parse(myDate)
If the culture of your system does not use that date format, then you should get that date string as an actual date:
' canadian date format is dd/mm/yyyy
Dim ci As New System.Globalization.CultureInfo("en-CA")
Dim theDate As Date = Date.Parse("13/04/2010", ci)
Make sure you specify an exact parse format like so:
Console.WriteLine(DateTime.ParseExact("17/12/2010", "dd/mm/yyyy", null));
I am not sure what the last parameter is but it is safe to ignore it.
I'm guessing that you are seeing the #m/dd/yyyy# in the debugger, like this screenshot below. Don't worry!
A Date variable isn't stored as a string. The debugger has to convert your Date into a string to display it, and it insists on showing dates in #m/dd/yyyy# format. But that doesn't have any effect on the runtime behaviour of your program.
Screenshot of Visual Studio Debugger http://img707.imageshack.us/img707/6205/debugger.gif