locked
Hello I Am Ruuning this on lostfpcus RRS feed

  • Question

  • Private Sub ID_LostFocus()
    If (IsNull(Me.ID.Value)) Then
    MsgBox "Error"
    DoCmd.GoToControl "First_Name"
    DoCmd.GoToControl "ID"
    End If
    End Sub

    But This is giving run time error 2046 gotocontrol commend is not available.

    Wednesday, December 27, 2017 6:42 AM

All replies

  • Hi Inam Ansari,

    I try to test the code on my side and find that it is working properly.

    I comment the messagebox to see the effect of GoToControl.

    see the testing result below.

    you can see that when textbox is empty and when you try to move to any other textbox or control then it will again focus to ID Textbox.

    but if ID textbox have the value then you can move to other textbox or control.

    the code is working correctly without any error.

    you can try to remove messagebox and again try to make a test on your side to check whether the issue is still exist or not.

    we will try to provide you further suggestions to solve the issue.

    for further reference, you can also refer link below.

    Need help with a Macro error # 2046

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 27, 2017 8:21 AM
  • ciao Inam,

    since on exit event is executed just before on lost focus, I would suggest a way which allows you to write less code, less verbose way then.

    simplier like this if you do not need to show any message box ( I assume yout textBox control is called txt):

    Option Compare Database

    Option Explicit

     

    Private Sub txt_Exit(Cancel As Integer)

     Cancel = Len(Me.txt & vbNullString) = 0

     End Sub

    or like this if you prefer showing a message box, I have just added backcolor change of txtBox control involved in exit Event ( I assume yout textBox control is called txt2)

    Option Compare Database

    Option Explicit

     

    Private Sub txt2_Exit(Cancel As Integer)

     

    With Me

        If Len(.txt2 & vbNullString) = 0 Then

            .txt2.BackColor = vbRed

            VBA.MsgBox prompt:="you have to fillthis control!", _

                       buttons:=vbCritical, _

                       Title:="Warning!"

            .txt2.BackColor = vbWhite

            Cancel = True

        End If

    End With

     

    End Sub

    you should also disallow close button on your form to complete it.

    HTH.

    Ciao, Sandro.

    Wednesday, December 27, 2017 11:55 AM
  • Hi lnam,

    I created a table with “ID” and “First_Name”, generate a form and add your code to ID_LostFocus, but I fail to reproduce your issue.

    I assume this issue is related with other events or code in your form, I would suggest you share us a simple access database which could reproduce your issue.

    Best Regards,

    Tao Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 3, 2018 6:48 AM