By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,510 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,510 IT Pros & Developers. It's quick & easy.

text box event in a DAP

P: 76
This is done in an Access 2003 ADO data access page.

I've got a text box (txtSearch) and I've got a command button (cmdSearch).

The cmdSearch has this onclick event:
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=vbscript event=onclick for=cmdSearch>
  2. <!--
  3. Dim rs
  4. Set rs = MSODSC.DataPages(0).Recordset.Clone
  5. On error resume next
  6.  
  7. rs.find "[WO NUMBER]=" & (txtSearch.value)
  8.  
  9. If (err.number <> 0) Then
  10.     Msgbox "Error: " & err.number & " " & err.description,,"Invalid Search"
  11.     Exit Sub
  12. End If
  13. cmdWOSearch.focus
  14.  
  15. If (rs.bof) or (rs.eof) Then
  16.     Msgbox "Error Work Order not found.",,"Search Done"
  17.     Exit Sub
  18. End If
  19. MSODSC.DataPages(0).Recordset.Bookmark = rs.Bookmark
  20. cmdWOSearch.focus
  21. -->
  22. </SCRIPT>
When a user enters text into txtSearch and hits enter I would like to call the cmdSearch onclick event. As of now, all the enter key does inside the txtSearch box is create a new line.
Really this is three questions.
How to catch the enter key when it is pressed in the txtSearch box.
How to call an event from another event.
Are data access pages good to use?
Oct 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: 76
So I figured out question number two on my own. You can interact with other elements in the page like so. cmdSearch.focus or cmdSearch.click or cmdSearch.dblclick, pretty cool.

I'm not sure why this won't work. It may have something to do with ascii code in VBscript?
So I try this.
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=vbscript event=onkeypress for=txtSearch>
  2. <!--
  3. dim KeyAscii
  4. KeyAscii = Asc(UCase(Chr(KeyAscii)))
  5. If KeyAscii = VbKeyEnter then
  6. cmdSearch.click
  7. End if
  8. -->
  9. </SCRIPT>
So for every key I press while txtSearch has focus this event will run. The problem is that this will catch everything. It won't weed out the return/newline/013/enter key that is pressed.
I thought maybe the page didn't like the VbKeyEnter criteria so I tried this
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=vbscript event=onkeypress for=txtSearch>
  2. <!--
  3. dim KeyAscii
  4. KeyAscii = Asc(UCase(Chr(KeyAscii)))
  5. If KeyAscii = Asc(013) then ' I also tried chr(13) and chr(13) & chr(10)
  6. cmdSearch.click
  7. End if
  8. -->
  9. </SCRIPT>
This doesn't catch anything at all. The only thing I'm catching is a cold. Curse you fall season.

I'm not clear on how the event: onkeypress works I guess. I declare KeyAscii and use it to test for the chr(13) but how does the keypressed by the user get to be equal to KeyAscii? onkeypress is simply an event does it pass anything to the code? From what I'm reading about this Access may be eating my enter keypress.
Oct 15 '07 #2

P: 76
It has to be eating it. This code should work.
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=vbscript event=onchange for=txtSearch>
  2. <!--
  3. dim searchstr
  4. searchstr = txtSearch.onkeypress
  5. Dim enter
  6. enter = chr(13)
  7. If searchstr = enter then
  8. msgbox (enter) & "there it is",,"OK"
  9. cmdSearch.click
  10. Exit Sub
  11. End If
  12. -->
  13. </SCRIPT>
I've read up about this problem in forms when the command button's property .default is set to true. How can I change this in a vbscript with MSODSC.

Thanks and I appreciate any light you may shed,
James
Oct 15 '07 #3

ADezii
Expert 5K+
P: 8,601
This is done in an Access 2003 ADO data access page.

I've got a text box (txtSearch) and I've got a command button (cmdSearch).

The cmdSearch has this onclick event:
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language=vbscript event=onclick for=cmdSearch>
  2. <!--
  3. Dim rs
  4. Set rs = MSODSC.DataPages(0).Recordset.Clone
  5. On error resume next
  6.  
  7. rs.find "[WO NUMBER]=" & (txtSearch.value)
  8.  
  9. If (err.number <> 0) Then
  10.     Msgbox "Error: " & err.number & " " & err.description,,"Invalid Search"
  11.     Exit Sub
  12. End If
  13. cmdWOSearch.focus
  14.  
  15. If (rs.bof) or (rs.eof) Then
  16.     Msgbox "Error Work Order not found.",,"Search Done"
  17.     Exit Sub
  18. End If
  19. MSODSC.DataPages(0).Recordset.Bookmark = rs.Bookmark
  20. cmdWOSearch.focus
  21. -->
  22. </SCRIPT>
When a user enters text into txtSearch and hits enter I would like to call the cmdSearch onclick event. As of now, all the enter key does inside the txtSearch box is create a new line.
Really this is three questions.
How to catch the enter key when it is pressed in the txtSearch box.
How to call an event from another event.
Are data access pages good to use?
  1. The ENTER Key is probably not being recognized in the KeyPress() Event because you have the Move after enter Option set to Next Field (Tools ==> Options ==> Move after enter ==> Next Field).
  2. All is not lost since you can trap the ENTER Key in the KeyDown() Event of [txtSearch], via the return value in the KeyCode Argument, then execute the Click() Event of cmdSearch as such:

    Expand|Select|Wrap|Line Numbers
    1. Private Sub txtSearch_KeyDown(KeyCode As Integer, Shift As Integer)
    2.   Const conEnterKey = 13
    3.  
    4.   If KeyCode = conEnterKey Then
    5.     Call cmdSearch_Click
    6.   End If
    7. End Sub
Oct 16 '07 #4

P: 76
That would work great if it were in a form but this is a data access page.
Microsoft Says:
"VBScript is designed to be a small and lightweight interpreted language. It is also designed to be safe, so it does not include direct access to the underlying operating system. VBScript syntax and usage is similar to VBA, except that the following features and keywords are omitted:

Language elements such as: #Const, #If...Then...Else, CVar, CVDate, Date, Declare (for declaring DLLs), Debug.Print, DoEvents, End, Erl, Error, GoSub...Return, GoTo, Like, LinkExecute, LinkPoke, LinkRequest, LinkSend, LSet, Mid, On Error GoTo, On...GoSub, On...GoTo, Optional, Option Base, Option Compare, Option Private Module, ParamArray, Resume, Resume Next, RSet, Static, Stop, Str, StrConv, Time, Type...End Type, TypeOf, and Val"

I wonder if I could create a function that effects the page globally. The default event for pressing the enter key while a textbox has focus is to create a new line. This is being caught by something else. I just need to turn "it" off. What ever "it" is. It is so easy to do in a form with the on enter event, but alas there is no such animal in the DAP world of VBScript.
Oct 16 '07 #5

P: 76
Here is how you do it.
1. Open the Microsoft Script Editor
2. Select your textbox from the objects drop down list.
3. Select the onkeypress event from the events drop down list.
4. Insert this code between the <script>...</script>
Expand|Select|Wrap|Line Numbers
  1. if window.event.keyCode = 13 then
  2. YOURBUTTONHERE.click()
  3. end if
  4.  
13 is the ascii number for the enter button you can use any button you want here.
So simple.

I also found another line of code that throws away the enter button so people are not entering the wrong(return carriage) data into the database.
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT>
  2.  
  3. function kH(e) {
  4. var pK = document.all? window.event.keyCode:e.which;
  5. return pK != 13;
  6. }
  7. document.onkeypress = kH;
  8. if (document.layers) document.captureEvents(Event.KEYPRESS);
  9.  
  10. </SCRIPT>
I appreciate your input on this topic. I hope I'm not getting a bad rap on here. I seem to post to my own questions more than anything. I should just keep a journal or something, but this way I get to share information for anyone who actually searches for their problem before asking the question.
Thanks for being my idea-bouncing-off-wall. thescripts FTW!
Oct 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.