Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Sep 2004
    Posts
    112

    Unanswered: Help with a bit of code

    Can anyone see the problem in my code?
    I have a text field called 'Text210' and a text field called 'order'
    if someone enters an incorrect value in to the 'order' field the 'Text210' field on my form displays the word "Incorrect"
    this in turn displays a message box "sorry the......"
    and the user hits ok.
    At this point the code is meant to undo what the user has inputted in the 'order' text box and place the cursur back in the 'order' box field.

    The code will generate the message but then the curser goes to another field altogether.

    Private Sub order_AfterUpdate()
    On Error GoTo ErrHandler:
    If Me.Text210 = "Incorrect" Then
    MsgBox "Sorry, the order number entered is not recognised", vbOKOnly, "Invalid Order Number"

    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
    Me.order.SetFocus
    End If

    ErrHandler:


    End Sub

    Any ideas??

    Saltman

  2. #2
    Join Date
    Nov 2002
    Location
    Sacramento, CA
    Posts
    34
    sorry....didn't read the entire problem and posted.
    Last edited by dragracer; 11-19-04 at 13:43.
    Make the money, don't let the money make you.

  3. #3
    Join Date
    Nov 2003
    Posts
    1,487
    Play with this....but put it into the BeforeUpdate event of your order field:

    Code:
    Private Sub order_BeforeUpdate(Cancel As Integer)
    On Error Resume Next
    If IsNull(Dlookup("[myTablesOrderID]", "myOrderTableName", _
    "[myTablesOrderNumber] = '" & Me.order & "'" Then
    Me.Text210 = "Incorrect"
    MsgBox "Sorry, the order number entered is not recognised", vbOKOnly, "Invalid Order Number"
    Cancel = True
    Me.order.SetFocus
    End If 
    End Sub


  4. #4
    Join Date
    Nov 2004
    Posts
    64
    it seems you only want to clear the field orders, add in Me![orders]=null as below

    Private Sub order_AfterUpdate()
    On Error GoTo ErrHandler:
    If Me.Text210 = "Incorrect" Then
    MsgBox "Sorry, the order number entered is not recognised", vbOKOnly, "Invalid Order Number"

    Me![orders]=null

    Me.order.SetFocus
    End If

    Peter.t

  5. #5
    Join Date
    Sep 2004
    Posts
    112

    Code still wont work

    This is my original code. All it want do is tab back to the order field after the msgbox has been seen on screen but it tabs to another field on my form! Why?.

    Private Sub order_AfterUpdate()
    On Error GoTo ErrHandler
    If Me.Text210 = "Incorect" Then
    MsgBox "Sorry, the order number entered is not recognised", vbOKOnly, "Invalid Order Number"

    Me.order.SetFocus

    ErrHandler:
    End If
    End Sub


    Saltman

  6. #6
    Join Date
    Nov 2004
    Posts
    23
    I'm not sure if this is the best way to do it but I added the following:

    Private Sub order_AfterUpdate()
    On Error GoTo ErrHandler
    If Me.Text210 = "Incorect" Then
    MsgBox "Sorry, the order number entered is not recognised", vbOKOnly, "Invalid Order Number"

    Me.Text210.SetFocus
    You can also add the line at this line to clear order field
    me!order = null


    Me.order.SetFocus

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    If you are doing a validation check then it must, must, must (sorry should, should , should) be in the "before update" event, not the "after update" event. Without seeing all the code is possible you may have a similar problem elsewhere. it is possible to get a contention within access with the focus being pulled by 2 competing events (especially if you set soemthing in the "got focus" and "lost focus" events).

    As an alternative strategy you can perform validation as each field is validated an refuse to allow the user to move off unless they have entered valid information. This is something thos most interface designers would frown on as being againt the "spirit" of modern GUI's - bring back VT100 / command line I say!

    You could consider lumping all the validation code into the forms "before update" (rather than the controls event)- it can seem less elegant but then if all you lump all validation code is in the same place it is easier to stop conflicts where you might have 2 or more fields requirng updating. I often set a background colour to show fields with validation errors, and or a scroll box underneath to highlight all the errors encountered on validation - Or perhaps the user base I support are that much thicker and make more mistakes.

    Also you may get away with calling a variable and column "order", but I don't think its a helpful habit to get into. If its not a reserved name then its getting close to one as Access does do some mighty funny tricks if you start using reserved words - the form designer can accept them but the query designer and JET can quite very upset very quickly and cause havoc. Soem of it comes down to a clear naming policy for varaiables AND tables / columns / indexs.
    HTH

  8. #8
    Join Date
    Sep 2004
    Posts
    112
    Thanks for your indepth reply but not sure what you are getting at ?
    I have posted my code a few times in different versions and I have tried it in the BeforeUpdate but it still does not want to focus back on to the order field.
    Do you have any other ideas?

    Saltman

  9. #9
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Are you doing any other checks at or about the same time which force a set focus event
    For example if it is you design stylistic to move the focus to an invalid field after validation failures then is you have more than one error there will be some contention, especially if you have put some checking in the "lost focus" event. If you have 2 controls with an "on focus", "Got Focus" OR "lost Focus" event then it is possible for the sert focus method to fail
    eg if when you complete your processing the user has left the focus on a button with a "Lost Focus" event whioch in itself chanegs the focus then you will have a contention, which event takes precendance - you don't know. Used sparing on/set/lost focus can be a great tool but used to often in the same form they are a nightmare. There's other ways of achieving the same result ie "Oi user, user data is c!!p do this or else"........

    I suggest you place all the validation code in the forms "before update" event, not the "after update" event - that way round you don't need to do the "undo" command in your first example of the code on this thread. Not an individual control so that its easy to manage.

    Furthermore, rather than using the setfocus to show a field which is invalid I suggest you change the background colour of the field concerned, say from white to red. Remember tha tif you do this you need to reset the colours on the change of a record (form "current" event) and addition of record, ("before addition") AND before validation.

    eg
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    <insert you validation code here>

    if intNoErrors>0 then 'or if you prefer text210="incorrect"
    order.backcolor=qbcolor(12)
    else
    order.backcolor=qbcolor(15)
    endif
    'OR if you prefer
    order.setfocus'
    cancel=true 'this forces the before update event to be cancelled
    End Sub

    I would suggest it ain't smart to do a text comparison so would tend to do the validation, and if an error is found increment the intNoErrors, you cna also use intNoErrors to store the error code and use a bitwise comparison to decode the error later on. Especially if you are passing information between forms / pages as on a website. eg error 0= no errors, 1=Invalid order number, 2=No order number, 4= wrong order number, 8= order number valid but wrong customer, 16= Invalid customer. etc.
    The advantage is that you encapsulate the errors into a single code
    eg if the customer number was invalid and no order had been specified the return code could be 18 (16+2).

    The point about the naming of column / field names is that ORDER is close enough to an SQL reserved word "ORDER BY", some SQL implementations get very sniffy about using reserved words - it can be a problem. Access has several inconsistancies whereby it will accept a reserved word in one area and reject it in another area leading to severe problems modifying evrything. it was a suggestion that you should consider (in future) making certain your choice of column / field names does not cause problems later on.

    HTH

  10. #10
    Join Date
    Sep 2004
    Posts
    112

    Doing my head in

    Thanks for that but its still not workin, back to basics.

    I enter a value in to a field called "order"
    I have a text box named Text210 that validates the value entered in to the "order" field, with the value in another field called"Mill_Order_No" this is the control source: IIf([order]=[Mill_Order_No]), "Ok", "Incorrect")

    (I would like to include the above into my code but cant figure out)

    Anyway using the code below when a user enters an incorrect value into the order field the above code does not activate unless you click some part of the form!. What I want it to do is when a user has entered an incorrect value and presses the enter key I want the Msgbox to pop up then.

    Private Sub Form_BeforeUpdate(cancel As Integer

    If Text210 = "Incorect" Then
    MsgBox "Order number not recognised"

    Me.order.SetFocus
    End If

    End Sub

    Please help.........................

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    OK, lets get back to basics
    what I would do on this occassion is:-

    in the order "lost focus" event place the following code.

    if [order] <> [mill_order_no] then ' order number not authenticated
    text210.text="Incorrect order no"
    order.setfocus
    else
    text210.text="Order No OK"
    endif

    A word of caution don't be to free using focus events its very easy to place code in to lost focus events and have the code boucing betweeen the two.

    IF you put it in the on change, keypress etc it will try to authenticate as soon as a change is made, not after. Alternatively you could put it in the key press event and trap for any key press which would cause the control to loose focus (eg tab, enter etc)

    if keypressed = chr$(<whatever the tab key is>) or <whatever the enter key is>

  12. #12
    Join Date
    Nov 2003
    Posts
    1,487
    Try using:


    Screen.PreviousControl.SetFocus


  13. #13
    Join Date
    Sep 2004
    Location
    Rio Bravo, Mexico
    Posts
    27
    Saltman, like mention on previous post, you should put your code under the "Before update" and add the "Cancel = true" after you meassage. This should prevent the cursor from moving to the next field. You should not even have to include "order.setfocus"

    Example:
    Dim intRes As Integer, strFolio As Variant, strCriterio as string
    strCriterio = "[Folio]= " & "'" & Me!FOLIO & "'"
    strFolio = DCount("[FOLIO]", "tblOrdenServicio", strCriterio)
    If strFolio > 0 Then
    intRes = MsgBox("Folio ya existe, deseas conservarlo? ", vbYesNo)
    If intRes = vbNo Then
    Cancel = True
    Exit Sub
    End If
    End If
    End Sub

    Dont pay attention to the spanish code, but basically what it does it checks if the data entered exists in a table, and if it does it displays the message then cancels the update if answer is NO. The "Cancel = true" prevents the cursor from jumping to the next field. You may want to add "me!order = null" after the cancel = true line. But like mention before, for the Cancel = true to work, it must be under the "Before update"

    Hope it helps
    Carlos

  14. #14
    Join Date
    Sep 2004
    Posts
    112

    Unhappy Still a pain in the head

    Thanks but I dont understand afew of your terms,
    what is 'folio', 'FOLIO'? are these meant to mean 'order', 'Mill_Order_No'?

    The table name of the 'order' field is wip dat/qty and the table name of the 'Mill_Order_no' is work in progress.

    when I enter data in to the field 'order' I want it to check the field 'Mill_Order_No' in the work in progress table.
    if there is no matching data I want the focus to stay on the 'order' field. If a matching field is found in the 'Mill_Order_No' I want to continue.

    Thanks

    Saltman

  15. #15
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    have you tried putting your validation code in the controls lost focus event?

    I find the description of the problem seems to move slightly with each iteration.

    the downside of putting it in the "lost focus" event is that you will only get the validation code running when you move off the control to another. If the user presses a record navigation button then the dataset can be updated with duff data. But you should be able to handle that outside. Another problem is that id the control the users moves to also has a "lost focus" event then you will get a contention which will probably lead to a stack overflow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •