ADO recordset null record displayed as 1

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi

Another quirk I can't see reference to.

I am pulling a small recordset out of an Access database. It is a single field recordset. Some records are null. If I try and load the recordset into an array I get a mismatch error. The field is set as a text fields. On closer inspection I notice that the null value is converted into the number 1 (hence the mismatch).

I found out using:
Code:
?m_clsDB.RunScript(strSQL).GetString(adClipString,-1,"|",vbcrlf,vbnull)

(The RunScript function returns a recordset using the recordset open method.)

Which returned:
Code:
[B][COLOR="Red"]1[/COLOR][/B]
Development & Projects
IT
Local Buying
Logistics
Mobility
Professional Services

The #1 above is actually a null value in the recordset. Why is it being converted to #1?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Because that's what vbNull is... :)
 
Upvote 0
Oooohh I see! Ok so I change it to vbNullString and it's as expected.

So the mismatch when converting the recordset to variant array isn't because it contains a number record. Any idea why else I'd get a mismatch error when using GetRows? Is it simply because it doesn't like a null item?
 
Upvote 0
What variable type are you assigning to? GetRows returns a variant.
 
Upvote 0
Code:
Dim varData as Variant

And then:

Code:
varData = Application.Transpose(m_clsDB.RunScript(strSQL).GetRows)
 
Upvote 0
What's the full code line?
 
Upvote 0
I was editing when you replied. Actually the complete code is:

Code:
Public Property Get Categories() As Variant
    Dim strSQL As String
    
    strSQL = "SELECT DISTINCT KPI_CATEGORY FROM MKPI_TBL;"
    Categories = Application.Transpose(m_clsDB.RunScript(strSQL).GetRows)
End Property
 
Upvote 0
I suspect it's an issue with Transpose. If you remove that does it work?
 
Upvote 0
Yes it does work. Although I'm sure I had a good reason for using Transpose, only I can remember now. :confused:

Thanks...again! :)
 
Upvote 0
GetRows returns the columns as the first dimension, which is why you need to transpose. My preference is to use my own transpose function rather than Application.transpose which seems to be touchy about a lot of things.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top