Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Select entire field on Set Focus (97)

    I have an unbound text box named order. On the before update I test to see if there is an order by that number. If there are no orders I display a msgbox stating that there are no records. After I hit OK in the message box I would like the entire field to be selected so I can just type a new value that will delete the old value. I get a error stating that I cannot use setfocus. This is what I have now:
    Dim strwhere As String
    strwhere = "[on] = forms.frmstatus.form.order"
    If DCount ("*", "tblOrder", strwhere) = 0 then
    MsgBox "This is not a valid order"
    cancel = true
    'me.order.SetFocus
    End If

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Select entire field on Set Focus (97)

    When the Before Update event occurs, order still has the focus, so SetFocus doesn't work. Instead, try this:

    Me.order.SelStart = 0
    Me.order.SelLength = 100

    (100 is just a value larger than the longest text that can be entered, it has no intrinsic meaning)

  3. #3
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Select entire field on Set Focus (97)

    I tried to put the code in the before update of the control. I get a run time error 2185. You can't reference a property for a control unless the control has the focus. So I tried to put in me.order.setfocus and it gave me a error 2108, you must save the field before you execute the setfocus method. I see in a previous reply that you suggested putting the selstart and sellength code in the before update of the form but that did not select the entire field for me. Where have I gone astray? Thank you for your help.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Select entire field on Set Focus (97)

    The code should be in the Before Update event procedure of the Order control. Since this event occurs while Orders still has the focus, error 2185 shouldn't occur. You can NOT include the line Me.Order.SetFocus in the Before Update event of Order itself.

    If you still have problems, could you post a stripped down copy of your database? See <post#=401925>post 401925</post#> for instructions.

  5. #5
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Select entire field on Set Focus (97)

    If you enter an order that is not in the tblopenorders the error will pop up. Thank you for taking a look at this.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Select entire field on Set Focus (97)

    Ah, I see now. The error occurs if no records are currently displayed, not if some records are already displayed (this was the case in my test form). Try this version:

    Private Sub Order_BeforeUpdate(Cancel As Integer)
    Dim strWhere As String
    strWhere = "[ON] = Forms!frmOpenOrderStatus!Order"
    If DCount("*", "tblOpenOrders", strWhere) = 0 Then
    MsgBox "This is NOT a valid order number, Please run retrieve orders again", _
    vbInformation
    Cancel = True
    Me.Order.Undo
    ElseIf DCount("*", "qryOpenOrderStatus") = 0 Then
    'query returns no records
    MsgBox "There are no records to be displayed", vbInformation
    Cancel = True
    Me.Order.Undo
    End If
    End Sub

    Note: I added Cancel = True in the second part.

  7. #7
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Select entire field on Set Focus (97)

    Hi Hans,
    That code works great when there is not an order or if there are no records. But if there are records, the field does not get highlighted, ready to enter another order number. It looks like it works after entering the first order number but not after entering the second order number. I tried on the after update event of the order control to do a setfocus but that had no effect. I also tried the selstart and sellength and that had no effect. Is there a way to select the entire field when there are records? Thank you for all your help.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Select entire field on Set Focus (97)

    I don't know how to solve this. Why don't you change the search text box to a combo box that lists only those order numbers that have corresponding records?

  9. #9
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Select entire field on Set Focus (97)

    I will try that. That would eliminate the need to check to see if the order number is valid. Thank you.

Posting Permissions

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