[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

textbox date manipulation!!!!!!

Hi
i have a tab control with seven tabs one for each day so they are labelled monday,tuesday etc. On each tab I have a textbox which holds the date(i am currently using the NOW function) what i would like to do is after i have entered my data into mondays tab have the date stay on mondays date even when the actual day moves to Tuesday and again when it becomes wednesday i would like tuesdays date to remain in the textbox on tuesdays tab. Then at the end of the week i would like to reset the dates to start a new week.
0
samandrew
Asked:
samandrew
  • 5
  • 4
  • 3
  • +2
1 Solution
 
VBRocksCommented:
What you need to do is save your data.  You can create a DataTable in memory, and then bind each
column to one of your textboxes.  When your form closes, you can save your data; and then when
your form opens again, you can load the data from the file.

Here's an example of how to do that (data validation not included):
    'Set this path to whatever you want, but make sure the file is an "xml" file
    Private m_dbFilePath As String = "c:\temp\DaysOfWeek.xml"
    Private m_DT As DataTable = Nothing    'table to store the data

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load

        'Load the table when the form loads
        LoadTable()

    End Sub


    Private Sub LoadTable()

        m_DT = New DataTable("DaysOfWeek")
        If My.Computer.FileSystem.FileExists(m_dbFilePath) = False Then
            'If the file does not exist, then create it
            CreateXMLFile()

        End If

        'Load the table from the file
        m_DT.ReadXml(m_dbFilePath)

        'Bind your textboxes to the table
        Me.txtMonday.DataBindings.Add("Text", m_DT, "Monday")
        Me.txtTuesday.DataBindings.Add("Text", m_DT, "Tuesday")
        Me.txtWednesday.DataBindings.Add("Text", m_DT, "Wednesday")
        Me.txtThursday.DataBindings.Add("Text", m_DT, "Thursday")
        Me.txtFriday.DataBindings.Add("Text", m_DT, "Friday")

    End Sub


    'This code should only get executed once
    Private Sub CreateXMLFile()

        'Create table
        Dim dt As New DataTable("DaysOfWeek")
        dt.Columns.Add("Monday")
        dt.Columns.Add("Tuesday")
        dt.Columns.Add("Wednesday")
        dt.Columns.Add("Thursday")
        dt.Columns.Add("Friday")

        'Add 1 empty row to the table
        dt.Rows.Add(dt.NewRow())

        'Write it out to the file
        dt.WriteXml(m_dbFilePath, System.Data.XmlWriteMode.WriteSchema)

    End Sub


    Private Sub Form1_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        'When the form closes, save the data
        m_DT.WriteXml(m_dbFilePath, System.Data.XmlWriteMode.WriteSchema)

    End Sub

0
 
EDDYKTCommented:
use weekday function?

ie on first tab

Debug.Print Date - Weekday(Date, vbMonday) + 1

second tab and so on
Debug.Print Date - Weekday(Date, vbMonday) + 2
Debug.Print Date - Weekday(Date, vbMonday) + 3
Debug.Print Date - Weekday(Date, vbMonday) + 4
Debug.Print Date - Weekday(Date, vbMonday) + 5
Debug.Print Date - Weekday(Date, vbMonday) + 6
Debug.Print Date - Weekday(Date, vbMonday) + 7
0
 
samandrewAuthor Commented:
Hi
Thanks for the response and sorry to seem ungrateful but being a beginner can you please clarify.

The date will be saved in memory and then recalled when i open up the form.
you say use xml file, I am using vb6 and am not sure what this means
I will also be saving all my data on each tab including the date txtbox to a database will i still be able to do this.

How would i be able to clear all the dates at the end of each week.

Many Thanks

Andy
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
VBRocksCommented:
That's not a bad idea Eddy, but if you go that route, I would recommend something more like this:

        'Get today's date
        Dim dt As Date = Today.Date

        'Get monday's date
        Dim mon As Date = Today.Date.Subtract( _
            New TimeSpan(7 - Weekday(Today.Date, FirstDayOfWeek.Monday), 0, 0, 0))

        Dim tue As Date = mon.AddDays(1)
        Dim wed As Date = tue.AddDays(1)
        Dim thu As Date = wed.AddDays(1)
        Dim fri As Date = fri.AddDays(1)

0
 
VBRocksCommented:
Oops, just caught an error, change the following line:

     Dim fri As Date = thu.AddDays(1)
0
 
VBRocksCommented:
Or, if you prefer, you can calculate using Monday:

        'Get today's date
        Dim dt As Date = Today.Date

        'Get monday's date
        Dim mon As Date = Today.Date.Subtract( _
            New TimeSpan(7 - Weekday(Today.Date, FirstDayOfWeek.Monday), 0, 0, 0))

        Dim tue As Date = mon.AddDays(1)
        Dim wed As Date = mon.AddDays(2)
        Dim thu As Date = mon.AddDays(3)
        Dim fri As Date = mon.AddDays(4)

0
 
samandrewAuthor Commented:
Hi guys

All very interesting code some i understand some I dont!!!!!!!!!!!!!!!!!!!!  am I putting this code in a command button also the date will be in a textbox not on an actual tab and I will need to save it to a database each day.

Grateful but slighty confused

Thanks

Andy
0
 
danaseamanCommented:
You said you are using Vb6 in your comment so try this (no need to store anything):

Create an array of TextBoxes where TextBox1(0) is Monday and TextBox1(4) is Friday.
TextBox1(0) will be on your Monday Tab, TextBox1(4)  will be on your Friday Tab.

Then add this code to project:

Option Explicit

Private Sub Form_Load()
   ResetDates
End Sub

Private Sub ResetDates()
   Dim i As Long
   Dim datMonday As Date
   datMonday = DateAdd("d", vbMonday - Weekday(Date), Date)
   For i = 0 To 4
      Text1(i).Text = datMonday + i
   Next
End Sub
0
 
samandrewAuthor Commented:
Hi danaseman

This code works great thank you, is it also possible using this code to Format the date "dd, mmm, yyy"
and most importantly how can I reset the date at the end of the week or will this happen automatically when we reach next mondays date?

Many Thanks
Andy
0
 
peetmCommented:
  For i = 0 To 4
      Text(i).Text = Format$(datMonday + i, "dd, mmm, yy")
   Next

It'll update correctly the next week.
0
 
samandrewAuthor Commented:
Hi Peetm

That works great thank you One last question I have a further 2 textboxs called txtproductcode.Text which i need to offset from Text1.Text by 5 days and txtprecode.Text which i need to offset from Text1.Text by 6 days do i need to use another control array?

Many Thanks
Andy
0
 
peetmCommented:
>>do i need to use another control array?

Up to you - if you don't feel like doing it in the existing ResetDates, write a ResetDates1 or something for those two special cases.  Personally, I'd add those after the for loop.

txtproductcode.Text  = Format$(datMonday + 5, "dd, mmm, yy")
...
0
 
danaseamanCommented:
Just add 2 more TextBoxes to the existing array. txtproductcode will now be TextBox1(5) and txtprecode will be TextBox1(6).

Then change above code to:
Option Explicit

Private Sub Form_Load()
   ResetDates
End Sub

Private Sub ResetDates()
   Dim i As Long
   Dim datMonday As Date
   datMonday = DateAdd("d", vbMonday - Weekday(Date), Date)
   For i = 0 To Text1.UBound
      Text1(i).Text = Format$(datMonday + i, "dd, mmm, yyyy")
   Next
End Sub

0
 
samandrewAuthor Commented:
Hi peetm

being a beginner i am probably not explaining things so good (understatement of the year)

txtproductcode.Text needs to offset +5 days from each day i.e Monday+5, Tuesday +5 etc

Many Thanks

Andy
0
 
peetmCommented:
See danaseaman's reply
0
 
danaseamanCommented:
Not sure if I understand correctly. If each day Tab has a txtproductcode and txtprecode then create 2 more control arrays txtproductcode(0) through txtproductcode(4) and txtprecode(0) through txtprecode(4).

Private Sub ResetDates()
   Dim i As Long
   Dim datMonday As Date
   datMonday = DateAdd("d", vbMonday - Weekday(Date), Date)
   For i = 0 To Text1.UBound
      Text1(i).Text = Format$(datMonday + i, "dd, mmm, yyyy")
      txtproductcode(i).Text = Format$(5 + datMonday + i, "dd, mmm, yyyy")
      txtprecode(i).Text = Format$(6 + datMonday + i, "dd, mmm, yyyy")
   Next
End Sub
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now