Results 1 to 4 of 4

Thread: Invalid Use Of Null

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Horst
    Posts
    262
    Hi group,

    (Pardon my English, I'm from holland)
    When I am changing records (next, previous, first, last record) and a field is empty I get an error: Invalid Use of Null.

    I filled the database with records (10,000+) from a different application and a lot of records have empty fields.

    How do I 'handle' the error?

    PS I'm a VB beginner and defenitly a ErrorHandler Beginner!

    Sander

  2. #2
    Lively Member
    Join Date
    May 2000
    Location
    Antrim
    Posts
    80
    Hi,

    the problem is probably within your database.

    If it is in access then select the field that is creating the problem in design view and change the 'Allow Zero-Length' property to yes.

    Alternatively if you want to handle the error from VB then you could try something like this:

    Get the run time error number from the message box produced when the error occurs.

    At the start of your procedure slip in a line of code ie:

    On Error GoTo Handler

    Then at the bottom of your procedure type:

    Exit Sub

    Handler:
    If Err.Number = (error number) then
    Move Next
    End If

    Hope this helps.

    Best regards,

    Rob Brown.

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    The problem is because your records contain null values, they cannot directly be assigned to textboxes and other controls. I guess you are using a data control and bound controls.

    In that case I don't know the solution to your problem, except that you can make all the fields in your database compulsory, i.e. Required = Yes and AllowZeroLength = No, or else you can provide default values to those fields which you think will remain null.

    In code I have got this trick to work around the nulls:

    Code:
    Text1.Text = rs.Fields("FirstName") & ""
    This ensures that the null values in the database don't crop up in your forms.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    There is another option to handling fields with NULL values, if you are using code to retrieve records, i.e. as in a select statement then you can use the ISNULL SQL function like so

    Code:
    select ISNULL(Fred,'') as Fred FROM Customers
    here Fred is the name of the field, you must include the "as Fred" or else the column of data will not have an identity and you will get unknown data error (or something like that). This effectively replaces a null field with and empty string so you do not have to think any more about the data in the field after that point. The problem is checked and resolved from the start.

    If the data type of the field is numeric then ISNULL(Fred,10) would work just as well, you can specify what you want for the default value as long as it is the correct data type.
    When assigning to a control like a text box you don't have to keep putting the & "" at the end every time.

    Code:
    Text1.text=rs.Fields("FirstName")
    BTW, there is also a similar vb function


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width