VB.NET Do Until Loop to get the Month Name - mysql

I want to get the month name from a Do Until......Loop and display in a CheckedListBox.
I have two tables.
1. Month_Count
2. Fees_Ledger
My month name function...
Function mnthName(ByVal mnth As Integer)
Dim name As String = String.Empty
name = MonthName(mnth, False)
Return name
End Function
To get the Month Details
"SELECT FromMonth,ToMonth,MonthCount FROM Month_Count WHERE SemesterNumber='1'"
And to get the Paid Count
"SELECT COUNT(*) AS TotMonthPaidCount FROM Fees_Ledger WHERE SemYear='1' AND FeeId='1'"
So...
' Got the values from queries
FromMonth = 7 '(July)
ToMonth = 6 '(June)
MonthCount = 12 '(Loop will rotate 12 times)
TotMonthPaidCount = 1 '(FeeId 1 paid one time)
'Declaring an integer variable
Dim StartM As Integer = 1
FromMonth = FromMonth + TotMonthPaidCount
' For the first month
CheckedListBoxMonth.Items.Clear()
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
' Now the loop to achieve the goal
Do Until StartM = (MonthCount - TotMonthPaidCount)
If FromMonth >= 12 Then
FromMonth = 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
Else
FromMonth += 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
End If
StartM += 1
Loop
This Subroutine results exactly what I want.
But the problem occurs when the StartMonth = FromMonth (6) + TotMonthPaidCount (7) value >12. As 13 or 14 or 15 has no Month Name, it is showing error.
Argument 'Month' is not a valid value.
I want it like below.
What should I do ?

Related

How to add new Access record by checkbox and number of different date?

I have query 1 or table 1 have the below data
Number of employee/start date/end date/code/different date by Days/checkbox
Example
200/01-01-2021/15-01-2021/E/14/Yes
I need when checkbox=Yes to open 14 record automatically in new table 2 with code like below:
employee/date/code
200/01-01-2021/E
200/02-01-2021/E
200/03-01-2021/E
200/04-01-2021/E
200/05-01-2021/E
200/06-01-2021/E
200/07-01-2021/E
200/08-01-2021/E
200/09-01-2021/E
200/10-01-2021/E
200/11-01-2021/E
200/12-01-2021/E
200/13-01-2021/E
200/14-01-2021/E
Extract your parameters:
Data = "200/01-01-2021/15-01-2021/E/14/Yes"
ENo = Split(Data, "/")(0)
FirstDate = DateValue(Split(Data, "/")(1))
ECode = Split(Data, "/")(3)
Periods = Split(Data, "/")(4)
Then run this query passing it the parameters:
PARAMETERS
Periods Short,
FirstDate DateTime;
SELECT DISTINCT
10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10) + 1 AS Sequence,
ENo As Employee,
DateAdd("d", [Sequence] - 1, [FirstDate]) AS DateStart,
ECode As Code
FROM
MSysObjects AS Uno,
MSysObjects AS Deca
WHERE
(10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10)) < [Periods];
Output:
You can the use this query as source in an append query.
Or, alternatively, use DAO to directly append the records:
Dim Records As DAO.Recordset
Dim Index As Integer
Set Records = CurrentDb.OpenRecordset("Select * From Table2")
For Index = 0 To Periods - 1
Records.AddNew
Records!Employee.Value = Eno
Records!Date.Value = DateAdd("d", Index, FirstDate)
Records!Code.Value = ECode
Records.Update
Next
Records.Close

Average depending of parameter given

I have a month parameter called DimTiempoMes So I when I run preview of report I select that month in combo box it can be from 1 to 12.
If I select 1 it return value of Fields!InventarioDirecto_Monto.Value of month 1
If I select 3 it return value of Fields!InventarioDirecto_Monto.Value of month 1,2 and 3 etc...
Now I want to get Average depending of selected Fields!InventarioDirecto_Monto.Value
For Example: If I selectDimTiempoMes with value10, formula will be for example:
InventarioDirecto_Monto Value / DimTiempoMes.Value
Execution: Sum(1,2,3,4,5,6,7,8,9,10) / 10
So I try next expression:
=SUM(Fields!InventarioDirecto_Monto.Value) / CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]",""))
Problem is SUM(Fields!InventarioDirecto_Monto.Value)always come with sum of 12 months values, so it always sum values of 12 months
Question is, How can I get only values that Fields!InventarioDirecto_Monto.Value return depending of selected parameter DimTiempoMes?
Some Data:
In this image I have 3,719 in first column because I use DimTiempoMes as 1: so my colum Acum who have expression should be 3,719 too because SUM(Fields!InventarioDirecto_Monto.Value) equals to 3719 and divisor equals to 1 because I search 1. So 3,719/1 = 3791 and I getting 40,593
Other Example: if I use DimTiempoMes as 4
There I should get 3719 + 3639 + 3378 + 2999 / 4 = 3433.73 and I receive 10148
Update:
I also try adding custom code like:
Function Promedio(ByVal items As Object(),ByVal meses As Object(), ByVal Valor1 As integer) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
For Each mes As Object In meses
IF CINT(mes) <= Valor1
Then ct = 1
For Each item As Object In items
If (ct = CINT(mes))
Then suma += Convert.ToDecimal(item)
End If
ct += 1
Next
End If
Next
suma = suma/valor1
If (Valor1 = 0) Then return 0 else return suma
End Function
And Expression something like:
=Code.Promedio(LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!InventarioDirecto_Monto.Value, "DS_SubInventarioDirecto"),LookupSet(Fields!Dim_ConceptosInventario_.Value, Fields!Dim_ConceptosInventario_.Value, Fields!nPeriodo.Value, "DS_SubInventarioDirecto"),CINT(replace(left(Split(Parameters!DimTiempoMes.Value,"[").GetValue(3),2),"]","")) )
But I donĀ“t get desire results

VBA Macro to subtract dates by duplicates ID

I would like to create a Macro in VBA to identify the first date and the last date by ID and then get the subtract result between them formatted in hours.
This is an example of my Table:
ID DATE
001 11/11/2013
001 11/21/2013
001 11/25/2013
002 12/04/2013
002 12/05/2013
003 12/05/2013
001 11/23/2013
The desired result:
ID DATE RESULT IN HOURS (Last Date - First Date)
001 11/11/2013 =(11/25/2013)-(11/11/2013)
001 11/21/2013
001 11/25/2013
002 12/04/2013 =(12/05/2013)-(12/04/2013)
002 12/05/2013
003 12/05/2013
001 11/23/2005
In the current table, there are 2 or more duplicates IDs with different Dates as you can see in the 001 ID Sample.
My first solution was sorting the table by ID and Date and then apply a Countif formula to get the duplicates IDs but I was only able to identify the First Date by ID and missing the Last Date.
I would very much appreciate your help. Thanks!
Nice problem. I like the use of collections, because they are quite fast. There must be numerous ways solve a problem like this, the following is one example:
Sub jzz()
Dim cArray() As Variant
Dim lastRow As Long
Dim rw As Long
Dim i As Long
Dim firstDate As Date
Dim lastDate As Date
Dim id As Long
Dim idColl As Collection
Set idColl = New Collection
'determine last row:
lastRow = Cells.SpecialCells(xlLastCell).Row
'if you plan to loop the cells repeatedly,
'the use of an array to hold cell values pays off, performancewise
cArray = Range(Cells(1, 1), Cells(lastRow, 2)).Value
For rw = 1 To lastRow
id = Cells(rw, 1)
'determine if id is used or not:
For i = 1 To idColl.Count
If idColl(i) = id Then GoTo nextRow
Next i
'id is not in collection, add it:
idColl.Add id
firstDate = cArray(rw, 2)
lastDate = 0
'loop array to find first and last dates:
For i = LBound(cArray) To UBound(cArray)
If cArray(i, 1) = id Then
If cArray(i, 2) < firstDate Then firstDate = cArray(i, 2)
If cArray(i, 2) > lastDate Then lastDate = cArray(i, 2)
End If
Next i
Debug.Print id, firstDate, lastDate
nextRow:
Next rw
End Sub
Private Function TimeDiffHours(T0 As Date, T1 As Date) As String
'function that returns the difference between T0 and T1 in hours:mins
TimeDiffHours = Int((T1 - T0) * 24) & ":" _
& Format(Round(((T1 - T0) * 24 - Int((T1 - T0) * 24)) * 60, 0), "00")
End Function
Note that this only finds the first and the last dates for each id and prints it out, along with the time difference in Hours:mins. It does nothing with it, not even save results. That is up to you now.

Insert a specific date in Mysql using vb.net

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)

Find number of weekdays/weekends in a given date range

I'm trying to find some VBA code to determine the number of week days and weekend days in a given date range using Access VBA.
For example:
Begin Date - 1/1/2012
End Date - 1/31/2012
Result should be:
Week days - 22
Weekend days - 9
Can anyone help out with this?
These two functions will calculate the number of weekdays and weekend days:
Function NumWeekendDays(dBegin As Date, dEnd As Date) As Long
Dim iPartial As Integer
Dim lBeginDay As Long
Dim lNumWeekendDays As Long
iPartial = DateDiff("d", dBegin, dEnd + 1) Mod 7
lBeginDay = 6 - DatePart("w", dBegin, vbMonday)
lNumWeekendDays = (DateDiff("d", dBegin, dEnd + 1) \ 7) * 2
If iPartial > 0 And lBeginDay - iPartial < 0 Then
If lBeginDay = -1 Then
lNumWeekendDays = lNumWeekendDays + 1
ElseIf iPartial - lBeginDay = 1 Then
lNumWeekendDays = lNumWeekendDays + 1
Else
lNumWeekendDays = lNumWeekendDays + 2
End If
End If
NumWeekendDays = lNumWeekendDays
End Function
Function NumWeekDays(dBegin As Date, dEnd As Date) As Long
NumWeekDays = DateDiff("d", dBegin, dEnd + 1) - NumWeekendDays(dBegin, dEnd)
End Function
Note: I found it simplest to calculate the partial-week weekend days by calculating the lBeginDay variable so that if the start date was Monday, lBeginDay == 5... if the start date was Friday, lBeginDay == 1, etc. Other variations should also work.