PDA

View Full Version : vba keyboard delete key help



gheepok
10-06-2010, 06:08 PM
i want to create a button that have delete key action.
this Application.Screen.PreviousControl = "" works on main form but does not works on subform.
please someone help.

Imdabaum
10-07-2010, 12:39 PM
http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx

Can I ask what you are trying to do with this? Seems like a lot of work to delete a control. Take into account that you won't be able to delete a control unless you are in design mode, and if you are in design mode, you won't be able to use the button that acts as your delete key. You might be using it in some other fantastic way that gets around that, but there's my two cents.

*For those who don't like reading MSDN articles the answer is {DEL}

I'm just basing the answer to the topic though. I'm not sure what the actual post is trying to accomplish. Clarification might be needed.

gheepok
10-07-2010, 08:10 PM
a clear button thats can use in main form and subform.

what i have here is the clear button can clear a seleted field on main form but can not clear on subform.

can someone help i.

thanks

hansup
10-08-2010, 07:25 AM
a clear button thats can use in main form and subform.

what i have here is the clear button can clear a seleted field on main form but can not clear on subform.
You will have to give us more detailed information about what you want your clear button to do.

You can reference a control for the subform current record like this:
Debug.Print Me!f_user!f_name
So, if you wanted to replace that control's value with an empty string, you can do this:
Me!f_user!f_name = ""
But, if you click in a field on the subform, then click your clear button, you will get an error from this line:
Application.Screen.PreviousControl = ""
I don't understand what your clear button should do.

Imdabaum
10-08-2010, 08:19 AM
a clear button thats can use in main form and subform.

what i have here is the clear button can clear a seleted field on main form but can not clear on subform.

can someone help i.

thanks

I'm looking at your sample.. Do you really want to be killing the ID? Or is this just a result of quick sample draw up?

Are you wanting a clear button or a delete button? Or an undo button to clear the data that you entered without saving it to the recordset?

gheepok
10-09-2010, 09:25 AM
i want a clear button that clear a selected field in the f_asset and f_user.

me!f_user!f_name only clear f_name field but not the selected field in the main form and sub form.

hansup
10-10-2010, 10:01 AM
If you click in a text box on the subform, then click your "clear" button, Application.Screen.PreviousControl will refer to the subform control ... not a text box on the main form.

I don't see how to get around that problem easily. The best I can offer is to store the name of the main form text box which was last visited. At the top of your module, try this:Option Compare Database
Option Explicit

Dim strLastVisited As String
Then, in the Got Focus event for each of the main form text box controls, you can store the control's name to strLastVisited.Private Sub id_GotFocus()
strLastVisited = Me.ActiveControl.Name
End Sub
Private Sub location_GotFocus()
strLastVisited = Me.ActiveControl.Name
End Sub
Private Sub type_GotFocus()
strLastVisited = Me.ActiveControl.Name
End SubThen try this code for your "clear" button's Click event:Private Sub Command7_Click()
Me.Controls(strLastVisited).Value = ""
Me!f_user.SetFocus
Application.Screen.ActiveControl.Value = ""
End Sub

gheepok
10-10-2010, 08:38 PM
Private Sub Command7_Click()

If Me!f_asset = Selected Then 'f_asset is the main form
Me!f_asset.SetFocus
Application.Screen.ActiveControl.Value = ""

ElseIf Me!f_user = Selected Then 'f_user is the sub form
Me!f_user.SetFocus
Application.Screen.ActiveControl.Value = ""


End If
End Sub

can someone help i with the syntax. i want a if else condition.

gheepok
10-11-2010, 11:35 PM
thanks hansup.

but it still need some fix because sometimes can clear 2 field.

hansup
10-12-2010, 05:52 AM
I don't understand. Here is what you told us:

i want a clear button that clear a selected field in the f_asset and f_user.

Seems to me that is what the current version of your form does.

gheepok
10-12-2010, 09:02 PM
hansup

when click clear on the selected textbox on the main form

the selected text box will be cleared.


when click clear on the selected text box on the sub form

the selected text box will be cleared.


the code will clear the seleced text box on main form and the not selected field on the sub form.

example

field location selected then click clear. the location clear but the id f_user also clear.

i want only location clear and not the id f_user clear.

when field company selected then click clear. the company clear and the type f_asset also clear.

i want only company clear.

any suggestion hansup?

hansup
10-12-2010, 09:31 PM
I have no more suggestions to offer, gheepok. Hopefully someone else can figure out how to help you. Good luck.

Imdabaum
10-13-2010, 06:51 AM
hansup
when click clear on the selected textbox on the main form
the selected text box will be cleared.
when click clear on the selected text box on the sub form
the selected text box will be cleared.
the code will clear the seleced text box on main form and the not selected field on the sub form.
example
field location selected then click clear. the location clear but the id f_user also clear.
i want only location clear and not the id f_user clear.
when field company selected then click clear. the company clear and the type f_asset also clear.
i want only company clear.
any suggestion hansup?

I think something is being lost in translation here. I thought hansup was on the right track so I stopped following.

So If fieldLocation is Selected and you click the clear button, you only want the fieldLocation on the main form to be cleared.

If fieldCompany is selected and you click clear, you only want the fieldCompany to clear?

So only one field will be cleared on a single Click_Event and that field will be the last one that was Active?