Results 1 to 9 of 9

Thread: Invalid use of null

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Location
    Dhaka, Bangladesh
    Posts
    222

    Invalid use of null

    Code:
    Set cn = New ADODB.Connection
        
        cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                              "Data Source=" & App.Path & "\home.mdb"
      cn.Open
        Set rs = New ADODB.Recordset
        
        
    rs.Open "select sum(income),sum(Spendecher) from main where date between #" & DTFr.Value & "# and #" & DTTo.Value & "#", cn
    
    Label3.Caption = rs(0)  'Your total Income..
    Label5.Caption = rs(1)  'Your Total Expenses..
    rs.Close
    a = Label3.Caption
    b = Label5.Caption
    the above code works fine. but after few days it show a msg, " Invalid use of null"
    Why this msg show? and highlight this
    Code:
    Label3.Caption = rs(0)
    then I change the code. I write
    Code:
      If Not IsNull(rs.Fields(0)) Then
       
            Label3.Caption = rs.Fields(0)
       
          End If
    
      If Not IsNull(rs.Fields(1)) Then
       
            Label5.Caption = rs.Fields(1)
       
          End If
    now show "Type mismatch" and highlight
    Code:
    a = Label3.Caption
    any body help me why the 1st msg show and now what I need to do?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Invalid use of null

    You can append a null string to the value to solve the 'invalid use of null' problem. And you should check if the Caption is numeric before setting it to your variable.
    Code:
    Label3.Caption = rs.Fields(0) & vbNullString
    Label5.Caption = rs.Fields(1) & vbNullString
    
    If IsNumeric(Label3.Caption) Then
     a = Label3.Caption
    End If
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Invalid use of null

    a = Label3.Caption
    How is "a" declared?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Location
    Dhaka, Bangladesh
    Posts
    222

    Re: Invalid use of null

    Quote Originally Posted by dee-u View Post
    You can append a null string to the value to solve the 'invalid use of null' problem. And you should check if the Caption is numeric before setting it to your variable.
    I declare variable
    Code:
    dim a as integer
    dim b as integer
    now that msg not show. but label3.caption not show any thing. another thing why this msg not show at 1st time and why show now? I didn't make any change, then why?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Invalid use of null

    Thread moved to 'Database Development' forum (the 'VB6' forum is only meant for questions which don't fit in more specific forums)

    now that msg not show. but label3.caption not show any thing.
    If you are using what dee-u showed (and you should), that is entirely expected - because there is no number to put in it.

    another thing why this msg not show at 1st time and why show now? I didn't make any change, then why?
    Your original code assumed that you would always get a record containing numbers back from the SQL statement, which is not the case - you will always get a record (because you are using Sum), but the values will be Null if there are no records in the table that match your Where clause:
    where date between #" & DTFr.Value & "# and #" & DTTo.Value & "#"
    As you have called your field Date (which is the name of a function which returns the current date) it is hard to predict if the field will be used rather than the function - so you should change the field name if you have any choice.

    On top of that, you haven't formatted the values of DTFr.Value and DTTo.Value properly... For how it should be, see the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of this forum)

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Invalid use of null

    I would have assumed that if there were no records in the DB for the specified range you would get 0 back for the SUM() function.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Location
    Dhaka, Bangladesh
    Posts
    222

    Re: Invalid use of null

    Quote Originally Posted by dee-u View Post
    you should check if the Caption is numeric before setting it to your variable.
    plz explain this line.

    GaryMazzone: but it have record.
    Last edited by abu taher; Apr 12th, 2010 at 07:27 AM.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Invalid use of null

    Quote Originally Posted by GaryMazzone View Post
    I would have assumed that if there were no records in the DB for the specified range you would get 0 back for the SUM() function.
    You've got me thinking, and I'm not entirely sure... I know there have been some situations with a Sum that I got Null back, but I can't remember any of the details.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Invalid use of null

    Correct. You'll get null back on aggregates when no rows were returned. 0 is an actual value. NULL means Unknown. Since no rows are returned, the only possible outcome is unknown.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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