Listbox population sometimes shows number instead of text value - sql

I have a listbox in a userform. I use a SQL query to pull data and then populate the listbox. When I look at the query in SQL, it is fine and shows text in those fields. The listbox is showing these numbers though, 4 and 5, what I am assuming is the column name or fields value?
It should say "Landfill/Wellfield" and "O2 levels", so it is not a text length issue. This is all test data. The last column correctly shows "Sparkplugs/Ignition System"
The listbox has 7 columns of data.
Code for the listbox population:
lbx_SSM.Clear
Do Until XL_RSet.EOF = True
For i = 1 To 7
If i = 1 Then
lbx_SSM.AddItem (XL_RSet.Fields(columnName(i)))
Else
If Not (XL_RSet.Fields(columnName(i)) = "Null")
Then lbx_SSM.List(lbx_SSM.ListCount - 1, i - 1) =(XL_RSet.Fields(columnName(i)))
End If
Next i
XL_RSet.MoveNext
Loop

I figured it out, I am not sure what the issue was, but using this to populate the listbox instead of the above does not cause the issue. I've used that first code all the time before so it might be a Fields() issue or something. See below:
XL_RSet.Open
XL_RSet.MoveFirst
i = 0
With Me.lbx_SSM
.Clear
Do
.AddItem
.List(i, 0) = XL_RSet![ID]
.List(i, 1) = XL_RSet![Unit]
.List(i, 2) = XL_RSet![Begin Time] ***Etc for other columns
i = i + 1
XL_RSet.MoveNext
Loop Until XL_RSet.EOF
End With

Related

VBA ComboBox Value by Index

I've got a form that contains a combobox which contains the values
With io6
.AddItem "90°"
.AddItem "180°"
.AddItem "270°"
.AddItem "360°"
End With
The member of this method I am actually using is .ListIndex. When a user selects 270 degrees, I am sending a 2 to a variable. When I read the variable, I want have the combobox correctly show the appropriate value. In other words, if the variable is 1, I want the combo box to show 180 degrees. Is that achievable without a select\if statement that writes into .Value?
Sub varChange(val as integer)
With comboBox
Select Case val
Case 1
.value = "90°"
Case 2
.value = "180°"
Case 3
.value = "270°"
Case 4
.value = "360°"
End Select
End With
End Sub
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
I'm not sure if I got it right, but I think you want to get the list index of the selected item.
You can get that by
x = io6.ListIndex + 1
The +1 is there since ListIndex is 0 based, but you wanted it 1 based.
And something completely different,
Remove the brackets. Method calls without return values does not use them in VBA
Edit after comment
To take this the other way, i.e getting a value from an index value, do like this:
y = io6.List(x - 1)
sValue = combobox.List(combobox.ListIndex,0)
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
Yes,
I used the following line:
io6.ListIndex = val - 1
So in your code it's:
Sub varChange(val as integer)
comboBox.ListIndex = val - 1
End Sub

If cell is blank then make variable equivalent to any value

I have a workbook with 3 List Boxes. Each List Box is populated by a list that is created, each List Box contains a "Blank" data point. I am trying to write a script that goes:
For lpRows = 1 to 100
For lpColumns = 1 to 8
If ListBox1 = "" and ListBox2 = "Val" And ListBox3 = "Var" Then
Sheets(RndmSheet).Cells(lprows,lpcolumns) = Sheets(DiffSheet).Cells(lprows,lpColumns)
else
end if
However, my code isn't working because VBA is simply searching for a blank cell, where I would prefer it to return any value that contains both ListBox2 and ListBox3, but Listbox1 would be irrelevant.
Is there an easy way to use a wildcard to achieve what I want or will I need to simply make a rather large nested if statements to handle this?
I just added in an if statement
if sheets(rndmsheet).cell(lpRows,lpColumns) = "" then
VarCell = Sheets(rndmsheet).cell(lprows,lpcolumns)
end if
Sorry if that doesn't really make sense.

if cell value 1 then listbox .listindex = value

I have the following code:
If ws.cells(A,B).value = "1" then .ListBox1.List(.ListBox1.ListIndex,5) = Checkbox.name
but it prompts an error and i'm not sure how to fix it
essentially, what i'm trying to achieve is that if the cell (A,B) has a value of 1 then insert a name of the checkbox into listbox, listindex of 5.
all other previous listindexes are added by the following code:
.list(.listcount -1, 1) = ws.cells(C,D).value
.list(.listcount -1,1) = ws.cells (E,F).value
This is how you populate a ListBox with items.
For i = 1 to 5
With me.ListBox1
.AddItem i
End With
Next
So assuming you have ListBox1 in UserForm1, this will result to:
Let's say you want to replace 5 by 6, you use .List property like this:
Me.ListBox1.List(4, 0) = 6
Syntax: expression.List(pvargIndex, pvargColumn)
Again, when you use .List property, Index and Column is always 1 less the actual Item and Column.
Since we want to replace 5 by 6 in a ListBox with 1 Column and 5 items in it, the Index of 5 is 4 and it's Column is 0.
The result would be:
Now, what if you have multiple-column ListBox and you want to populate those Columns.
Say Column 2 of our ListBox1, we use this code.
For i = 1 To 5
With Me.ListBox1
.List(i - 1, 1) = "Item" & i
End With
Next
Take note of the Index which is i - 1 and the Column which is 1 (actual column count -1)
The result would be:
So let's go back to your question.
This line:
If ws.cells(A,B).value = "1" then .ListBox1.List(.ListBox1.ListIndex,5) = Checkbox.name
will fail since you are trying to populate Column 6 of your ListBox1 which I believe doesn't exist.
Also you used .ListIndex property which returns the currently selected item index.
I don't know where you are executing your code but initially, .ListIndex value is -1 since it will only have value on runtime when the user get to select an Item.
Now these lines:
.list(.listcount -1, 1) = ws.cells(C,D).value
.list(.listcount -1, 1) = ws.cells(E,F).value
actually just replaces the last item second column value.
First it passes ws.cells(C,D).Value to it then overwrites it with ws.cells(E,F).value.
Hope this anwers some of your questions a bit.

vba listbox multicolumn add [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding items in a Listbox with multiple columns
With MFC VC++ there are two controls, ListBox and ListCtrl. But with VBA it seems we have only ListBox.
I want to create a listbox with 2 columns (Company_ID, Company_Name).
Here is what I tried:
I created lstbox(control type ListBox)
Row source type = value list
I am taking value from user from two edit boxes and when user clicks "add" then it should be added to the listbox with 2 columns.
In the VBA code routine I added the following lines:
lstbox.ColumnCount = 2
lstbox.AddItem (Company_ID)
The following code is not working which seems to be related with adding column value:
lstbox.Column(1,lstbox.ListCount - 1) = Company_name
This gives error:
Runtime error '424' object required.
Could anyone help with vba code to add to multi column listbox.
Simplified example (with counter):
With Me.lstbox
.ColumnCount = 2
.ColumnWidths = "60;60"
.AddItem
.List(i, 0) = Company_ID
.List(i, 1) = Company_name
i = i + 1
end with
Make sure to start the counter with 0, not 1 to fill up a listbox.

Fill a column's blank spaces with like values

I'm trying to add excel files to an access database. I'd like to have each room name searchable by room number. Right now, if you query the room number, it only comes up with the row containing that room number, even though the rows below the room number also contain data for that same room... like so:
http://i802.photobucket.com/albums/yy304/Growler2009/Database_Fill_In.jpg
So, what I'd like to do is write a script that will scan through the room number column (Column 1)... each time the script lands upon a unique room number, it fills in the blank spaces below it with that same room number.
After:
http://i802.photobucket.com/albums/yy304/Growler2009/BlankSpace_FillIn.jpg
So far I've written this test script... but it doesn't seem to be working... Any tips?
Thanks!
RoomNumber = Array(1, 2, 3, 4, 5)
For RoomNumberRow = 1 To 5
For ColIndex = 1 To 5
If Cells(RoomNumberRow, ColIndex).Value = RoomNumber(RoomNumberRow) Then
'Move down a cell
ActiveCell.Offset(1, 0).Select
'changes that cell to the same room number
Cells.Value = RoomNumber(RoomNumberRow)
End If
Next RoomNumberRow
Assuming the materialDescriptionColumn is not empty until the end of the list, and the roomNumberValue is not empty in the first row.
If all the columns have empty fields before the end of the list, a For loop would be an option.
counter = 'First row.
Do Until IsEmpty(Cells(counter, materialDescriptionColumn).Value)
roomNumberValue = Cells(counter, roomNumberColumn).Value
If roomNumberValue = "" Then
Cells(counter, roomNumberColumn) = roomNumber
Else
roomNumber = roomNumberValue
End If
counter = counter + 1
Loop