Insert a specific date in Mysql using vb.net - mysql

Okay here is the scenario, I need to insert a specific date in mysql. Everytime I insert this date I get 0000-00-00.
Everytime a user pays between the 1st and the 20th of the month then the wb_due-date column would increment the month by 1
Ex.
I have a default value of wb_paid-date = 2013-10-15 and wb_due-date = 2013-10-20.
Now User1 Paid on 2013-10-15 and after I clicked button, the date saved on wb_due-date was 0000-00-00 instead of 2013-11-20
Take a look at my code
Function iterate(ByVal d As Date) As String
Dim m As Integer = d.Month
If d.Month >= 1 And d.Month <= 11 Then
m += 1
ElseIf d.Month = 12 Then
m = 1
End If
Return m
End Function
cmd = New MySqlCommand("INSERT INTO tbl_billing(wb_paid-date, wb_due-date)
VALUES(CURDATE(), iterate(Now.Date) , con)

First, let's fix your function:
Function iterate(ByVal d As DateTime) As String
Return d.AddMonths(1).ToString("yyyy-MM-dd")
End Function
Also, if you're putting a string date into command, you're almost certainly doing something wrong. Let's just do this:
Function iterate(ByVal d As DateTime) As DateTime
Return d.AddMonths(1)
End Function
Then we'll fix your Sql Command:
cmd = New MySqlCommand("INSERT INTO tbl_billing(wb_paid-date, wb_due-date) VALUES(CURDATE(), ? " , con)
cmd.Parameters.Add("?", SqlDbType.DateTime).Value = iterate(Today)

Related

VB.NET How to create a reminder by checking database saved dates

I have this few line of codes where I save the date from datetimepicker to my database column REMINDER.
myCommand.CommandText = "Update kup_table SET REMINDER = #reminder Where ID = #theIDD"
myCommand.Parameters.AddWithValue("#reminder", DateTimePicker1.Value.Date)
myCommand.Parameters.AddWithValue("#theIDD", theID)
myCommand.ExecuteNonQuery()
In my database, the date is saved in this format 2015-12-14 00:00:00 since the datatype is DATETIME.
How do I compare it with the date now? If the saved date and today date are a match, then a reminder will go off.
I have tried using this sql command but having still having trouble where the reminder is always zero. Thanks in advance.
myCommand.CommandText = "Select COUNT(*) from kup_table Where REMINDER BETWEEN DATE() AND DATEADD('d', 1, DATE())"
mySqlConn.Open()
Console.WriteLine("Connected.")
count = myCommand.ExecuteScalar()
MsgBox("You have " + count.ToString + " reminder(s).")
Select COUNT(*) from kup_table Where DATE(REMINDER) = DATE(NOW())
The DATE() function returns the date part of the datetime only. (e.g. 2015-12-14)
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date

Access Query. If records Due Date = Last Week Insert Yes in Cell

Good morning All,
I am working on the following however, I can't figure it out for the life of me. I have table that contains a bunch of due dates. What I am trying to do is add a query that adds another field and inserts a yes if the date equals a day in last week. Honestly I need it to insert yes as long as it does not = this week.
I've tried using:
Urgent: IIf([Due Date]=[Due Date]
Between
DateAdd("d",1-Weekday(Date()) 7, Date()) And
DateAdd("d",1-Weekday(Date())-1, Date()),"Yes","")
with no luck. What am I missing here?!
Thanks all. Cheers.
Here's how to return Urgent if 'not this week' means previous Saturday and before. Not sure how you would do it without code.
Helper Function to get the first day of this week based on current date. Paste it into a new or existing global module.
Function FirstDateOfTheWeek() As Date
Dim dt As Date
If Weekday(Date) = vbSaturday Then
FirstDateOfTheWeek = DateAdd("y", -6, Date) 'today is Saturday? then return previous Sunday
ElseIf Weekday(Date) = vbSunday Then
FirstDateOfTheWeek = Date 'today is Sunday? then return Sunday because its the first day of the week
Else 'weekday, so just go backwards until we hit previous sunday's date
dt = Date
While Weekday(dt) <> vbSunday
dt = DateAdd("y", -1, dt)
Wend
FirstDateOfTheWeek = dt
End If
End Function
An IsUrgent function to be called from query. Paste it into a new or existing global module.
Function IsUrgent(dt As Variant) As String
If IsNull(dt) Then 'if null date is passed then return blank string; Variant chosen instead of date for this case; change it to N/A if you want?
IsUrgent = ""
Exit Function
End If
If dt < FirstDateOfTheWeek Then
IsUrgent = "Yes"
Else
IsUrgent = ""
End If
End Function
The Query Column calling IsUrgent() function:
IsUrgent: IsUrgent([Due Date])

capture dates by quarters in access

To make my life much easier working with my data I set 3 columns of same date content. The first displays in the format mm/dd/yyyy, second in the format of yyyy-mm and the third in the format yyyy-q.
I did it purposely due to my reports. Sometimes I need to create monthly, quarterly, yearly etc. Usually I work with a form where I invite the user select start and end date and by a click of a button run a report. This report extracts a query where I specify on the date section to pull all information between start and end date. This time I want to do the same procedure but instead of start and end date - I want the user to select which quarter he wants so that the query will pull all information regarding this quarter. What do I specify in the criteria to archive this?
Filter on
DatePart("q", [YourDateField])
or
Format([YourDateField], "yyyyq")
To obtain the first and last date of a quarter, given the year and the quarter, you can use these expressions:
DateQuarterFirst = DateSerial(Y, 1 + 3 * (Q - 1), 1)
DateQuarterLast = DateSerial(Y, 1 + 3 * Q, 0)
If you have a date of the quarter, you can these functions to obtain the first and last date of the quarter of that date:
Public Function DateThisQuarterFirst( _
Optional ByVal datDateThisQuarter As Date) As Date
Const cintQuarterMonthCount As Integer = 3
Dim intThisMonth As Integer
If datDateThisQuarter = 0 Then
datDateThisQuarter = Date
End If
intThisMonth = (DatePart("q", datDateThisQuarter) - 1) * cintQuarterMonthCount
DateThisQuarterFirst = DateSerial(Year(datDateThisQuarter), intThisMonth + 1, 1)
End Function
Public Function DateThisQuarterLast( _
Optional ByVal datDateThisQuarter As Date) As Date
Const cintQuarterMonthCount As Integer = 3
Dim intThisMonth As Integer
If datDateThisQuarter = 0 Then
datDateThisQuarter = Date
End If
intThisMonth = DatePart("q", datDateThisQuarter) * cintQuarterMonthCount
DateThisQuarterLast = DateSerial(Year(datDateThisQuarter), intThisMonth + 1, 0)
End Function

VB.NET - mysql check if timestart and timeend entered from vb.net conflicts with the time_start and time_end from mysql

I have two datetimepicker that is formatted to time only (e.g. 07:30 AM).
When I enter a time in this two datetimepicker it should prompt if the time conflicts from the mysql time_start and time_end columns (even if it is a 1 min. time conflict).
Example:
Winform, I enter
timestart: 06:00 AM
timeend: 08:00 AM
In mysql I have:
| time_start | time_end |
| 06:00 AM | 07:00 AM |
When I enter button, It should prompt me that the time I entered is conflicting with the time from mysql (some instances that it will prompt me with that time in mysql is (06:01 AM - 06:59 AM) OR (06:59 AM - 07:01 AM) OR (05:00 AM - 09:00 AM), etc.
What I have tried is this
'reader.GetDateTime(2) is time_start
'reader.GetDateTime(3) is time_end
Public Function checkConflict() As Boolean
Dim conflict As Boolean = False
sqlconn.Open()
Dim query As String
query = "SELECT * FROM tbl_schedule ORDER BY section_id, day, time_start"
sqlcommand = New MySqlCommand(query, sqlconn)
reader = sqlcommand.ExecuteReader
While reader.Read
For x As Integer = 1 To daycount
If dt_Start.Value >= reader.GetDateTime(2) And dt_End.Value <= reader.GetDateTime(3) Then
conflict = True
End If
Next
End While
sqlconn.Close()
Return conflict
End Function
How I use the function
If checkConflict() = False Then
MsgBox("No conflict")
Else
MsgBox("There is conflict")
End If
For x As Integer = 1 To daycount
If dt_Start.Value >= reader.GetDateTime(2) And dt_End.Value <= reader.GetDateTime(3) Then
conflict = True
End If
what is that daycount? did you make this work?

QRY returning error if value is blank?

I am running the below expression to compare the diffence of 2 dates. If both dates are in the cell I would like it to return a 0 but if Date2 is blank I would like the difference to show. Right now I just get #ERROR if there is no date in date2. Any ideas would be greatly appreciated.
expr2: NetWorkDays([Date1],[Date2])
Option Compare Database
Public Function NetWorkdays(dteStart As Date, dteEnd As Date) As Integer
Dim intGrossDays As Integer
Dim dteCurrDate As Date
Dim i As Integer
intGrossDays = DateDiff("d", dteStart, dteEnd)
NetWorkdays = 0
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbMonday) < 6 Then
End If
Next i
End Function
The function will always return 0 if you don't remove the NetWorkdays = 0 from the function, and you will continue you to get an error if you don't wrap where you call the function in an if statement like this:
If Not IsNull(Date2) Then
txtResult = NetWorkdays(Date1, Date2)
Else
txtResult = 0
End If
txtResult being the textbox you want to display your results in.
You may want to look at this link http://msdn.microsoft.com/en-us/library/bb258196(v=office.12).aspx. It is a function designed for access to calculate the number of workdays between two dates.
Using this function you should get the results you want doing something like this:
If Not IsNull(Date2) Then
txtResult = 0
Else
txtResult = Work_Days(Date1, Now())
End If