Results 1 to 5 of 5
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Probably simple but it ain't working (Access2K, Win2KPro)

    Two quick problems:

    1) on a form, I have code to check if the data in a textbox already exists if the record is new; if it is, I pop up a vbCritical msgbox saying the data already exists, please enter a unique value. I want to set the focus back to the textbox in question, but the setfocus doesn't fire. I have tried using the dupe-checking code in OnExit and OnLostFocus but in neither case does the focus fire. I have tried Forms!IntakeForm!ClientFileNumber.SetFocus and Me.ClientFileNumber.SetFocus and have tried If Cancel = True...SetFocus...End If or just writing the SetFocus code after the vbCritical line, but in all cases the focus goes on to the next control.

    What is needed to get the SetFocus to work?

    2) On a form/subform I want to setfocus to a control on the subform but can't seem to get that to work either. In this scenario, the form/subform is pulled up after selecting a record in a drop-down and clicking on a button to open the form.

    What is needed to find and setfocus to a subform control?

    I know these questions are really simple so I think I am missing something simple as well. Just a nudge in the right direction would be much appreciated.

    TIA

  2. #2
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,623
    Thanks
    3
    Thanked 60 Times in 60 Posts

    Re: Probably simple but it ain't working (Access2K, Win2KPro)

    What event are you triggering the uniqueness check? You may be getting an error condition from the database engine presuming that the field involved has a unique index. Otherwise I don't know of a reason why the SetFocus code shouldn't work - but perhaps some other code is moving the focus after you attempt to. As to referencing a subform, you should be able to do it the same way you reference controls on a subform from code on a main form - see the VBA help for examples. If that doesn't solve the problem, post what you are trying to use, and we'll try to help.
    Wendell

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Probably simple but it ain't working (Access2K, Win2KPro)

    the control is a simple text box - not indexed. i'm not getting any errors but the event doesn't fire. the only thing that seems to be me to be happening is when i TAB out of the field and the data already exists, i get the expected message box and the tab goes merrily on to the next control (also a text box).

    here's the code:

    Private Sub ClientFileNumber_Exit(Cancel As Integer)
    Dim rst As DAO.Recordset
    Dim EnteredFileNumber As String
    Dim X As Integer
    Dim Response As Integer

    If Me.NewRecord = True Then
    ' Access the query results
    Set rst = CurrentDb.OpenRecordset("ClientInformation", dbOpenDynaset)

    EnteredFileNumber = Me.ClientFileNumber
    X = 0

    ' Step through the query result records
    Do While Not rst.EOF
    If rst![ClientFileNumber] = EnteredFileNumber Then
    X = X + 1
    End If
    rst.MoveNext
    Loop

    Set rst = Nothing

    If X > 0 Then

    Response = MsgBox("There is " & X & " records with the file number " & EnteredFileNumber & "!" & vbCrLf & "Please enter another file number.", vbCritical)
    If Response = 1 Then
    Forms!IntakeForm!ClientFileNumber.SetFocus
    End If
    End If
    End If
    End Sub

    As you can see, it's On Exit and if NewRecord is True the code cycles thru the table and incriments a counter for any other instances of the same file number and pops up a vbCritical style message if X > 0. I tried using the trigger for clicking on the OK for the message box to get the SetFocus to fire and have also tried just doing SetFocus after the MsgBox code. But, not working...

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Probably simple but it ain't working (Access2K

    Try replacing Forms!IntakeForm!ClientFileNumber.SetFocus with

    Cancel = true

    This cancels the exit from the control and returns the focus to the control.
    Regards
    John



  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Probably simple but it ain't working (Access2K

    that works and is elegant to boot.

    thanks all!

Posting Permissions

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