Retrieve Column 1 value from VBA ComboBox after using "combobox.value =" to modify selected index value - vba

Hello I am working with VBA and I have run into a strange situation. I am hoping that someone out there has run into the same thing and can give me a little help. In my program, there is a 2 column combo box that has it's value set to a string. The value is set like this:
ComboBox1.Value = "this_string"
Initially this ComboBox has 2 legit columns. I am able to confirm this by using the Immediate Window Later in code the first column of that combo box is checked. For example:
value = ComboBox1.Column(0)
But the code breaks here. I get this error:
Could not get the Column property. Invalid property array index.
After the value assignment. I can no longer get any Column values. Why is this?
Thank you for your help,
Billy

This question should not have a vb6 tag. - Unlike VBA, Vb6 only has single column comboboxes.

Related

MS Access 2016 vba: Empty value passed from form field (text box)

I'm having a strange situation:
I have a simple form with 2 text boxes.
The second one has the vba code triggered after being updated.
For example: typing value to the first field, ENTER, typing value into the second field, ENTER and the code starts.
The code, initially, takes the values from the text boxes and assign them to the string variables (pre declared as strings), like:
test1 = Me.frmSSN.Value
The problem is, the test1 variable seems to be empty after the line above.
It seems to happen only when I type, for example this string:
073QB8KJ2D00A4X
It works fine, when entering CNB0K2W5JK
The tool is a simple serial number comparison.
Just for test, I've entered this line into the code:
aaa="073QB8KJ2D00A4X"
When running in the step-by-step mode and hovering mouse over the "aaa" I'm getting: aaa= and then nothing.
Not even single "" like aaa="" or so. Just aaa=
After retrying multiple things - I believe it's about the value I enter:
073QB8KJ2D00A4X
Could be, that for access/vba it's some control string or so?
I'm just dumb now...
Thanks in advance for any help
Marek
p.s. Source fields are plain text boxes. And here's the code:
Dim user As String
Dim 1stSN As String
Dim 2ndSN As String
1stSN = Me.frmSSN.Value
2ndSN = Me.frmHPSN.Value
Then the values are being used as a part of SQL query. The problem is - in this situation - query doesn't work, as the sql string looks like:
"Select * From sbo_SerialSource where SN like " and nothing after "like".
Debug shows the correct value (with serial number), but query fails with "syntax error" message. Seems like there are some strange "control characters" are being created/added.
That's it.
And I have to use the serial numbers as these "strange ones", because that's how they come from the vendor.

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.

Comparing Excel cell values and a userform Textbox

Ok, the problem is that im working in a excel userform, but i cannot find a way to do a comparation between a textbox value and a cell value and if some statement is meet it will write other value to another textbox (the program is has already been made with the use of excel macros, but i wanted to make it look better, and im trying to use the userform)
Here is the code i am trying to run
Select Case (TextBox1)
Case Is <= Sheet2.Cells(3, 8)
TextBox21 = 4
End Select
(When you run the code its does nothing)
Do i need to do something else to make it work?, because im using the same excel file of my tables to make the userform,if i'm confusing something please do let me know.
Thanks for your answer and sorry for my English.
If CInt(TextBox1.Value) <= CInt(Sheet1.Cells(1, 1).Value) Then
TextBox2.Value = 4
end if
The problem is that you are trying to compare strings, when you should be comparing numbers. You just need to convert the values to an int. You might also want to put in a little more validation to ensure that textBox1 and cells(1,1) are in fact numeric values.

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.

How do you use the value of a named cell in a macro in a different sheet, same workbook?

I'm just not getting it, I'm using Excel 2003 and am totally confused as I'm just not getting it ...can anyone help? I need to check that named value with a number of column headings on the active sheet and then insert a column to the left of the column holding the matching text. I'm sure that bit is very hard - but I'm not even able to get started here...
Well, to get your named value column (which I presume is a named range...), you would use:
ActiveWorkbook.Sheets("mySheet").Range("myRange").Column
So you could do something like:
myNamedRange = ActiveWorkbook.Sheets("mySheet").Range("myNamedRange").Value
myCol = ActiveWorkbook.Sheets("myMainSheet").Rows("1:1").Find(myNamedRange).Column
ActiveWorkbook.Sheets("myMainSheet").Cells(1, myCol).EntireColumn.Insert