ListBox or Combobox ADODB imported Decimal items invisible in the list - sql

I have encountered this problem several times already and have been able to work around it till now. Also the almighty search engines didn't help me.
The problem is that when I have populated a listbox or combobox from a ADODB recordset all Decimal data elements are not visible in the box, for example with the following (conn is a ADODB connection):
Private Sub GetFilteredRecords()
Dim strSQL As String
Dim arr As Variant
'create the SQL
strSQL = "SELECT * FROM vwStandard_Fee2"
'execute the SQL and fill the rs ( rsFiltered )
Set rsFiltered = conn.Execute(strSQL)
'Apply recordset to the listbox on the form
If Not (rsFiltered.EOF = True And rsFiltered.BOF = True) Then
arr = rsFiltered.GetRows()
With lbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
With cbDeeper
.ColumnCount = rsFiltered.Fields.Count
.List = TransposeArray(arr)
End With
End If
End Sub
Above contains 6 columns of Ids (all show Type = Variant/Decimal), of which the containing values are all not "shown" for some strange reason. Only the String and Date columns are shown normally, the Decimals are there but empty!
Here some snippets:
Now in case of a combo box I can get one column's value shown if their column the BoundColumn when I select that listitem, but only in the value fo the combobox (so still not in the list).
My initial workaround was to convert them into String values before adding to the Listbox/Combobox, in this case however I want to directly link the query result to the Box.List without looking at the details. And thus I am looking for a solution in stead of a work around.
In short: my numerical field items are invisible BY DEFAULT for some strange reason. Workaround was to make the items String values. I am now looking for a solution for this bug/problem instead:
What is causing this?
How to solve it?

So all string data is appearing? And,only numerics don't appear?
Then you may want to convert your numerics to strings and pass it to your list, combo boxes.
Which you have already done I noticed.
Now for any reason if your max number of rows and length of array/recorders row count doesn't match it could also cause an issue. However it seems your setting rows of combobox using recordset row count. Instead of using an array can you try to iterate over the recordset to populate the combobox? yes this is not performance friendly, buy guess what we need it to work without bugs before optimizing. ;-)
Have you bound your combobox to the recordset? Can you confirm if your array is single dimension and it has data to feed to the box?
You may try to populate the listbox using a saved query in the DB to if the issue still persists.
However, list boxes and combo boxes based on SQL statements are slower than
list boxes and combo boxes based on saved queries.
So can you try the following to set rowsource property? Make sure to test on both number,and test columns. As well as on old combo box and new one.
Rowsource->build query->
sqlview copy to rowsource property box->
delete or don't save that above built query since you already have SQL statement.
Just wanted you to try out possibilities to narrow down the issue.
UPDATING ANSWER WITH MOST POSSIBLE ISSUE AND SOLUTIONS
As per my comments, they mainly given assuming you had issues populating listbox/combobox
I forgot to ask something very very important, have you declared
Option Base 1 to make sure to avoid losing one of the array's column
values if you are dumping 2D array...? because you do not have any
explicit declartion for the array you are using to dump data into the
listbox.......... :)
Make sure your Listbox is enabled to show multi column data.
*So you have three choices, *
Option Base 1
ReDim your array and do looping to fill it and dump it into .list.
Since ReDim array need you to anyway loop through, you may just as well
use the recorset iself to add the data.
You seem to have a dimension issue with the array which is not declared but transposed from recordset and then to listbox/combobox. So your undeclared array is not populating multi-columns properly. That could be the reason it works when you declare array proeprly.......
Infact in your comment you have said so,
When I create an array in my code and populate it in my code (entry by
entry) it will show without any problem – K_B 14 mins ago

OK after going through various possible causes it seems to be the case that:
VBA has no Decimal Variant Type of its own.
VBA can handle Decimal from within a variable declared as Variant (thus becoming a Variant/Decimal)
This normally doesn't stop your program from working, but in Controls like Listbox and Combobox the Type Variant/Decimal is not interpretable and thus wont draw that specific entry.
For example populate a listbox called lbHigher with this:
Private Sub ListBoxProblem()
Dim tempArray(2, 2) As Variant
tempArray(0, 0) = "A"
tempArray(0, 1) = 1
tempArray(0, 2) = 1.1
tempArray(1, 0) = "B"
tempArray(1, 1) = CStr(CDec(5.2))
tempArray(1, 2) = 2.3
tempArray(2, 0) = "C"
tempArray(2, 1) = DateSerial(2012, 12, 13)
tempArray(2, 2) = 100
tempArray(3, 0) = "D"
tempArray(3, 1) = -1
tempArray(3, 2) = CDec(5.2)
lbHigher.ColumnCount = 3
lbHigher.List = tempArray
End Sub
Everything works fine except for the CDec(5.2). The CStr(CDec(5.2)) works fine as well as VBA will first have converted the Decimal to String before the Listbox gets to get it.
So either: Dont let the SQL generate any Decimal output OR convert any Decimal output to Single/Double/String/Integer/Long in VBA before handing it to the Listbox.

Related

convert text to single type

Dim fontsize As Single = CSng(SynopsisTSCmbFontSize.Text)
rtbSynopsis.Font = New Font(SynopsisTSCmbFonts.Text, fontsize)
to change the fontsize to the value selected in a combo box, the value has to be of the Single type.
the combobox is populated with numbers entered at the design mode, ranging from 7-78. I know that these are entered as strings.
the error is :
I have tried a number of things to convert the text (which are numbers, no letters) from the combobox to single to no avail. try parse did not work, trimming did not work, first convert to INT or DBL, then to SNG did not work.
What is the correct syntax here?
I would have thought that it was pretty standard stuff to change the fontsize.
I found a solution : instead of populating the combobox at design time, I populated it at runtime where I had full control over the type.
Dim i As Single
For i = 5 To 70
SynopsisTSCmbFontSize.Items.Add(i)
TreatmentTSCmbFontSize.Items.Add(i)
Next
once the comboboxes are correctly populated, I can run the rest of the code with no errors
thank you all for your time!

Multiply two text box values and show sum in third text box

I need to multiply the values of two text boxes and display the sum in a third text box in Word.
I have tried every variation of code I can think of.
When entering data in the first two text boxes, no error is generated but no answer appears in the third text box.
I am trying to multiply the results entered into Word text boxes (Legacy Forms, Text Form Fields) bookmarked "Text61" and "Amount247", showing the results in the same type of text box, bookmarked "Text71".
Sub MultiplyTotal()
Dim ff As String
ff = ActiveDocument.FormFields("Text61").Result
ff = ActiveDocument.FormFields("Amount247").Result
ff = ActiveDocument.FormFields("Text71").Result = ("Text61") * ("Amount247")
End Sub
The code in the question is on the right track for performing the calculation, it just needs to use the variable(s) for capturing the results and performing the calculation a bit differently.
One variable is required for each item to be included in the calculation.
Since calculations are done with numbers, it's also helpful to force-convert the text (String) to a numerical value (the Val function), although if the user is careful it's not strictly necessary in VBA - but a good habit.
Sub CalculateFF()
Dim ff1 As String, ff2 As String
ff1 = ActiveDocument.FormFields("Text61").Result
ff2 = ActiveDocument.FormFields("Amount247").Result
ActiveDocument.FormFields("Text71").Result = Val(ff1) * Val(ff2)
End Sub
Note that it's not strictly necessary to use code to perform simple calculations in Word documents containing form fields, where forms protection is activated. A calculation type of form field can be used that executes a calculation without the need of code. If that approach is of interest, ask in an end-user venue such as Super User.

How to maintain formatting for numbers in multi-value combo box?

I have a table that holds data of type Number that has a format of "R-"00 and in my Form that is linked to this table, I have a multi-combo box. When I go to select the values, they are correct - they all have the "R-00n" formatting where n is the actual number entry (auto-numbered). However, when I make my selection, the value will just read as 1, 2, 3 instead of R-001, R-002, R-003. How do I maintain my formatting after selections?
My query is SELECT Reference_ID FROM References for the Data of the combo-box in the form and it is pulling the right records, formatted correctly ("R-001, R-002, R-003") but when I make my selections, the value in the form is only 1, 2, 3. It drops the formatting. Is there something I need to configure in my Form to maintain the custom formatting?
Thank you.
I suppose you mean ListBox and not Combobox, right?
So I also suppose you already have some code to read the selected items of your ListBox like this:
Dim item As Variant
For Each item In YourListBox.ItemsSelected
Debug.Print YourListBox.ItemData(item)
Next
Regarding your problem:
You wrote that the tables field Reference_ID stores the number itself, but because it is formatted by "R-"000 it displays values like R-003.
So you already know, that the value itself stored is without formatting.
You could check that by retrieving the data like this:
?DLookup("Reference_ID","References","Reference_ID = 3")
It will show up 3 and not R-003.
So the same belongs when you read the data from the ListBoxes selected items.
As far as I know you can't read out the formatted text from the ListBox. But what you can do is to format the read value yourself:
Dim item As Variant
For Each item In YourListBox.ItemsSelected
Debug.Print Format(YourListBox.ItemData(item), "R-000")
Next
Another approach could be to set the Row Source of your ListBox to already formatted values:
Select Format(Reference_ID, "R-000") From References

How to make Listbox.List keep type information?

If I affect a full array to a ListBox, using ListBox1.List = [{1;2;3;4}] for example, the Listbox's .List items keep their correct type (here numbers), but if I use ListBox1.AddItem 5 or Listbox1.List(5) = 6 to set an individual item the type is automatically changed to String.
Sample Code:
Private Sub UserForm_Initialize()
ListBox1.List = [{1;2;3;4}]
ListBox1.AddItem 5
ListBox1.AddItem
ListBox1.List(5) = 6
End Sub
Later on, when comparing values, I get wrong results because numbers are not equals to text (5 <>"5").
Is there any easy (1) way to ensure the type of the list items is not converted to String?
(1) I know I can explicitly make the conversion to String, but I rather keep my values as numbers instead of "numbers-strored-as-text" in the listbox
I guess that will be impossible when using AddItem. According to https://learn.microsoft.com/en-us/office/vba/api/access.listbox.additem, the first parameter is a string, so everything you pass will be converted to a string.
Probably your best bet is to collect all items in an array and assign the array using ListBox1.List. Or you have to live with numbers stored as string...
Update
I mixed the pages up - link to the Access page was wrong.
Anyhow, Documentation is rather poor. It says "valid object" but doesn't define what that means. At least it is not possible to add a object - that throws a type mismatch.
Also, the documentation states that a Variant is returned, but it seems thatis not true - when I try to get the result, the compiler throws an error.
As a conclusion, I assume that AddItem converts everything to a string (and throws an error if that fails). So I still assume that you have to build up an array and assign it to List if you want to have real numbers.

adding characters into a datagrid in vb.net

I'm new to vb.net. I would like to know how i could i/p a string via textbox and display its individual characters into a datagrid view in diff rows?
Are you only using a single column in the datagridview? if so you could do this easily by stepping through each character in the string and adding a row containing that character... see the code example below
InputString = TextBox1.Text
For a = 0 To InputString.Length - 1
Datagridview1.Rows.Add(InputString(a))
Next
Cant see why you'd want to do this though - If this isn't what you're trying to accomplish please give us more details