Results 1 to 6 of 6
  1. #1
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73

    StrComp causing "Invalid use of Null" error

    Hello folks! I am trying to learn VBA and found a book to help me. I am using some code it gave me for an example of StrComp:

    Private Sub cmdCompare_Click()
    Dim iResult As Integer

    iResult = StrComp(txtFirst.Value, txtSecond.Value)

    Select Case iResult

    Case -1
    MsgBox "The first string is less than the second"

    Case 0
    MsgBox "Both strings are equal"

    Case 1
    MsgBox "The first string is greater than the second"

    Case Else
    MsgBox "One or both of the strings are Null"
    End Select

    End Sub

    The debugger states that the line:

    iResult = StrComp(txtFirst.Value, txtSecond.Value)



    is the culprit, each time I leave one of the text boxes empty. The companion software with the same code that I downloaded for the book does the same thing.

    The book is for access 2007 and I have 2010, so I do not know if something changed or if it is just bad code, but I would like to know what is generating the error.

    I appreciate the help!

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Change your code to:
    iResult = StrComp(Nz(Me.txtFirst,""), Nz(Me.txtSecond,""))
    ...and see what happens.

  3. #3
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73
    It ignores the last Case. If one box is empty, it states the other is greater, and they are equal if they are both empty.

    Is it possible that the code just will not work as it is written (well, I know it doesn't...)

    Ok, I guess I want to know how to make it do what it was intended to, which is tell if one or both strings is null.

    Oh, and if you can explain what I just put in there, (the Nz) it would be much appreciated!

    Thanks!

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Okay, I see the problem. StrComp() returns a variant and you have it defined (Dim'd) as an Integer which will blow up when the Null is returned. Just change this line
    Dim iResult As Integer
    ...to...
    Dim iResult As Variant
    ...and the original code should work.
    You can also look up the Nz() function in help to see that it is doing.

  5. #5
    sephiroth2906 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2011
    Posts
    73
    Ok, after looking up what a variant does that makes sense. If I have code that may return a Null value, then I have to use that instead. I appreciate the help!

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad we could help.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 13
    Last Post: 12-05-2011, 05:10 AM
  2. Update query from form "invalid use of null"
    By Lady_Jane in forum Programming
    Replies: 4
    Last Post: 08-16-2011, 01:37 PM
  3. "Invalid Database Object Reference"
    By jgelpi16 in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 06:39 AM
  4. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 PM
  5. Convert null to "" in Access
    By isaac_2004 in forum Access
    Replies: 1
    Last Post: 12-04-2009, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums