im trying to get the data in my database and show it in my listview (userform). I ran into an error saying the "Type Mismatch" im quite confuse on what part is the mismatch, already search what this error means but i've declare all my variables in correct data types. But since I have blanks or with null value it displays a error. On this line If not IsNull(Records(j, i)) Then li.ListSubItems.Add , , Records(j, i) Im trying to display null or empty on listview the mismatch error was gone but if column1 is blank and column2 have is not blank, the values on column2 will be pasted on column1.
Private Sub ListView_Data()
On Error GoTo ERR_Handler
Dim sql As String
Dim con As ADODB.Connection
Dim rst As New ADODB.Recordset
Dim Records As Variant
Dim i As Long, j As Long
Dim RecCount As Long
Set con = New ADODB.Connection
con.Open "Provider=SQLOLEDB;Data Source=10.206.88.119\BIWFO;" & _
"Initial Catalog=TESTDB;" & _
"Uid=user; Pwd=pass;"
sql = "select * from [TESTDB].[dbo].[tbl_MN_Omega_Raw]"
rst.Open sql, con, adOpenKeyset, adLockOptimistic
'~~> Get the records in an array
Records = rst.GetRows
'~~> Get the records count
RecCount = rst.RecordCount
'~~> Populate the headers
For i = 0 To rst.Fields.count - 1
ListView1.ColumnHeaders.Add , , rst.Fields(i).Name, 200
Next i
rst.Close
con.Close
With ListView1
.View = lvwReport
.CheckBoxes = False
.FullRowSelect = True
.Gridlines = True
.LabelWrap = True
.LabelEdit = lvwManual
Dim li As ListItem
'~~> Populate the records
For i = 0 To RecCount - 1
Set li = .ListItems.Add(, , Records(0, i))
For j = 1 To UBound(Records)
If not IsNull(Records(j, i)) Then li.ListSubItems.Add , , Records(j, i)
'End If
Next j
Next i
End With
ERR_Handler:
Select Case Err.Number
Case 0
Case Else
MsgBox Err.Description, vbExclamation + vbOKOnly, Err.Number
End Select
End Sub