I've created a simple VBA interface to connect Excel to a MySQL DB. The VBA part acts as a preview of data for the user to choose what item he wants to import to the Excel sheet.
Until now I've work with a very complete set of data, but I got to a Table which (because of the nature of the items) some fields are NULL.
Now every time I try to check the values in the VBA I get the Run-time error 13 Type mismatch in the listview component. At first I though it was a field with DECIMAL typing, but after changing it to a DOUBLE (for testing) the problem persisted, and it was until I notice that if only checks columns with no NULL value, the problem disappears. Off course I can't omit this values.
I tried some .Tostring functions but with no success. And I failed to implement a IF to check for NULL in the obj.
This is my code:
Private Sub Exec_search_Click()
'Define variables related to Form
Dim Prod_input As MSForms.TextBox
Dim QueryResults As ListView
' Connection variables
Dim rs As ADODB.Recordset
Dim conn As New ADODB.Connection
Dim ConnCmd As ADODB.Command
Dim ColNames As ADODB.Fields
Dim sqlstr As String ' SQL to perform various actions
Dim server_name As String
Dim database_name As String
Dim user_id As String
Dim password As String
Dim ProductName As String
Dim Count As Integer
'Grab the product
Set Prod_inputText = Me.Controls.Item("Prod_input")
ProductName = Prod_inputText.Value
Set QueryResults = Me.Controls.Item("QueryResults")
server_name = "localhost"
database_name = "#####"
user_id = "########"
password = "#########"
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 8.0 ANSI Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password & ""
' Extract MySQL table data to first worksheet in the workbook
Set rs = New ADODB.Recordset
Set ConnCmd = New ADODB.Command
sqlstr = "SELECT referencia, descricao, pvp FROM produtos WHERE referencia LIKE '" & ProductName & "%'" ' extracts all data
rs.Open sqlstr, conn
'This will allow the command object to use the active connection
ConnCmd.ActiveConnection = conn
'Define the query string (Comes from our textbox) & the command type
ConnCmd.CommandText = sqlstr
ConnCmd.CommandType = adCmdText
'Exectue the query & get the column names (Fields)
Set rs = ConnCmd.Execute
Set ColNames = rs.Fields
'Write the results to query section
With QueryResults
'Lets clear the old results
.ColumnHeaders.Clear
.ListItems.Clear
'For each field name, add it to the list view as a column header
For Each ColName In ColNames
'Go to the column headers collection, and use the add method to add a new header
.ColumnHeaders.Add Text:=ColName.Name
Next
' If the recordset is empty, display a message
If rs.EOF Then
'Display the message
MsgBox Prompt:="The query returned no results. No Data to display.", Buttons:=vbInformation, Title:="Query Status"
'Close the connection
conn.Close
'Exit the sub
Exit Sub
Else 'Otherwise begin populating the list view
'Grab the recordset
With rs
'Keep going until you've reach the end of the recordset
Do Until .EOF
'Initialize a count, this will help to determine whether to add a new row vice a new column
Count = 1
'Loop through all the fields in the recordset
For Each fld In .Fields
'If it's the first field of the recordset, that means we have the first column of a new row
If Count = 1 Then
'If it's a new row, then we will add a new ListItems (ROW) object
Set ListItm = QueryResults.ListItems.Add(Text:=fld.Value)
Else
'If it's not a new row, then add a ListSubItem (ELEMENT) instead
ListItm.ListSubItems.Add Text:=fld.Value
End If
'Make sure to increment the count, or else EVERYONE will be a "New Row"
Count = Count + 1
Next
'Move to the next recordset
.MoveNext
Loop
End With
'When you're done with all the recordsets close the connection
conn.Close
End If
End With
End Sub
I get the error in the line : ListItm.ListSubItems.Add Text:=fld.Value
I only started working on VBA very recently, so I might be missing something simple, and I didn't find anything related to this problem in here, but also, I'm very new to SO.
isnull(referencia,0)would replace nulls for 0 ? Or a similar check before putting the value in