value from cbo box and text box

M

Memphis

Hello,
So, I have a txtbox1, a combo box, and txt box2
txtbox1 has the user's name, the combo box has "Blank", "YES", and "NO".
I need the cbo box to copy the data from txtbox1 to txtbox2 if the selection
is "YES". I would like for this to happen instantly.
I also want the value to remain blank in txtbox2 if the cbo has no value
("Blank").
My problem comes when I click YES and then it copies the data to txtbox2 and
if I need to change the value to either Blank or NO, the data does
not update until I change the focus to another object.

This does not occurr with a check box, things happen instantly, so why can't
this also happen by selecting a value from a cbobox?

Having said this, a chk box has only two values, this one has 3 values,
perhaps this is where the problem lies.

here is my code..

Thank you for your help.

Private Sub cboNameMatch_Change()
If MatchAllegation_01.cboNameMatch.Value = "YES" Then
MatchAllegation_01.txtUserNameAllegation.Value =
MatchAllegation_01.txtUserName.Value
ElseIf MatchAllegation_01.cboNameMatch.Value = "NO" Or
MatchAllegation_01.cboNameMatch.Value = "" Then
MatchAllegation_01.txtUserNameAllegation.Value = ""
End If
End Sub
 
D

Dave Peterson

I created a small userform with a combobox and two textboxes and named them
nicely.

This was the code I used behind the userform:

Option Explicit
Private Sub cboNameMatch_Change()
If UCase(Me.cboNameMatch.Value) = UCase("YES") Then
Me.txtUserNameAllegation.Value = Me.txtUserName.Value
Else
Me.txtUserNameAllegation.Value = ""
End If
End Sub
Private Sub UserForm_Initialize()
With Me.cboNameMatch
.AddItem "YES"
.AddItem "NO"
.AddItem ""
End With
End Sub

The "Me" in the code is the keyword for the object that owns the code--in this
case, my userform. (I don't need to use its name.)

And if I changed (really changed!) the value in the combobox, the textboxes
updated right away.

But if I tried to change from Yes to Yes, then that really isn't a change, so
nothing happened.

If this works for you in a test userform, try it in your real userform.

Just a curiousity question...

Why do you have "" as an option in that combobox?

And since you're only using two options (Yes/No), you could use a checkbox or
even a pair of optionbuttons.
 
M

Memphis

Thank you very much for your help Dave, it worked wonderfuly.

I stay clear from a chk boxes because the graphical representation of a chk
box with no Value is a grayed out check mark inside the chk box, this might
confuse people. For my DB i need to collect yes, no and also a blank value.

I have the rowsource of the cbo set to "=Sheet1!A5:A7", this is where A5
has no text, A6 shows "YES", and A7 shows "NO".

Is this code supposed to replace my RowSource under the cbo properties?
What is its function?

Private Sub UserForm_Initialize()
With Me.cboNameMatch
.AddItem "YES"
.AddItem "NO"
.AddItem ""
End With
End Sub

Thank you.
 
D

Dave Peterson

Yep. I didn't know how you created the cbonamematch combobox. It seemed pretty
easy for my testing to just add the items.

And less dangerous, too...

If I'm using a rowsource, I like to keep it on a hidden sheet. Then I know
it'll be much more difficult for the average user (including me) to mess it up
(inserting/deleting rows or columns--or changing data).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top