Date Fields and Null values - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Date Fields and Null values

jbassmanpjbassmanp Posts: 160Member
I am not a new programmer but only been with Visual Basic about a year of so. Have learned a lot and produced some good commerical apps. But I just recently discovered a problem that I can not seem to solve. Never needed to do this before, but in this particular app I found that once a date is entered into a textbox field I can not delete that value and go back to a Null value. I can do this in the data grid mode but not with textboxes. The datebase is set Dates will accept null values and as default they are blank and if never touched remains null and once changed from one valid date to another all is fine. But I can not delete that field in the textbox mode. Get errors about converting string values to date or can messages regarding the null value. It is the same in VB2003 and VB2005, have anyone out there had this problem and better yet solved it...

Comments

  • DrMartenDrMarten Posts: 748Member
    : I am not a new programmer but only been with Visual Basic about a year of so. Have learned a lot and produced some good commerical apps. But I just recently discovered a problem that I can not seem to solve. Never needed to do this before, but in this particular app I found that once a date is entered into a textbox field I can not delete that value and go back to a Null value. I can do this in the data grid mode but not with textboxes. The datebase is set Dates will accept null values and as default they are blank and if never touched remains null and once changed from one valid date to another all is fine. But I can not delete that field in the textbox mode. Get errors about converting string values to date or can messages regarding the null value. It is the same in VB2003 and VB2005, have anyone out there had this problem and better yet solved it...

    ======================================================================

    Hi,

    Instead of using
    [code]
    textbox1.text="" 'Which will make it a NULL string but may work
    [/code]

    Try
    [code]
    textbox1.text=Nothing()
    [/code]


    Regards,

    Dr M.

    P.S. I believe the () brackets are needed.


  • jbassmanpjbassmanp Posts: 160Member
    :
    : Hi,
    :
    : Instead of using
    : [code]
    : textbox1.text="" 'Which will make it a NULL string but may work
    : [/code]
    :
    : Try
    : [code]
    : textbox1.text=Nothing()
    : [/code]
    :
    :
    : Regards,
    :
    : Dr M.
    :
    : P.S. I believe the () brackets are needed.
    :
    :
    I tried your suggestion but it doesn't work.
    I have tryied DOBtextbox.text="" , DOBtextbox.text=Nothing , DOBtextbox.text =CDATE(Dobtextbox.text) and on. The first two gives string convertion to date invalid errors and the CDATE options gives a string to date error. any other suggestion....


  • DrMartenDrMarten Posts: 748Member
    : :
    : : Hi,
    : :
    : : Instead of using
    : : [code]
    : : textbox1.text="" 'Which will make it a NULL string but may work
    : : [/code]
    : :
    : : Try
    : : [code]
    : : textbox1.text=Nothing()
    : : [/code]
    : :
    : :
    : : Regards,
    : :
    : : Dr M.
    : :
    : : P.S. I believe the () brackets are needed.
    : :
    : :
    : I tried your suggestion but it doesn't work.
    : I have tryied DOBtextbox.text="" , DOBtextbox.text=Nothing , DOBtextbox.text =CDATE(Dobtextbox.text) and on. The first two gives string convertion to date invalid errors and the CDATE options gives a string to date error. any other suggestion....


    ======================================================================

    Hi,

    Did you try it with the round brackets as follows?
    [code]
    Dobtextbox.text=Nothing()
    [/code]

    By textbox mode do you mean at program design time?

    Right-click the Dobtextbox while the program isn't running.
    Select PROPERTIES. In the TEXT field box, delete all of the text.
    This then becomes the DEFAULT value for that textbox.

    So when the program is run it will be blank until you assign any text to it with either>>
    [code]
    Dobtextbox.Text="Whatever"
    [/code]

    or

    [code]
    Dobtextbox.AppendText(" Whatever.")
    [/code]


    Regards,

    Dr M.

  • iwilld0itiwilld0it Posts: 1,134Member
    For starters, a database NULL is not the same as an empty string or the "Nothing" keyword in Visual Basic.

    I usually write conversion functions for this scenario ...

    [code]
    Public Function ToDateOrNull(ByVal value As String) As Object
    If IsDate(value) Then
    Return Date.Parse(value)
    End If

    Return Convert.DBNull
    End Function
    [/code]

    When adding back to database, you just wrap this function call around the value that is to be a date or NULL ...

    Here is a silly example of it's usage ...
    [code]
    Dim myDate As Object = ToDateOrNull(txtSomeDate.Text)
    [/code]

    If txtSomeDate does not have a valid date, then a DBNull is returned ...

    In .NET, there is two ways to return a Database NULL ...

    [code]
    Dim nullObj As Object = Convert.DBNull

    -- or --

    Dim nullObj As Object = DBNull.Value
    [/code]
  • jbassmanpjbassmanp Posts: 160Member
    [b][red]This message was edited by jbassmanp at 2006-4-11 7:57:39[/red][/b][hr]
    : : :
    : : : Hi,
    : : :
    : : : Instead of using
    : : : [code]
    : : : textbox1.text="" 'Which will make it a NULL string but may work
    : : : [/code]
    : : :
    : : : Try
    : : : [code]
    : : : textbox1.text=Nothing()
    : : : [/code]
    : : :
    : : :
    : : : Regards,

    : : :
    : : : Dr M.
    : : :
    : : : P.S. I believe the () brackets are needed.
    : : :
    : : :
    : : I tried your suggestion but it doesn't work.
    : : I have tryied DOBtextbox.text="" , DOBtextbox.text=Nothing , DOBtextbox.text =CDATE(Dobtextbox.text) and on. The first two gives string convertion to date invalid errors and the CDATE options gives a string to date error. any other suggestion....
    :
    :
    : ======================================================================
    :
    : Hi,
    :
    : Did you try it with the round brackets as follows?
    : [code]
    : Dobtextbox.text=Nothing()
    : [/code]
    :
    : By textbox mode do you mean at program design time?
    :
    : Right-click the Dobtextbox while the program isn't running.
    : Select PROPERTIES. In the TEXT field box, delete all of the text.
    : This then becomes the DEFAULT value for that textbox.
    :
    : So when the program is run it will be blank until you assign any text to it with either>>
    : [code]
    : Dobtextbox.Text="Whatever"
    : [/code]
    :
    : or
    :
    : [code]
    : Dobtextbox.AppendText(" Whatever.")
    : [/code]
    :
    :
    : Regards,
    :
    : Dr M.
    :
    :
    If you are familar with dataforms in 2003 screens with field and labels are auto. set up and if you desire you just move them around to locations that you want. In 2005 you drag field and drop on form where you want it. Both give you a Label name and Textbox Field. The actual process of saving etc. is built in to code. It does not matter what database i'm using Access, SQL or ?. If the database field is speicified as date in that method once a valid date has been enter only another date can take its place. In fact the screen cursor hangs in that field until valid date is entered. Never was a problem before because if date was not known it was left blank. When known it was entered, if wrong corrected. This app. needs/desires that the field value go back to blank(can be skipped if solution not found). As stated earlier, in the datagrid mode the process is no problem just highlight field data and delete and save changes WahLa you have blank date again. PS. I tried with and without brackets no go.....

    JBP


  • jbassmanpjbassmanp Posts: 160Member
    [b][red]This message was edited by jbassmanp at 2006-4-11 11:30:29[/red][/b][hr]
    : For starters, a database NULL is not the same as an empty string or the "Nothing" keyword in Visual Basic.
    :
    : I usually write conversion functions for this scenario ...
    :
    : [code]
    : Public Function ToDateOrNull(ByVal value As String) As Object
    : If IsDate(value) Then
    : Return Date.Parse(value)
    : End If
    :
    : Return Convert.DBNull
    : End Function
    : [/code]
    :
    : When adding back to database, you just wrap this function call around the value that is to be a date or NULL ...
    :
    : Here is a silly example of it's usage ...
    : [code]
    : Dim myDate As Object = ToDateOrNull(txtSomeDate.Text)
    : [/code]
    :
    : If txtSomeDate does not have a valid date, then a DBNull is returned ...
    :
    : In .NET, there is two ways to return a Database NULL ...
    :
    : [code]
    : Dim nullObj As Object = Convert.DBNull
    :
    : -- or --
    :
    : Dim nullObj As Object = DBNull.Value
    : [/code]
    :
    Dr. M. and iwilld0it I thank you both for your quick reponses to the question. iwilld0it hit it right on the head, problem gone. Man that was a sweet line of code cleared up the issue very nicely. That program/app is scheduled for deployment to user testing and hopefully completion this week. Once again thanks





  • iwilld0itiwilld0it Posts: 1,134Member
    Glad to have been helpful ...

    : [b][red]This message was edited by jbassmanp at 2006-4-11 11:30:29[/red][/b][hr]
    : : For starters, a database NULL is not the same as an empty string or the "Nothing" keyword in Visual Basic.
    : :
    : : I usually write conversion functions for this scenario ...
    : :
    : : [code]
    : : Public Function ToDateOrNull(ByVal value As String) As Object
    : : If IsDate(value) Then
    : : Return Date.Parse(value)
    : : End If
    : :
    : : Return Convert.DBNull
    : : End Function
    : : [/code]
    : :
    : : When adding back to database, you just wrap this function call around the value that is to be a date or NULL ...
    : :
    : : Here is a silly example of it's usage ...
    : : [code]
    : : Dim myDate As Object = ToDateOrNull(txtSomeDate.Text)
    : : [/code]
    : :
    : : If txtSomeDate does not have a valid date, then a DBNull is returned ...
    : :
    : : In .NET, there is two ways to return a Database NULL ...
    : :
    : : [code]
    : : Dim nullObj As Object = Convert.DBNull
    : :
    : : -- or --
    : :
    : : Dim nullObj As Object = DBNull.Value
    : : [/code]
    : :
    : Dr. M. and iwilld0it I thank you both for your quick reponses to the question. iwilld0it hit it right on the head, problem gone. Man that was a sweet line of code cleared up the issue very nicely. That program/app is scheduled for deployment to user testing and hopefully completion this week. Once again thanks
    :
    :
    :
    :
    :
    :

Sign In or Register to comment.