locked
when date change counter variable is equal to 1 RRS feed

  • Question

  • hi

    when date change counter variable is equal to 1 like             

    Date                       Counter

    27/3/2020                  28

    28/3/2020                   1

    how to program for it


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Saturday, March 28, 2020 4:16 PM

Answers

  • Hi,

    Thank you for reply.
    But, I'm afraid, you need to give us more information.

    I guess you have a table like Excel sheet. When a patient comes to your counter, you will increase the sequential number for each patient.  And at midnight, the new date starts, the seq. number will be reset to 1.

    If this is correct, please provide your code or a table for patients you accept.

    Regards, 

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by muhammadanzar Sunday, March 29, 2020 7:01 PM
    Sunday, March 29, 2020 12:48 PM

All replies

  • Hi

    ' Form1 with DataGridView1
    ' and Buttom1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim StartDate As Date = Now.Date
      Dim counter As Integer = 28
      Dim r As New Random
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DataGridView1
          .Columns.Add("Date", "Date")
          .Columns.Add("Count", "Count")
        End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If r.Next(0, 20) > 18 Then
          counter = 1
          DataGridView1.Rows.Add(StartDate.AddDays(counter), counter)
        Else
          counter += 1
          DataGridView1.Rows.Add(StartDate, counter)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, March 28, 2020 5:33 PM
  • if PreviousDate.date <> now.date then counter = 1

    Probably does it not fix your problems, but your question is so summier that the answer can in fact as well be only summier. 



    Success
    Cor

    Saturday, March 28, 2020 6:10 PM
  • Not sure if this is what you are after,

    Dim d As Date
    
    For counter As Integer = 28 To 1 Step -1
        d = New Date(Now.Year, Now.Month, counter)
        Console.WriteLine($"{d:d} {counter,5}")
    Next


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, March 28, 2020 8:16 PM
  • Hi,

    As others say, I'm afraid, your description is ambiguous, or it allows several interpretations.
    If you cannot explain it with words, please provide more examples.
    i.e. what is the counter value in case of 29/02/2010, 01/03/2020, 02/03/2020...

    Regards, 

    Ashidacchi -- http://hokusosha.com

    Saturday, March 28, 2020 11:56 PM
  • hi,

    i have a language barrier,  i work in hospital reception,  counter means, number of patients.  at night when date change our requirement is counter again start from 1. 


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Sunday, March 29, 2020 12:08 PM
  • thanks for reply

    hi,

    i have a language barrier,  i work in hospital reception,  counter means, number of patients enter in the hospital.  at night when date change our requirement is counter again start from 1. 

    hope you understand


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Sunday, March 29, 2020 12:11 PM
  • Hi,

    Thank you for reply.
    But, I'm afraid, you need to give us more information.

    I guess you have a table like Excel sheet. When a patient comes to your counter, you will increase the sequential number for each patient.  And at midnight, the new date starts, the seq. number will be reset to 1.

    If this is correct, please provide your code or a table for patients you accept.

    Regards, 

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by muhammadanzar Sunday, March 29, 2020 7:01 PM
    Sunday, March 29, 2020 12:48 PM
  • Hello,

    To reset the Counter, in the following example the following form level variable is used

    Private Counter As Integer = 1

    Then add the following method to the form.

    Public Async Function ScheduleAction(action As Action, executionTime As DateTime) As Task
        Await Task.Delay(CInt(Math.Truncate(executionTime.Subtract(Now).TotalMilliseconds)))
        action()
    End Function

    Here I call the method above in a button click but could be wherever you want. 

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim executeDateTime = New DateTime(Now.Year, Now.Month, Now.Day + 1, 0, 0, 0, 0)
        Await ScheduleAction(Sub() Counter = 1, executeDateTime)
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Sunday, March 29, 2020 1:03 PM
  • Hi

    Version 2:

    Here is a stand alone test example.Needs a new Project with a BLANK DataGridView1, Button1 (COUNT + 1), Button2 (COUNT - 1) and Button3 (NEW DAY)

    Give it a try and see if anywhere close to what you want.

    ' Form1 with 
    ' BLANK DataGridView1
    ' Button1 (COUNT + 1)
    ' Button2 (COUNT - 1)
    ' Button3 (NEW DAY)
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("MUHAMMAD ANZAR")
      Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Counter.xml")
      Dim current As Integer = -1
      Dim NowDate As Date = Now.Date
      Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        dt.WriteXml(path)
      End Sub
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MinimumSize = New Size(400, 200)
        With dt
          .Columns.Add("Date", GetType(Date))
          .Columns.Add("Count", GetType(Integer))
          If IO.File.Exists(path) Then
            .ReadXml(path)
            NowDate = CDate(.Rows(.Rows.Count - 1).Item(0)).Date
          End If
        End With
        current = dt.Rows.Count - 1
    
        With DataGridView1
          .DataSource = dt
          .AllowUserToAddRows = False
          .RowHeadersVisible = False
          .Font = New Font("Calibre", 14)
          With .DefaultCellStyle
            .BackColor = Color.Khaki
            .ForeColor = Color.Black
          End With
          With .Columns("Date")
            .DefaultCellStyle.Format = "dddd, dd MMM yyyy"
            .DefaultCellStyle.Padding = New Padding(0, 0, 12, 0)
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
          End With
          With .Columns("Count")
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
          End With
        End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not dt.Rows.Count > 0 Then
          dt.Rows.Add(NowDate.Date, 0)
          current += 1
        End If
        dt.Rows(current).Item(1) = CInt(dt.Rows(current).Item(1)) + 1
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button3.Click
        NowDate = NowDate.AddDays(1)
        dt.Rows.Add(NowDate.Date, 0)
        current += 1
      End Sub
      Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim c As Integer = CInt(dt.Rows(current).Item("Count")) - 1
        If c < 0 Then c = 0
        dt.Rows(current).Item("Count") = c
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Sunday, March 29, 2020 2:39 PM
  • thanks for reply

    I guess you have a table like Excel sheet. When a patient comes to your counter, you will increase the sequential number for each patient.  And at midnight, the new date starts, the seq. number will be reset to 1.

    He best explain my problem. 


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Sunday, March 29, 2020 8:53 PM
  • Hi

    Did you try the code I posted - it does exactly that!

    AND WHY DOYOU MARK POSTS WHICH DO NOT CONTAIN AN ANSWER AS THE ANSWER?


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, March 29, 2020 9:04 PM
    Sunday, March 29, 2020 9:02 PM
  • thanks for reply

    hi,

    i have a language barrier,  i work in hospital reception,  counter means, number of patients enter in the hospital.  at night when date change our requirement is counter again start from 1. 

    hope you understand


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Yes I understand, my example is conceptual in that each iteration of the for statement simulates updating a counter in a variable or a row/column in a database table. So if you accumulated in the counter 50 today then at mid-night the code would reset the value to 1.

    I used a for-next and console write line so you could see the idea without messing with either a variable or database value. I would if doing this store the value in a database, not a variable but that would be your choice.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 29, 2020 9:58 PM