PC Review


Reply
Thread Tools Rate Thread

Assign string to a Control

 
 
alex
Guest
Posts: n/a
 
      16th Dec 2009
Assign string to control

Hello,
Using Access 03

Im attempting to convert a string name to a control nameHeres the
code:

Private Sub Form_Current()

Dim ctl As Control
For Each ctl In Me.Controls
If Not TypeOf ctl Is Label Then 'omit labels
If IsNull(ctl) Then 'if no value
Dim strLabel As String
strLabel = "Me.lbl" & (ctl.Name)
strLabel.Visible = True does not work!
'Debug.Print (strLabel)
Else
End If
End If

Next ctl
End Sub

All of my labels have an lbl prefix. If the ctl is null, Id like
to concatenate the lbl in front of (ctl.name) and hide the label.
It seems to work (at least the name) in the immediate window.

I cant get Access to recognize the control, however.

Keep in mind that my labels are NOT linked to my textboxes et al. I
did this because I place the label on top of the textbox to save
space. If the textbox is empty, the label is displayed. If not, the
textbox value appears. This only seems to work when the label and
texbox are not linked.

Thanks,
alex
 
Reply With Quote
 
 
 
 
Dirk Goldgar
Guest
Posts: n/a
 
      16th Dec 2009
Try this:

'------ start of revised code ------
Private Sub Form_Current()

Dim ctl As Control

For Each ctl In Me.Controls
If Not TypeOf ctl Is Label Then 'omit labels
Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
End If

Next ctl

End Sub
'------ end of revised code ------

That should hide the label if the control is null, and hide the label if it
isn't.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)


"alex" <(E-Mail Removed)> wrote in message
news:5c84253e-ba3a-40a7-a376-(E-Mail Removed)...
Assign string to control

Hello,
Using Access 03

Im attempting to convert a string name to a control nameHeres the
code:

Private Sub Form_Current()

Dim ctl As Control
For Each ctl In Me.Controls
If Not TypeOf ctl Is Label Then 'omit labels
If IsNull(ctl) Then 'if no value
Dim strLabel As String
strLabel = "Me.lbl" & (ctl.Name)
strLabel.Visible = True does not work!
'Debug.Print (strLabel)
Else
End If
End If

Next ctl
End Sub

All of my labels have an lbl prefix. If the ctl is null, Id like
to concatenate the lbl in front of (ctl.name) and hide the label.
It seems to work (at least the name) in the immediate window.

I cant get Access to recognize the control, however.

Keep in mind that my labels are NOT linked to my textboxes et al. I
did this because I place the label on top of the textbox to save
space. If the textbox is empty, the label is displayed. If not, the
textbox value appears. This only seems to work when the label and
texbox are not linked.

Thanks,
alex



 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      16th Dec 2009
Try
strLabel = "lbl" & ctl.Name
Me.Controls(strLabel).Visible = True

You might run into errors since
== the control might be a line , box , button, etc and not have a value
property (Line: If IsNull(ctl) Then)
== the control might not have an associated label (Line, Box, button)
== If the control is a combobox or a list the value might be null, but you
might not want to hide the label.

Also, as you move from record to record you might want to show/hide the label
so you should have something that hides or shows the labels instead of just
showing the label.

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

alex wrote:
> Assign string to control
>
> Hello,
> Using Access 03
>
> Im attempting to convert a string name to a control nameHeres the
> code:
>
> Private Sub Form_Current()
>
> Dim ctl As Control
> For Each ctl In Me.Controls
> If Not TypeOf ctl Is Label Then 'omit labels
> If IsNull(ctl) Then 'if no value
> Dim strLabel As String
> strLabel = "Me.lbl" & (ctl.Name)
> strLabel.Visible = True does not work!
> 'Debug.Print (strLabel)
> Else
> End If
> End If
>
> Next ctl
> End Sub
>
> All of my labels have an lbl prefix. If the ctl is null, Id like
> to concatenate the lbl in front of (ctl.name) and hide the label.
> It seems to work (at least the name) in the immediate window.
>
> I cant get Access to recognize the control, however.
>
> Keep in mind that my labels are NOT linked to my textboxes et al. I
> did this because I place the label on top of the textbox to save
> space. If the textbox is empty, the label is displayed. If not, the
> textbox value appears. This only seems to work when the label and
> texbox are not linked.
>
> Thanks,
> alex

 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      16th Dec 2009

'------ start of revised code ------
Private Sub Form_Current()

Dim ctl As Control
'To ignore any errors that this code can generate
'you can add the following line to Dirk's code
On Error Resume Next

For Each ctl In Me.Controls
If Not TypeOf ctl Is Label Then 'omit labels
Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
End If

Next ctl

End Sub
'------ end of revised code ------


John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County

Dirk Goldgar wrote:
> Try this:
>
> '------ start of revised code ------
> Private Sub Form_Current()
>
> Dim ctl As Control
>
> For Each ctl In Me.Controls
> If Not TypeOf ctl Is Label Then 'omit labels
> Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
> End If
>
> Next ctl
>
> End Sub
> '------ end of revised code ------
>
> That should hide the label if the control is null, and hide the label if
> it isn't.
>

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      16th Dec 2009
"John Spencer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> '------ start of revised code ------
> Private Sub Form_Current()
>
> Dim ctl As Control
> 'To ignore any errors that this code can generate
> 'you can add the following line to Dirk's code
> On Error Resume Next
>
> For Each ctl In Me.Controls
> If Not TypeOf ctl Is Label Then 'omit labels
> Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
> End If
>
> Next ctl
>
> End Sub
> '------ end of revised code ------



Good idea, John.

Interestingly enough, I was concerned about what would happen in the case of
controls with no Value property, but I found that IsNull(ctl) returns False
for them and doesn't raise an error. I was surprised at that; my guess is
that they have a different default property. However, the code should still
guard against the possibility that there is no corresponding label, so the
"On Error Resume Next" statement is still recommended.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

 
Reply With Quote
 
Duane Hookom
Guest
Posts: n/a
 
      16th Dec 2009
I would select all of the controls that have a label and set their tag to
"lbl".
Then use code like:

Private Sub Form_Current()

Dim ctl As Control
Dim strLabel As String

For Each ctl In Me.Controls
If Instr(ctl.Tag,"lbl") > 0 Then
strLabel = "lbl" & (ctl.Name)
Me(strLabel).Visible = IsNull(ctl)
End If
Next ctl
End Sub

--
Duane Hookom
Microsoft Access MVP


"alex" wrote:

> Assign string to control
>
> Hello,
> Using Access ‘03
>
> I’m attempting to convert a string name to a control name…Here’s the
> code:
>
> Private Sub Form_Current()
>
> Dim ctl As Control
> For Each ctl In Me.Controls
> If Not TypeOf ctl Is Label Then 'omit labels
> If IsNull(ctl) Then 'if no value
> Dim strLabel As String
> strLabel = "Me.lbl" & (ctl.Name)
> strLabel.Visible = True ‘does not work!
> 'Debug.Print (strLabel)
> Else
> End If
> End If
>
> Next ctl
> End Sub
>
> All of my labels have an “lbl” prefix. If the ctl is null, I’d like
> to concatenate the “lbl” in front of (ctl.name) and hide the label.
> It seems to work (at least the name) in the immediate window.
>
> I can’t get Access to recognize the control, however.
>
> Keep in mind that my labels are NOT linked to my textboxes et al. I
> did this because I place the label on top of the textbox to save
> space. If the textbox is empty, the label is displayed. If not, the
> textbox value appears. This only seems to work when the label and
> texbox are not linked.
>
> Thanks,
> alex
> .
>

 
Reply With Quote
 
alex
Guest
Posts: n/a
 
      16th Dec 2009
On Dec 16, 12:24*pm, John Spencer <spen...@chpdm.edu> wrote:
> '------ start of revised code ------
> Private Sub Form_Current()
>
> * * Dim ctl As Control
> 'To ignore any errors that this code can generate
> 'you can add the following line to Dirk's code
> * * On Error Resume Next
>
> * * For Each ctl In Me.Controls
> * * * * If Not TypeOf ctl Is Label Then 'omit labels
> * * * * * * Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
> * * * * End If
>
> * * Next ctl
>
> End Sub
> '------ end of revised code ------
>
> John Spencer
> Access MVP 2002-2005, 2007-2009
> The Hilltop Institute
> University of Maryland Baltimore County
>
>
>
> Dirk Goldgar wrote:
> > Try this:

>
> > '------ start of revised code ------
> > Private Sub Form_Current()

>
> > * *Dim ctl As Control

>
> > * *For Each ctl In Me.Controls
> > * * * *If Not TypeOf ctl Is Label Then 'omit labels
> > * * * * * *Me.Controls("lbl" & ctl.Name).Visible = Not IsNull(ctl)
> > * * * *End If

>
> > * *Next ctl

>
> > End Sub
> > '------ end of revised code ------

>
> > That should hide the label if the control is null, and hide the label if
> > it isn't.- Hide quoted text -

>
> - Show quoted text -


Gentlemen,
The code seems to work well...
John,
You're right; I had to add the error handling because not all the
controls have the associated labels (yet!). The code was looking for
them. I should probably also change the code to look for only text or
combo boxes.
Thanks again,
alex
 
Reply With Quote
 
David W. Fenton
Guest
Posts: n/a
 
      17th Dec 2009
"Dirk Goldgar" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Try this:
>
> '------ start of revised code ------
> Private Sub Form_Current()
>
> Dim ctl As Control
>
> For Each ctl In Me.Controls
> If Not TypeOf ctl Is Label Then 'omit labels
> Me.Controls("lbl" & ctl.Name).Visible = Not
> IsNull(ctl)
> End If
>
> Next ctl
>
> End Sub
> '------ end of revised code ------
>
> That should hide the label if the control is null, and hide the
> label if it isn't.


If you're trying to hide the label of a Null control, why not use
ctl.Controls(0).Visible, instead of depending on a naming
convention? I never name my labels anything other than the gibberish
default name, and access their properties via the control they are
attached to.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
 
Reply With Quote
 
David W. Fenton
Guest
Posts: n/a
 
      17th Dec 2009
"Dirk Goldgar" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> However, the code should still
> guard against the possibility that there is no corresponding
> label, so the "On Error Resume Next" statement is still
> recommended.


For what it's worth, I would dissent on that. I just don't use On
Error Resume Next -- if I know an error can occur, I trap for the
specific errors I expect and ignore them. This allows an unexpected
error to be properly handled by my error handler.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
 
Reply With Quote
 
David W. Fenton
Guest
Posts: n/a
 
      17th Dec 2009
John Spencer <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Also, as you move from record to record you might want to
> show/hide the label so you should have something that hides or
> shows the labels instead of just showing the label


If you're doing this in the form's OnCurrent vent, I'd recommend a
custom collection for this, rather than walking the entire controls
collection of the form. In that case, you'd populate the collection
in the form's OnLoad, and then walk the collection in the OnCurrent
to set whatever you wanted to set. This would mean that you can use
the tag property, or whatever code conditions you want to pick out
the controls that you want in your collection that you're operating
on in the OnCurrent. Also, it makes it easy to set up multiple
collections for different purposes so that your conditional logic
can be much simpler.

It's also noticeably faster, even on forms with <100 controls. I was
surprised at this, but apparently, walking the controls collection
is not as efficient as looping a custom collection of smaller size.

If anyone's interested, I can repost sample code on how to
accomplish this.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assign Values to String Value =?Utf-8?B?UEMtUmVuZXc=?= Microsoft Excel Discussion 1 15th Sep 2006 02:43 AM
Cannot assign varible to SQL string =?Utf-8?B?QnJ1Y2U=?= Microsoft Access VBA Modules 1 9th Feb 2005 06:46 AM
How can I assign a number to a string? =?Utf-8?B?Sm9nIERpYWw=?= Microsoft Excel Misc 3 14th Jan 2005 03:44 AM
Assign result to string Ben Microsoft Access VBA Modules 2 17th Nov 2003 05:26 PM
Re: Create a formula into a String then assign string to a cell Myrna Larson Microsoft Excel Programming 6 23rd Aug 2003 09:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:54 PM.