Member 13694538 Ответов: 1

Я не могу решить проблему с формой даты


Private Sub set_CutoffDates()

        sqlstring = "SELECT " &
            "       * " &
            "   FROM " &
            "       tblBillingProcess "


        read_record(sqlstring, "tblBillingProcess")

        txtDate.Text = Date.Now.ToShortDateString


        ' check if first billing get the last month's dates
        If ds.Tables("tblBillingProcess").Rows.Count = 0 Then


            'Dim datenow As Date = Now.Date
            'Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
            'Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))
            'Dim firstdayprevmonth As String = Prev_Month & "/01/" & Prev_Year

            'Dim GetLastDayOfMonth As String = DateSerial(CInt(Prev_Year), CInt(Prev_Month) + 1, 0)

            'txtFrom.Text = firstdayprevmonth
            'txtTo.Text = GetLastDayOfMonth





            Dim datenow As Date = Now.Date

            Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
            Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))
            'Dim firstdayprevmonth As String = Prev_Month & "/01/" & Prev_Year

            Dim periodFrom As String = Prev_Month & "/21/" & Prev_Year


            Dim NOW_Month As String = Month(datenow)
            Dim NOW_Year As String = Year(datenow)





            Dim firstdayofmonth As String = NOW_Month & "/01/" & NOW_Year
            Dim GetLastDayOfMonth As String = NOW_Month & "/20/" & NOW_Year

            'Dim duedate As String = DateAdd("d", +10, datenow)

            Dim duedate As String = DateAdd("d", +10, GetLastDayOfMonth)


            txtFrom.Text = periodFrom
            txtTo.Text = GetLastDayOfMonth
            txtDueDate.Text = duedate



        Else
            Dim datenow As Date


            'sqlstring = "SELECT " & _
            '    "       MAX(Periodfrom) as Periodfrom " & _
            '    "   FROM " & _
            '    "       tblBillingProcess "

            sqlstring = "SELECT " &
           "       MAX(Periodto) as Periodfrom " &
           "   FROM " &
           "       tblBillingProcess "

            read_record(sqlstring, "tblBillingProcessdate")

            datenow = CDate(ds.Tables("tblBillingProcessdate").Rows(0).Item("Periodfrom").ToString)


            'Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
            'Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))


            Dim Prev_Month As String = Month(datenow)
            Dim Prev_Year As String = Year(datenow)

            Dim periodFrom As String = Prev_Month & "/21/" & Prev_Year




            Dim NOW_Month As String = Month(DateAdd("m", +1, datenow))
            Dim NOW_Year As String = Year(DateAdd("m", -1, datenow))

            Dim firstdayofmonth As String = NOW_Month & "/01/" & NOW_Year
            Dim GetLastDayOfMonth As String = NOW_Month & "/20/" & NOW_Year

            Dim duedate As String = DateAdd("d", +10, CDate(GetLastDayOfMonth))


            txtFrom.Text = periodFrom
            txtTo.Text = GetLastDayOfMonth
            txtDueDate.Text = duedate


        End If



    End Sub


Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
        Try

            If btnadd.Text = "&Add" Then

                If Get_PendingPresentReading() = True Then
                    MsgBox("You must complete all present reading before creating new billing process ",
                                    MsgBoxStyle.Exclamation, Text)
                    Exit Sub
                End If



                reset_controls()
                dgProcess.Rows.Clear()
                txtBillingNo.Text = String.Empty
                txtFrom.Text = String.Empty
                txtTo.Text = String.Empty
                txtDueDate.Text = String.Empty
                txtDate.Text = String.Empty



                generate_billingNO()
                load_data()
                set_CutoffDates()

                btnadd.Text = "&Save"

                btnedit.Enabled = False
                btncancel.Enabled = True
                btnMain.Enabled = False
                btnFind.Enabled = False
            Else

                If MsgBox("Are you sure to save this transaction ",
                          MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.No Then
                    Exit Sub
                End If


Что я уже пробовал:

проблема возникает, когда я пытаюсь добавить новый счет

В сообщении говорилось::

строка формы преобразования "4/20/2017" для ввода "дата" в поле недопустима. на МС ВБ. компилятор преобразования услуг . Современные(строковое значение)

проблема в строке 1054
Dim duedate As String = DateAdd("d", +10, CDate(GetLastDayOfMonth))


а в строке 1102
<pre>set_CutoffDates



фотография проблемы
[^]

1 Ответов

Рейтинг:
1

Maciej Los

Во-первых, во-вторых и в-третьих: дата-это дата, а не ее строковое представление!

Вывод: весь ваш код неверен!

Проверить это:

'needs to import Microsoft.VisualBasic
Dim DateNow As Date = Date.Now
Dim FirstDayOfPrevMonth As Date = DateAdd("M", -1, DateAdd("d", - Day(DateNow) +1, DateNow))
Console.WriteLine("{0}", FirstDayOfPrevMonth.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)) 'displays: 2018-02-01


Но(!), вы должны использовать Структура DateTime (Система)[^]. Видеть:
Dim DateNow As DateTime = DateTime.Now
Dim FirstDayOfPrevMonth As DateTime = DateNow.AddDays(- Day(DateNow) +1).AddMonths(-1)
Console.WriteLine("{0}", FirstDayOfPrevMonth.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))


Вы видите разницу?