set color and focus on textfield

LVWO

New Member
Joined
Feb 7, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Is it posible theway I do this ?
I have a form with 5 textfields and 2 listbox. Some of them are mandatory, others not . A user must fill/select the fields , but sometimes (unfortunately) he can hit the acceptbutton while not all fields are filled in. I mark in another sheet if a textfield is ok . When pushing the accept-button, i control if there are still unfilled fields and i want to set the cursor to that empty field and set the backcolor and the focus to the field
I wrote :

Dim idx As Integer
Dim ctl As Control
idx = xx ' tabindex of the empty textfield , xx is the return of a function, which works correct

For Each ctl In Me.Controls
If ctl.TabIndex = idx Then
ctl.Name.BackColor = vbYellow ---> doesnot work
ctl.SetFocus --> doesnot work
MsgBox ctl.Name & " niet ingevuld of geselecteerd"
Exit For
End If
Next

How resolve ?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I would use the Tag property for the required controls, maybe setting it to Reqd in the property sheet. Then loop over controls only looking to see if anything is in them.
VBA Code:
If ctl.Tag = "Reqd" And Len(ctl) = 0 Then << means is required and nothing is in it
   ctl.Backcolor = vbYellow

There are other ways to detect if nothing is entered into a control - that's just one.
Why it's so hard to set focus to a userform textbox I haven't figured out yet. :(
 
Upvote 0
I would use the Tag property for the required controls, maybe setting it to Reqd in the property sheet. Then loop over controls only looking to see if anything is in them.
VBA Code:
If ctl.Tag = "Reqd" And Len(ctl) = 0 Then << means is required and nothing is in it
   ctl.Backcolor = vbYellow

There are other ways to detect if nothing is entered into a control - that's just one.
Why it's so hard to set focus to a userform textbox I haven't figured out yet. :(
thank yoe
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top