Get linked control label Caption text (1 Viewer)

Falcon88

Registered User.
Local time
Today, 22:07
Joined
Nov 4, 2014
Messages
299
How to get caption text of linked labels for every control on a form


I have a form based on a table with this textboxes and combo boxes :

- txtOrderID
its linked label caption : Order Number

- txtOrderDate:
its linked label caption : Visit Date

- cboCompanyNm :
its linked label caption : Company Code

- cboEmployeeNm:
its linked label caption : Employee Name

- txtWriter:
its linked label caption : Writer Name

And multi other controls .


If a user let any control blank . How to inform user by refering not to the
control name , i want to refer to the
Control by its linked label caption ?
Example: if user let cboCompanyNm blank , inform user beforeupdate that record by msg "Company Code is blank please select any company. " then set focus on cboCompanyNm combobox.

Is that enable and why ?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:07
Joined
May 21, 2018
Messages
8,527
Me.sometxtbox.controls(0).caption
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:07
Joined
Feb 19, 2013
Messages
16,612
providing the label control is bound to the textbox control you will want

textboxname.controls(0).caption
 

Falcon88

Registered User.
Local time
Today, 22:07
Joined
Nov 4, 2014
Messages
299
Very thanks another time .

How to refere to the control's caption that contains mark of ( * ) , example :

- txtOrderID *:


- txtOrderDate *:


- cboCompanyNm *:

- cboEmployeeNm*:

- txtWriter*:


How to do the code to loop through
the controls that is ( null ) and their
label caption text contains ( * ) ?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:07
Joined
Oct 29, 2018
Messages
21,473
Very thanks another time .

How to refere to the control's caption that contains mark of ( * ) , example :

- txtOrderID *:


- txtOrderDate *:


- cboCompanyNm *:

- cboEmployeeNm*:

- txtWriter*:


How to do the code to loop through
the controls that is ( null ) and their
label caption text contains ( * ) ?
Hi. You could use the InStr() function. Alternatively, you might also consider using the control's Tag property to mark the controls with a "*" to easily work with them.
 

Falcon88

Registered User.
Local time
Today, 22:07
Joined
Nov 4, 2014
Messages
299
I try to get the controls that have a label caption text that contains "*" as :
Dim ctl as control

For each ctl in me.form
If ctl.type=acTextbox or ctl.type=acCombobox then
If ctl &""="" then
If ctl.controls(0).caption like
"*"&"*"&"*"
Msg ctl.controls(0).caption
End if
End if
End if
Next ctl

But this returns all controls caption .
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:07
Joined
Oct 29, 2018
Messages
21,473
I try to get the controls that have a label caption text that contains "*" as :
Dim ctl as control

For each ctl in me.form
If ctl.type=acTextbox or ctl.type=acCombobox then
If ctl &""="" then
If ctl.controls(0).caption like
"*"&"*"&"*"
Msg ctl.controls(0).caption
End if
End if
End if
Next ctl

But this returns all controls caption .
Hi. Like I said, you could try using the InStr() function. For example:

If InStr(ctl.Controls(0).Caption,"*")>0 Then

Hope that helps...
 

Falcon88

Registered User.
Local time
Today, 22:07
Joined
Nov 4, 2014
Messages
299
I use this code but gives me error 2467
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer) 
' perform data validation 
Dim ctl As Control
For Each ctl In Me
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
          If InStr(ctl.Controls(0).Caption, "*") > 0 Then
            If ctl & "" = "" Then
                MsgBox "You must enter a value to  : " & vbCrLf & _
                    Left(ctl.Controls(0).Caption, (InStr(ctl.Controls(0).Caption, "*") - 1)) & vbCrLf & _
  "field", vbCritical + vbMsgBoxRtlReading + vbMsgBoxRight, "Required Data "
                    DoCmd.GoToControl ctl.Name
                    Cancel = True
                Exit For
            End If
         End If
        End If
    Next ctl
If  Not Cancel Then
 ' passed the validation process
 If  Me.NewRecord Then 
If MsgBox("Data will be saved, Are you Sure?", vbYesNo, "Confirm") = vbNo Then Cancel = True 
Else 
' run code for new record before saving
 End If 
Else
 If MsgBox("Data will be modified, Are you Sure?", vbYesNo, "Confirm") = vbNo Then Cancel = True 
Else 
' run code before an existing record is saved ' example: update date last modified End If End If 
End If 
' if the save has been canceled or did not pass the validation , then ask to Undo changes 
If Cancel Then
 If MsgBox("Do you want to undo all changes?", vbYesNo, "Confirm") = vbYes Then 
Me.Undo
 End If 
End If 
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:07
Joined
Oct 29, 2018
Messages
21,473
I use this code but gives me error 2467
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
' perform data validation
Dim ctl As Control
For Each ctl In Me
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
          If InStr(ctl.Controls(0).Caption, "*") > 0 Then
            If ctl & "" = "" Then
                MsgBox "You must enter a value to  : " & vbCrLf & _
                    Left(ctl.Controls(0).Caption, (InStr(ctl.Controls(0).Caption, "*") - 1)) & vbCrLf & _
  "field", vbCritical + vbMsgBoxRtlReading + vbMsgBoxRight, "Required Data "
                    DoCmd.GoToControl ctl.Name
                    Cancel = True
                Exit For
            End If
         End If
        End If
    Next ctl
If  Not Cancel Then
' passed the validation process
If  Me.NewRecord Then
If MsgBox("Data will be saved, Are you Sure?", vbYesNo, "Confirm") = vbNo Then Cancel = True
Else
' run code for new record before saving
End If
Else
If MsgBox("Data will be modified, Are you Sure?", vbYesNo, "Confirm") = vbNo Then Cancel = True
Else
' run code before an existing record is saved ' example: update date last modified End If End If
End If
' if the save has been canceled or did not pass the validation , then ask to Undo changes
If Cancel Then
If MsgBox("Do you want to undo all changes?", vbYesNo, "Confirm") = vbYes Then
Me.Undo
End If
End If
End Sub
Hi. What is error 2467 and which line is causing it?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:07
Joined
Feb 19, 2013
Messages
16,612
I suspect this line is not doing any good

If ctl & "" = "" Then
 

Falcon88

Registered User.
Local time
Today, 22:07
Joined
Nov 4, 2014
Messages
299
Can any one try to use this code on any single form then on a Continuous form to see the error ?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:07
Joined
Feb 19, 2013
Messages
16,612
just a guess but if you have a continuous form it is likely the control will not have an associated label since it will be in the header section so the label cannot be referenced in this way

If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
If InStr(ctl.Controls(0).Caption, "*") > 0 Then
 

Micron

AWF VIP
Local time
Today, 15:07
Joined
Oct 20, 2018
Messages
3,478
DoCmd.GoToControl ctl.Name
I think it's because an object reference needs to be passed to this method. Rather than create a variable for that and/or SET to it, just use SetFocus method.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:07
Joined
Feb 19, 2013
Messages
16,612
another guess - the control is disabled or hidden
 

Micron

AWF VIP
Local time
Today, 15:07
Joined
Oct 20, 2018
Messages
3,478
just a guess but if you have a continuous form it is likely the control will not have an associated label since it will be in the header section so the label cannot be referenced in this way
I don't get that. Unless you expressly separated them, they'd both be in the detail section. If you separate them, then you can't refer to the controls collection of a control anyway. To answer the inquiry from post 15, I tried it with a cf as a subform - no problem. It's just a test form, hence the otherwise useless names.

?forms!form7.child0.form.locationid.controls(0).caption
LocationID:

I should have paid more attention to post 13 as I think we're off track here. I would think the reference is misspelled or not open based on that.
 

Users who are viewing this thread

Top Bottom