how to know which command button is clicked

ceder

New member
Local time
Today, 05:11
Joined
May 2, 2012
Messages
6
hi,

i have two command buttons for active calendar, i want to put the calendar beside the clicked command button in the form named entry. how can i know which button is clicked?
i try the following code to identify if the command button command138 is clicked, does this way is right?

Dim c As CommandButton
If c = Formsentry("Command138") Then....

the second code Formsentry("Command138") is always not right, i try Form("sentry").Command138...how to correct it?

or do you have some good idea to put the calendar beside a clicked command button?
thank you

 
Almost :) it would be Forms("sentry").Command138 unless the code is in the sentry form in which case Me.Command138

You could do the comparison this way I think (the clicked button will be the active control):

If Forms("sentry").ActiveControl.Name = Command138.Name Then

or if the codes in the sentry form's module:

If Me.ActiveControl.Name = Command138.Name Then

Note: You can't do a comparison between two objects in VB or VBA. In VB you can use the equals function (if object1.equals(object2) then) but there's no such function in VBA. The only way I know of is to compare their names.
 
Last edited:
thank you for your help

the code is not the entry form, it is in a module. i try the following code:

If Forms("entry").ActiveControl.Name = Command138.Name Then

and run, command138 is highlight and a message pop up "compile error variable not defined". i do not know where is the problem?
 
Hi,

compile error variable not defined
Usually you have to declare variables. for example, i want to save a form name as a string

Code:
Dim frmMyForm As String
frmMyForm = "frmMyFormName"
 
DoCmd.OpenForm frmMyForm

without checking, i suspect your issue is the missing ' ! '

when calling forms in Access, its required-

Code:
[I]If Forms[COLOR=red]![/COLOR]("entry").ActiveControl.Name = [COLOR=navy]Command138[/COLOR].Name Then[/I]


hope that helps :),



Nidge
 
For a Start please rename Command138 to something more descriptive. Like cmdDoThis

code is not the entry form, it is in a module. i try the following code:

Do you mean a Class Module? If so move it to the Module behind the Form unless you have reasons not to.

Finally Compile and if it fails let's know the problem.
 
thank you
where should i put the declare variables?
i put it in the module under
Public Function CalendarFor(....)
with:
Dim MyForm As String
MyForm = "entry"

DoCmd.OpenForm MyForm


and run, the same msg "...variable not defined"
 
Hi,

i think you should either-

post the full code so we can see it all and not make us second guess the rest

or

post a db with the code in.

its difficult to assume what else is going on and people will lose interest quite quickly if the info is sporadic, trust me :)

we are here to help but in the same token, we know what we are helping with


Nidge
 
MyForm = "entry"

These are exactly the same thing. You can use either with the same result.
 
it is the codes in a module, i want to move frmCalendar in its procedure with lL, lT so that frmCalendar is around the clicked calendar button command138 or cmdcaldate. so i want to defined lL, lT according which button is clicked.
...
Public gtxtCalTarget As TextBox 'Text box to return the date from the calendar to.
Public lL, lT As Long

Public Function CalendarFor(txt As TextBox, Optional strTitle As String)

If Forms("entry").ActiveControl.Name = Command138 Then
lL = 0
lL = Forms("entry").Command138.Left - Forms("entry").Command138.Width
lT = 0
lT = Forms("entry").Command138.Top + Forms("entry").Command138.Height
End If


If Forms("entry").ActiveControl.Name = cmdCalDate Then
lL = 0
lL = Forms("entry").cmdCalDate.Left + Forms("entry").cmdCalDate.Width
lT = 0
lT = Forms("entry").cmdCalDate.Top + Forms("entry").cmdCalDate.Height
End If

On Error GoTo Err_Handler
'Purpose: Open the calendar form, identifying the text box to return the date to.
'Arguments: txt = the text box to return the date to.
' strTitle = the caption for the calendar form (passed in OpenArgs).

Set gtxtCalTarget = txt
DoCmd.OpenForm "frmCalendar", windowmode:=acDialog, OpenArgs:=strTitle

Exit_Handler:
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & " - " & Err.Description, vbExclamation, "CalendarFor()"
Resume Exit_Handler
End Function

...

sometimes, when i change somethings in If Forms("entry").ActiveControl.Name = Command138 Then, progame is blocked, msg "you can't exit microsoft access now. if you're running a vb module that is using ole or dde, you may need to interrupt the module"
 
My mistake in the first version. It should be:

Code:
If Forms("sentry").ActiveControl.Name = Forms("sentry").Command138.Name Then

BTW: That definitely doesn't need a bang (!) after forms. That alternate syntax would be:

Code:
If Forms!sentry.ActiveControl.Name = Forms!sentry.Command138.Name Then

Which is the other way to refer to a form. Both those examples do the same thing and should work.
 
And if you're going to be constantly referring to that form and its controls put everything in a with Forms!sentry block:

Code:
With Forms!sentry
    If .ActiveControl.Name = .Command138.Name Then

    End If
End With
 
thanks, i tried
With Forms!sentry
If .ActiveControl.Name = .Command138.Name Then

End If
End With

and
If Forms("sentry").ActiveControl.Name = Forms("sentry").Command138.Name Thenbut both blocked the program, so i have to turn off my computer. there is a msg "you can't exit microsoft access now. if you're running a vb module that is using ole or dde, you may need to interrupt the module"

i have to search ole or dde...
 
Definitely put the code in the entry form's module.

Or if it must be a generic function that does the same thing for various different forms then pass the controls by reference to the function.

But your function is not generic as it refers to the entry form specifically.

But the error with the code I gave now is that I was still calling the form sentry, not entry.
 
i don't know is it my module is class or not. i don't understand
Definitely put the code in the entry form's module

i put the wrong place?
i put =CalendarFor([...]."") next to on click of the cmdbtn to active module.
the module start with

Option Compare Database

Option Explicit

i have to study
 
i don't know is it my module is class or not. i don't understand

When you don't understand ask. No one here will mind explaining more. It is what we do.

If you open your Code you should see some code behind the various forms. Things like Close, Print or Open another Form or a Report. These are your Form Modules and Report Modules.

Then there are Class Modules. There you will find Code that is called by More than the one Form or Report. This is also where you store user defined functions. UDF.

In your situation you need to refer to the Form's Controls etc. So put the Code behind behind the form. Then instead of refering to the Form simply use Me.

eg Me.ControlName.Visible False

If you called this Code from Various Forms, Then use a Class Module. But in calling the Function or Procedure you will need to pass the Form's Name.

eg CallClassModule Me

This would call CallClassModule and Pass the Name of the Form.

Hope this Helps.
 

Users who are viewing this thread

Back
Top Bottom