Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command Button Selects Last Label Clicked

Status
Not open for further replies.

Drummermoose

Technical User
Jan 22, 2002
48
GB
I have a form with labels which you can double-click to access other forms. Is there a way so that I can have a button so that someone can single-click on a label and then click on the button to link to the form the label goes to on double-click.

I'm using Access 97

Thanks,

Ben
 
One trick I used in a previous life was to create some invisible (Tranparent) buttons that were layered throughout the form. Not sure if that was what u were looking for or not. Steve Medvid
"IT Consultant & Web Master"
 
Thanks for the suggestion, but I don't think it will work.

I'm looking on the lines of using one button to link to a number of forms, depending on which label I select.

Any More Ideas!

Ben
 
Option Compare Database
Option Explicit
Dim lcText1 As String

Private Sub Command1_Click()
MsgBox (lcText1)
End Sub

Private Sub Test1_Click()
lcText1 = Me.Test1.Caption
End Sub

Private Sub Test2_Click()
lcText1 = Me.Test2.Caption
End Sub

In the click of lbl fields, I would change the background color so they can see what they selected. Also, need to reset all other lbl fields to default color, so only one item, the current one is a different color.

htwh

Steve Medvid
"IT Consultant & Web Master"
 
There are a couple ways you could do it.

1. Set up a global variable (i.e. mstrLabelSelected)
2. On the OnClick event of the label call a function that sets mstrLabelSelected equal to the name of the label.
3. I would give the name of the label the same name as the form (except for the prefix). For example, "lblForm01" is used to launch "frmForm01". You could also include, in the Tag property, the name of the form you want launched when the label is selected.
4. On the Onclick event of the button, open the form specified by the label name (or Tag property) of the label referenced by mstrLabelSelected.

Properties of Label:
Name....... lblForm01
OnClick ... =LabelSelected([lblForm01])
Tag ....... FormName=Form01 (optional)

The code below assumes 1st option. That is, the name of the label is also the name of the form.

Function LabelSelected(lblSelected as Label)
mstrLabelSelected = lblSelected.Name
End Function

Sub Button_OnClick()
DoCmd.OpenForm "frm" & mid(mstrLabelSelected,4)
End Sub

Another idea would be to change the color of the label when it is selected. Then when the user selects the button you would have to loop thru the labels to determine which one has the specific color. Then open the form assigned to it. This method is a little more complicated because you would have to change the color back when another label is selected.
 
Thanks for the help, but I haven't sorted it yet.

Steve, your codes works, I think as it should do, by bringing up a message box saying what label has been selected, but I couldn't get it to link to the form in the label's double-click.

FancyPrairie, I can't rename all labels the same name as the forms because I have a label to open up a form in add mode and another to open it up in edit mode.

In total I have 5 labels. 2 linked to one form, 2 to another and 1 linking to a form with a list of reports to print.

I sorted the colour thing no problem. The label selected goes to a shade of yellow (10092543) and the others stay or go white (16777215).

Any more ideas...

Thanks for the help so far.

Ben
 
1. You can still use the naming convention I suggested.
lblAdd_YourFormName
lblEdt_YourFormName

Then when you open the form, do the following
DoCmd.OpenForm mid(mstrLabelSelected,8) 'Strips off "lblAdd_" or "lblEdt_"

This would work nice for looping thru the controls to toggle the colors on/off. Because you only have to look at controls whose name begins with "lblAdd_" or "lblEdt_".

2. The other way you could do this is to set the tag property of the label equal to the name of the form you want to open.

Label1.Tag = YourFormName

Docmd.OpenForm Label1.Tag
 
Um, I think Steve was just trying to show you how to capture which label was clicked by the user. The msgbox is just a visual confirmation that you're grabbing the right value, you won't need it in your finished code.

To expand on Steve's code, you would need to create an if or case statement like this:

Private Sub Command1_Click()
if lcText1 = "form 1" then
docmd.openform "form1" 'or whatever code you have right now on the double click event


else ... (etc)

End Sub

Steve's idea of changing the label colors is a good one, although I think radio buttons might be a more intuitive & familiar way to do this task overall.

Anyhoo, hope this helps & have fun.
Fuyo
 
The way I understood the initial question was that Ben wanted the user to select the label, and then select a command button which would launch the form indicated by the label that was previously selected.

In addition, if the user double-clicked on the label it would also launch the form.

If so, he would need to store the label that was selected (via the OnClick event of the label) so when the command button was selected he would know what label was selected. He would also know which label was selected by its color.

By using a naming convention for the labels or storing the form name in the Tag property would eliminate the extra code required to launch the appropriate form (i.e bunch of If...Then or Select Case statements).
 
The code below should handle the whole thing. It highlights the label selected and unhighlights the previous label selected. The user can double-click on the label to launch the form or select a command button to launch the form.

The OnClick event of each label should equal:
=Highlight([NameOfLabel]) (Where NameOfLabel is the name of the label calling the function)

The OnDoubleClick event of each label along with the OnClick event of the command button should equal
=OpenMyForm()

The Tag property of each label contains the name of the form to be opened.

=========================================================
Code:
Option Compare Database
Option Explicit

    Dim mstrLabelName As String      'String to hold name of label selected
Function Highlight(lbl As Label)
Code:
'************************************************************************************
'*  1. If "mstrLabelName" = vbNullString, then nothing selected yet.
'*     If &quot;mstrLabelName&quot; <> vbNullString, then the label referenced by &quot;mstrLabelName&quot;
'*        is highlighted.  Therefore, unhighlight it.
'*  2. Set &quot;mstrLabelName&quot; = name of label selected
'*  3. Change the foreground color of the item selected (i.e. highlight it).
'****************************************************************************************

    If (mstrLabelName <> vbNullString) Then Me(mstrLabelName).ForeColor = 0  'black
    mstrLabelName = lbl.Name
    Me(mstrLabelName).ForeColor = 255   'red
    
End Function
Function OpenMyForm()
Code:
'**********************************************************
'*  Open the form based on the label that is highlighted  *
'**********************************************************

    DoCmd.OpenForm Me(mstrLabelName).Tag
    
End Function
Private Sub Form_Open(Cancel As Integer)
Code:
'******************************
'*  Initialize mstrLabelName  *
'******************************

    mstrLabelName = vbNullString
    
End Sub
 
By the way, the Enable property of the command button should be set to false in design view. It should only be set to true when mstrLabelName is <> vbNullString.
 
Thanks to everyone for the help!

Here's what I did, just for reference.

In the on_click event of every label I had

lcText1 = &quot;Labelname&quot;

In the on_click event of the command button I had

If lcText1 = &quot;Labelname1&quot; Then
DoCmd.OpenForm &quot;Form1&quot;, , , , acFormEdit

ElseIf lcText1 = &quot;Labelname2&quot; Then
DoCmd.OpenForm &quot;Form2&quot;, , , , acFormAdd

ElseIf lcText1 = &quot;Labelname3&quot; Then
DoCmd.OpenForm &quot;Form3&quot;, , , , acFormAdd

ElseIf lcText1 = &quot;Labelname4&quot; Then
DoCmd.OpenForm &quot;Form2&quot;, , , , acFormEdit

ElseIf lcText1 = &quot;Labelname5&quot; Then
DoCmd.OpenForm &quot;Form3&quot;, , , , acFormEdit

End
End If
End Sub


This works fine for me. I hope it is of some use to someone in the future.

Cheers,

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top