0

I need some help. Is there is any ways to display data from Access to form using VB. Especially from Date/Time(Access) to DateTimePicker(Windows Form)

My Data Type and format of date in Access is:

Data Type = Date/Time

Format = Short Date

My code:proddate_tb.Text = ds.Tables("MasterMB4").Rows(i).Item("ProdDate").ToString()

But it prompted below error.enter image description here

Community
  • 1
  • 1
stillLearning
  • 84
  • 2
  • 11

2 Answers2

1

Firstly, I would recommend adding a few breakpoints and checking some values as the code runs and see if you can find where things go wrong. It might even be that the value in the database is NULL.

Sooner or later we all have to learn to debug, might as well start early.

Secondly, Make your that the number of rows returned in your database query aren't less than the number of times your loop runs. I'm guessing you are running a loop based on your code:

ds.Tables("MasterMB4").Rows(i).Item("ProdDate")

I always do this:

 Dim row_count As Integer = ds.Tables("Table_Name").Rows.Count - 1

    If Not row_count < 0 Then

       For i = 0 To row_count

            If IsDBNull(ds.Tables("MasterMB4").Rows(i).Item("ProdDate"))Then
                  proddate_tb.Value = Date.Now

            Else
                  proddate_tb.Value = Cdate(ds.Tables("MasterMB4").Rows(i).Item("ProdDate"))

            End If
       Next

    End If

As for setting the datepicker. Try :

DatePicker.Value = CDate(ds.Tables("MasterMB4").Rows(i).Item("ProdDate"))
0

Now I'm able to prompt the value from Access to DateTimePicker(Form). Below is my code and please feel free to point out if it need to be improved.

If DBNull.Value.Equals(ds2.Tables("MasterMB4").Rows(i).Item("ProdDate")) Then
     proddate_tb.Text = Date.Now
Else
     proddate_tb.Text = ds2.Tables("MasterMB4").Rows(i).Item("ProdDate")
End If

I'm not sure if declare a different ds(DataSet) can give much impact to the coding because previously I'm using ds(DataSet) in another Private Sub under the same Windows Form and after I'm using a different ds(DataSet) which is ds2, IF...ELSE statement and DBNull.Value.Equals, DateTime from Access are now able to prompted to DateTimePicker in Form.

I'm able to do it after read answer from What is a NullReferenceException, and how do I fix it? and from @The White Wolf.

stillLearning
  • 84
  • 2
  • 11