David
David

Reputation: 177

Check if object is Null in Excel VBA

I can't check if an imported value from SQL to Excel is Null. In debug mode I can se that the assigned value is Null, i.e. not the string "Null". Have tried is Nothing as in my code example, but also isEmpty and = "Null".

It's the line If dbList(2) Is Nothing Then in the code below that I'm having trouble with.

How can I check if the recordset is Null?

...
Dim CmdSP As New ADODB.Command
CmdSP.CommandType = adCmdText
CmdSP.CommandText = "SELECT FundName, FundId, SRL.Comment FROM XXX SRL ON XXX = XXX ORDER BY FundName ASC"

CmdSP.ActiveConnection = dbConn

Dim dbList As ADODB.Recordset
Set dbList = CmdSP.Execute

Dim row As Integer
row = 1
While Not dbList.EOF
    DataStorage.Range("dsFundsTopLeft")(row, 2) = dbList(0)
    DataStorage.Range("dsFundsTopLeft")(row, 3) = dbList(1)
    If dbList(2) Is Nothing Then
        DataStorage.Range("dsFundsTopLeft")(row, 4) = "No rating"
    Else
        DataStorage.Range("dsFundsTopLeft")(row, 4) = dbList(2)
    End If
    dbList.MoveNext
    row = row + 1
Wend
...

Upvotes: 2

Views: 9497

Answers (1)

FunThomas
FunThomas

Reputation: 29592

Try function isNull

If isNull(dbList(2)) Then

Upvotes: 5

Related Questions