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

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

Related

Select & Assign Specific Row of a ListBox to a Variable As String

I have tried to find an answer to this but have had no luck. I am using VB.NET and VisualStudio 2019
I have a listbox (lboxsectionnames)that has several different section names listed (as shown in the screenshot). I am trying to specify a specific row, and assign that row to a variable as text.
For this instance I am trying to get the first row text, however, I would like to be able to specify row # in future and get text as well.
Dim firstSectionName
lboxSectionNames.GetItemText(0)
firstSectionName = lboxSectionNames.SelectedIndex.ToString
ListBox Text Example
ListBox.Items is an ObjectCollection and can be accessed with your index like this
lboxSectionNames.Items(0).ToString()
or using SelectedIndex like this:
lboxSectionNames.Items(lboxSectionNames.SelectedIndex).ToString()
although you could also do the above like this:
lboxSectionNames.SelectedItem.ToString()

with a VBA userform, how do you populate a combobox with a different text and value?

I am trying to populate a VBA userform combobox with a named range, and I want the text value to display in the combobox for the user to select, but the value I want returned is the cell reference to the initial named range. That way it would point back to the named range, and if I change that text, it would automatically update all the references I have on the sheet that were entered by the form.
Right now I can get the named range to populate the combobox by iterating through the range, and using an .AddItem to give it the textual value, but then it just dumps that textual value into my spreadsheet, when instead I would like it to have the cell reference from the range that populated it.
The easiest way to do this is to create an Array(X,2) X being the number of items in the array. Then you put the String you want to display in Array(1,1) and the data you want in Array(1,2) then simply populate the combo box like normal. When you and use the array to reference which item they choose. They choose item 8, you know it is Array(8,2) If you need more help please post some code.
So, after more research it would seem that I am unable to directly accomplish what I want with the combobox. I ended up just looping through the range to fill the combobox with the textual values, and then when I submit the form, I looped through the same range of values, checked it if the submitted value matches one from the range of values, and if it does, then return the .Address instead of the .Value, and that gave me the cell reference I was looking for. Too bad the combobox doesn't work more like a real html select menu, it would have made my life a little easier, but in the end I was able to make it work, it just isn't as clean as I would have liked.

Get the id of the selected value in a dropdown validation list on Excel in VBA

Using Excel, lets say I have a validation list made of 5 values like this one :
Patate
Tomate
Courgette
Concombre
Patate
In a cell containing a drop down list made of these 5 value, I select the fifth value : "Patate".
I want to get in VBA that this cell contains the 5th value of my validation list. Something like :
x = Cell.Validation.GetIDValueSelected
x = 5
I can't use Vertical search because I might have 2 or even more time the same value in my list (too long to explain why).
This list is also dynamics (depending of another sheets) so it doesn't always contains 5 values.
I hope I made it clear for everyone to understand my needs but I will be glad to add more information if needed.
Thank you for your time.
Sadly, once you have used DV to fill a cell with junk, there is no way to tell which piece of junk you picked:
You would have to pad each piece of junk with a different number of blanks.

Word 2007 vba, directly select field by name attribute of code property?

Is there a way to have vba select a field in a word doc using the NAME attribute of the field CODE property? maybe using a pseudo selector?
background:
i have 8 fields (DOCPROPERTY Name_First, Name_Last, etc) in my document in 4 places (total 32 items)
instead of looping through the entire collection of fields, I want to be able to return a collection of fields that match Name_First, and then make changes,InsertAfter, etc, then call the update method on those fields only. Updating all fields is causing a noticeable delay on each form field when exited to the next field.
The MSDN documentation only shows examples using ordinal index numbers, which isn't very helpful in my real world. If someone changes the ordinal position of a field, DonkeyKong!
A bookmark can be referenced by name. If you want to update say 4 fields to reflect Name_First, you could place bookmarks on all 4 fields with names like "Name_First_1", "Name_First_2" etc. Then update the fields like:
For i = 1 To 4
ActiveDocument.Bookmarks("Name_First_" & i).Range.Fields(1).Update
Next
In your words: the bookmarked range is the pseudo selector. The field to update is always the first field in the referenced range.

ListBox or Combobox ADODB imported Decimal items invisible in the list

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.