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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Focus on Subform

Status
Not open for further replies.

pavczech

Technical User
Aug 22, 2012
6
GB
Hi
I have form and subform. I am using the on-screen keyboard and it works well on the form but doesn't on the subform. It Lost the focus or the object doesn't support this property or method.

Private Function TypeAlphaNum(strKey As String) As String
Screen.PreviousControl.SetFocus
Me.Controls(Screen.ActiveControl.Name).SelStart = Nz(Len(Me.Controls(Screen.ActiveControl.Name)), 0)
Me.Controls(Screen.ActiveControl.Name).SelLength = 0
Me.Controls(Screen.ActiveControl.Name).Value = Me.Controls(Screen.ActiveControl.Name).Value & strKey
End Function

Could somebody help. Thanks
 
Just curious here - why are you doing this? What you're trying to do is capture the current fields name... any reason? And any reason so complex for something so simple?

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Hi
My knowledge of VBA is not that good. I am reading the book about it and it is getting better but still.
Anyway it is for on-screen keyboard. I am not sure how complex or simple is it. If I could replace the first part of code for something simpler and it would work on subform too that would be great.


Private Function TypeAlphaNum(strKey As String) As String
Screen.PreviousControl.SetFocus
Me.Controls(Screen.ActiveControl.Name).SelStart = Nz(Len(Me.Controls(Screen.ActiveControl.Name)), 0)
Me.Controls(Screen.ActiveControl.Name).SelLength = 0
Me.Controls(Screen.ActiveControl.Name).Value = Me.Controls(Screen.ActiveControl.Name).Value & strKey
End Function
Private Sub Command1_Click()
TypeAlphaNum "1"
End Sub
Private Sub CommandSPACE_Click()
TypeAlphaNum " "
End Sub
Private Sub CommandBSPACE_Click()
Screen.PreviousControl.SetFocus
Me.Controls(Screen.ActiveControl.Name).SelStart = Nz(Len(Me.Controls(Screen.ActiveControl.Name)), 0)
Me.Controls(Screen.ActiveControl.Name).SelLength = 0
Me.Controls(Screen.ActiveControl.Name).Value = Left(Me.Controls(Screen.ActiveControl.Name).Value, Len(Me.Controls(Screen.ActiveControl.Name)) - 1)
End Sub


Thanks
 
You do realize that if you need an onboard keyboard, one is built into Windows, right? Depending upon your version, how you get to it may be slightly different. But for Vista or 7, you could simply open the start menu and type keyboard and it will come right up.

The reason I ask is because to me, this sounds more like a homework assignment than anything, and the current school year (in the US) has just started, or is just about to start depending upon district/institution.

Now, assuming this is not homework...

Please verify... are you saying you are USING the system/built-in onscreen keyboard, or that you are for some reason needing to BUILD one yourself? I can see the benefit in doing it for practice, but I am just wondering about practicality of duplicating a built-in system functionality.

So with the code you've posted, what have you tried? What isn't working? What errors are you receiving? Please detail what you are doing, what you are expecting, and what you are getting.... in other words, what's the difference between what you EXPECT to happen, and what is actually happening? Also, if it just isn't working right, have you tried stepping through the code?

How to step through code: Move your mouse cursor within the procedure... well, in this case, you'd need to call the function from a button or another procedure. Since yours is a function, and you're actually building the keyboard, I guess, then you'd want to run this off the button click. So behind every button on your form, you'd call the same function like this:

First of all, I'd imagine it best to use a public variable for Form and Control like this:
Code:
Public frm As Form
Public ctl As Control

Then build your function to do your deal:
Code:
Private Function TypeAlphaNum() As String
     Set Form = Forms(Form.Name)
     Set ctl = frm.ActiveControl
     Dim strKey As String ' moved this out of the calling function,
                          ' b/c if you use ActiveControl, you won't need to pull in that name... 
                          ' assuming the function can reside within the form.
     strKey = ctl.Name

     'Do the rest of the function stuff here
End Sub

Then call it from each button:
Code:
Private Sub Button1_Click()
     Call YourFunction
'Or if you really are returning a value - which I don't see that you are, so it probably should be a procedure anyway
     Msgbox YourFunction
End Sub

Private Sub MyButton_Click()
     Call YourFunction
'Or if you really are returning a value - which I don't see that you are, so it probably should be a procedure anyway
     Msgbox YourFunction
End Sub

In the end, the current code, as it is, is far too complicated, I think, if you're just wanting to capture the current control, and do something with it. I think I saw something in there about the previous control. What do you want to do with the previous control?

So really it boils down to this:
1. What do you want to do? - what do you want the form/controls to do?
2. What has the current code done? Has it worked at all? If so, what happened? Was that expected/desired?

If you just copied and pasted the code from a book (or typed from reading a book) or pasted from online somewhere, and just ran it without applying it to your specific situation, then you're NOT likely to get the results you expect.




"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Hi ya thanks for that.
Dont have much time at the moment as my wife is just about to give birth ( we are actually on maternity ward ) shh! don't tell my wife i am doing this.
So it is not school work but i feel like been at school. I am learning bit of the vba from books and as reading through few forums, but mostly just copy and paste and amend a bit but mostly it is hit and miss! Anyway i know about build in keyboard in windows but the keys are too small and there are some keys which i wouldn't have use for so just trying to build up one.
I have form with buttons which are working as a keys. There is a click_function which enters the symbol in to the field. This all works fine. There is a subform on this form which when i click on the field i would like to press the button on main form and it would insert the symbol. Instead of that it brings up message: object doesn't support this property or method.
Any help. I will try the code you have put there when i get the chance :)
Thanks for now
 
Wow, well we hope the birth goes well. And CERTAINLY don't let learning VBA get in the way of taking care of your wife and new baby. That should be your #1 priority now. Just post back here later when you're ready - even if that's week(s) from now. [thumbsup2]

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Hi
well still pregnant, even though we were in second stage of labour. Means I have bit of time, well you never know when could it happend.
Back to the subject. I have tried amending the code but it came up with message Compile error: Wrong number of arguments or invalid property assignment.
Not sure what that means so here is what i have done:

Option Compare Database

Public frm As Form
Public ctl As Control

Private Function TypeAlphaNum() As String
Set Form = Forms(FormA)
Set ctl = frm.ActiveControl
Dim strKey As String
strKey = ctl.Name
End Function
Private Sub Command1_Click()
TypeAlphaNum "1"
End Sub
Private Sub Command2_Click()
TypeAlphaNum "2"
End Sub
Private Sub Command3_Click()
TypeAlphaNum "3"
End Sub
Private Sub Command4_Click()
TypeAlphaNum "4"
End Sub

Can enybody see what is wrong with this?
Thanks
 
Here's one spot (in [red]red[/red])... with correction below it in [blue]blue[/blue]
Code:
Option Compare Database

Public frm As Form
Public ctl As Control

Private Function TypeAlphaNum() As String
[b]Set Form = Forms([red]FormA[/red])[/b] [green]'here, you're missing the quotes around FormA[/green]
Set ctl = frm.ActiveControl
Dim strKey As String
strKey = ctl.Name
End Function
Private Sub Command1_Click()
TypeAlphaNum "1"
End Sub
Private Sub Command2_Click()
TypeAlphaNum "2"
End Sub
Private Sub Command3_Click()
TypeAlphaNum "3"
End Sub
Private Sub Command4_Click()
TypeAlphaNum "4"
End Sub

----- to -----

Code:
Option Compare Database

Public frm As Form
Public ctl As Control

Private Function TypeAlphaNum() As String
[b]Set Form = Forms([blue]"FormA"[/blue])[/b] [green]'here, you're missing the quotes around FormA[/green]
Set ctl = frm.ActiveControl
Dim strKey As String
strKey = ctl.Name
End Function
Private Sub Command1_Click()
TypeAlphaNum "1"
End Sub
Private Sub Command2_Click()
TypeAlphaNum "2"
End Sub
Private Sub Command3_Click()
TypeAlphaNum "3"
End Sub
Private Sub Command4_Click()
TypeAlphaNum "4"
End Sub

There may be some other errors in there, but that was the first that jumped out at me.



"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Hi
I have already tried that one. I have downloaded a sample of the db. If you would be so kind and have a look. Could you tell me if you find what is wrong. By the way my son have arrived today so I have spent most of the day at maternity ward today and going back there first thing in the morning. ( only a week late )
Thanks very much for your help.
 
 http://www.mediafire.com/?5vwu2alfp5r0dwu
Glad to hear all went well with your new baby boy.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Sorry
Did you have a chance to have a look on my sample why is it still not working?
Thanks
 
No, sorry. I didn't get the time I was hoping to have to look at this and one other actually. I wound up wasting too much time trying to remotely help someone with their computer... I say wasting, b/c we're going to have to go a totally different route on it after all... we were so close to getting it fixed, to, so I really hate that one. Oh well.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top