Capture Unbound Form Control's Value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello. I'm trying to capture a controls value: a dropdown box with two
selections (either "Red" or "Blue") keeps returning the value "Red" even if
"Blue" is selected?
 
Hello. I'm trying to capture a controls value: a dropdown box with two
selections (either "Red" or "Blue") keeps returning the value "Red" even if
"Blue" is selected?

Donald, which property are you using to access the value? Could you
post you code as well?
 
Say I have a control "CboColor" on a form with the "ITEMS" collection set to
list 'Red' and 'Blue' and the "TEXT" value set to 'Red'. I'm trying to get
the value of the control via code as such:

Dim strCboColorVal
strCboColorVal = Me.CboColor.Text()

Then to check if it's working:

Debug.Print(strCboColorVal)

And the value 'Red' occurs even if 'Blue' is selected in the dropdown box.
 
Say I have a control "CboColor" on a form with the "ITEMS" collection set to
list 'Red' and 'Blue' and the "TEXT" value set to 'Red'. I'm trying to get
the value of the control via code as such:

Dim strCboColorVal
strCboColorVal = Me.CboColor.Text()

Then to check if it's working:

Debug.Print(strCboColorVal)

And the value 'Red' occurs even if 'Blue' is selected in the dropdown box.






- Show quoted text -

Try using...

strCboColorVal = Me.CboColor.SelectedItem.Text
 
..Text is not available for .SelectedItem. I tried .ToString and got same
unwanted result.
 
.Text is not available for .SelectedItem. I tried .ToString and got same
unwanted result.







- Show quoted text -

Sorry, I mistyped on that last one...

strCboColorVal = Me.CboColor.SelectedText
 
That's not working either. Here's a part of what I'm working with:

If DUEDATE < Now() And
frmMain.CboOdueColor.Text = "Red" Then
Debug.Print(frmMain.CboOdueColor.Text)
With oSheet.Range("A" & iRow, "K" &
iRow)
.Font.ColorIndex = 3
Debug.Print("Color Set to RED")
End With
ElseIf DUEDATE < Now() And
frmMain.CboOdueColor.Text = "Blue" Then
Debug.Print(frmMain.CboOdueColor.Text)
With oSheet.Range("A" & iRow, "K" &
iRow)
.Font.ColorIndex = 5
Debug.Print("Color Set to BLUE")
End With
End If
 
That's not working either. Here's a part of what I'm working with:

If DUEDATE < Now() And
frmMain.CboOdueColor.Text = "Red" Then
Debug.Print(frmMain.CboOdueColor.Text)
With oSheet.Range("A" & iRow, "K" &
iRow)
.Font.ColorIndex = 3
Debug.Print("Color Set to RED")
End With
ElseIf DUEDATE < Now() And
frmMain.CboOdueColor.Text = "Blue" Then
Debug.Print(frmMain.CboOdueColor.Text)
With oSheet.Range("A" & iRow, "K" &
iRow)
.Font.ColorIndex = 5
Debug.Print("Color Set to BLUE")
End With
End If







- Show quoted text -

Assuming you used the designer property dialog to set the Items
collection by typing Red on the first line, and Blue on the second
line, the follwing code will produce th correct result...

If frmMain.cboOdueColor.SelectedItem.ToString = "Red" Then

....

End If

will give you the result you are looking for.
 
Still not working. I'm using this code from a separate class to catch/use
the values of the controls but they are only returning the values they were
set to at design time. How to return current values?

Dim strFontSize
strFontSize = frmMain.NumFontSize.Value.ToString
If strFontSize = 10 Then
Debug.Print(strFontSize)
ElseIf strFontSize = 17 Then
Debug.Print(strFontSize)
End If
Dim ODueFontColor, ODueFontColorVal
ODueFontColor = frmMain.CboOdueColor.SelectedItem.ToString
If ODueFontColor = "Red" Then
ODueFontColorVal = 3
Debug.Print(ODueFontColorVal)
ElseIf ODueFontColor = "Blue" Then
ODueFontColorVal = 5
Debug.Print(ODueFontColorVal)
End If
 
Donald Fisher said:
Still not working. I'm using this code from a separate class to
catch/use
the values of the controls but they are only returning the values
they were
set to at design time. How to return current values?

Dim strFontSize
strFontSize = frmMain.NumFontSize.Value.ToString
If strFontSize = 10 Then
Debug.Print(strFontSize)
ElseIf strFontSize = 17 Then
Debug.Print(strFontSize)
End If
Dim ODueFontColor, ODueFontColorVal
ODueFontColor = frmMain.CboOdueColor.SelectedItem.ToString
If ODueFontColor = "Red" Then
ODueFontColorVal = 3
Debug.Print(ODueFontColorVal)
ElseIf ODueFontColor = "Blue" Then
ODueFontColorVal = 5
Debug.Print(ODueFontColorVal)
End If


First, it's strongly recommended to enable Option Strict. This code is slow,
unsafe and is doing unnecessary conversions.

Are you using different instances of the Form? Ist frmMain the visible
instance of the Form that you want to refer to? If you are not sure, set a
break point at the first line and have a look at frmMain.visible.


Armin
 
I'm not very familiar with option strict but am using explicit at the moment.
As far as I can see there is only one instance of the form frmMain. I set
my break point but how do I look at frmMain.Visible? I know that I can see
my vars etc. in the Autos/Locals windows but am not familiar with the
property you speak.
 
Wow! I've got it! At least it's working anyway. I'd still like to learn
about using option strict and "looking" at frmMain.Visible though. I added
some code to the control's ValueChanged property:

Private Sub NumFontSize_ValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles NumFontSize.ValueChanged
Dim strFontSizeSel
strFontSizeSel = Me.NumFontSize.Value.ToString
Debug.Print(strFontSizeSel)
strFontSizeglob = strFontSizeSel
Debug.Print(strFontSizeglob)
End Sub

When I fetch strFontSizeglob from my class it works perfect!
 
Donald Fisher said:
Wow! I've got it! At least it's working anyway. I'd still like to
learn about using option strict

Enable it in the project properties. Right below the Option Explicit
setting.
and "looking" at frmMain.Visible
though.

Visible is a property of the Form. You can examine it in the quick watch,
watch or debug window (? frmMain.visible).
 
Thanks for the info. I ended up switching from a numericupdown control to a
dropdown text box since I couldn't get the numericupdown control to update
automatically to the stored setting value. If anyone figures out a working
method to do that please post. Thanks!
 
Back
Top