My code doesn't wants to set the control default value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mihail
    Contributor
    • Apr 2011
    • 759

    My code doesn't wants to set the control default value

    Hello !

    I try to open a form from the NotInList event (check box) and to pass the NewData as OpenArgs.
    Code:
    DoCmd.OpenForm "frmContoare_tipuri", , , , , acDialog, NewData
    Here is the Load event in the form I wish to open (frmContoare_tip uri)
    Code:
    Private Sub Form_Load()
        If Not IsNull(Me.OpenArgs) Then
            Tip.DefaultValue = Me.OpenArgs
        End If
    End Sub
    But I've obtained the #Name? (see attachment)in the Tip field

    Also I've tried to use the Open event but with the same result.

    In order to debug, I use this:
    Code:
    Private Sub Form_Current() 'Debug
        MsgBox Tip.DefaultValue
    End Sub
    And the message is what I expected to be: the right value (also you can see this in attached word file)

    Any idea ?

    Thank you !
    Attached Files
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Mihail, that's a curious one. Have you verified that your code in Form_Load is the culprit? If you comment out
    Code:
    Tip.DefaultValue = Me.OpenArgs
    does the error go away?

    Jim

    Comment

    • Mihail
      Contributor
      • Apr 2011
      • 759

      #3
      I open this form from two different places.
      From other place I open this form without OpenArgs (it is why I have the IF statement in Form_Load) and all is OK.

      So I'm very sure that this code bring me in trouble.
      Just, like you, I don't understand why.

      Maybe you should know that I have also this code in frmContoare_tip uri.
      Code:
      Private Sub Form_Open(Cancel As Integer)
          Call FormMoveResize(Me)
          DoCmd.GoToRecord , , acNewRec
      End Sub
      But, I repeat: All is OK if I don't open with OpenArgs.

      Thank you for reply !

      Comment

      • jimatqsi
        Moderator Top Contributor
        • Oct 2006
        • 1293

        #4
        There's more happening than we can see from these posts. I doubt changing the default value property is causing the problem.

        The thing is, that "#Name" usually appears in a control when the control source doesn't appear in the recordsource of the form. But in a continuous form, one would expect it on every row if it happens at all.

        I would still comment out that line and verify the problem stops happening when you don't execute
        Code:
        Tip.DefaultValue = Me.OpenArgs
        . I have an idea that the problem will continue.

        The form's order of events is Open ->Load ->Resize ->Activate ->Current

        I believe you should reconsider what you have happening in each of those events. Right now the default property is being set AFTER you have moved to the new record row.

        Jim

        Comment

        • Mihail
          Contributor
          • Apr 2011
          • 759

          #5
          Code:
          Private Sub Form_Open(Cancel As Integer)
              Call FormMoveResize(Me)
              DoCmd.GoToRecord , , acNewRec
              If Not IsNull(Me.OpenArgs) Then
                  Tip.DefaultValue = Me.OpenArgs
              End If
          End Sub
          I would still comment out that line and verify the problem stops happening when you don't execute
          Expand|Select|W rap|Line Numbers

          Tip.DefaultValu e = Me.OpenArgs

          . I have an idea that the problem will continue.
          Unfortunately :) the problem disappear when I do this. All is OK if I comment this line.

          I believe you should reconsider what you have happening in each of those events. Right now the default property is being set AFTER you have moved to the new record row.
          I've tried this:
          Code:
          Private Sub Form_Open(Cancel As Integer)
              Call FormMoveResize(Me)
              DoCmd.GoToRecord , , acNewRec
              If Not IsNull(Me.OpenArgs) Then
                  Tip.DefaultValue = Me.OpenArgs
              End If
          End Sub
          but with exactly the same result.

          Comment

          • jimatqsi
            Moderator Top Contributor
            • Oct 2006
            • 1293

            #6
            You must put the default value property setting prior to your
            Code:
            DoCmd.GoToRecord , , acNewRec
            The moment you go to a new record the DefaultValue property is needed, but you haven't plugged it in yet. Line 3 should appear after line 6.

            That doesn't explain why you're getting what you're getting, but it will prevent you from getting the tip default you want.

            You might try putting this in the OnCurrent event
            Code:
            If Not IsNull(Me.OpenArgs) Then ' tip default passed in OpenArgs?
                If Len(Nz(Me.tip)) = 0 Then ' Is this a new entry?
                    Me.tip = OpenArgs
                End If
            End If
            That could be another way to achieve the same result, but without ever knowing why the first idea did not work.

            Jim

            Comment

            • Mihail
              Contributor
              • Apr 2011
              • 759

              #7
              As you can see I have tried this. No change.

              Code:
              Private Sub Form_Load()
                  If Not IsNull(Me.OpenArgs) Then
                      Me.Tip.DefaultValue = Me.OpenArgs
                  End If
                  DoCmd.GoToRecord , , acNewRec
              End Sub
              
              Private Sub Form_Open(Cancel As Integer)
                  Call FormMoveResize(Me)
              '    If Not IsNull(Me.OpenArgs) Then
              '        Me.Tip.DefaultValue = Me.OpenArgs
              '    End If
              '    DoCmd.GoToRecord , , acNewRec
              End Sub
              I'll try to avoid as much as possible to set the value for the field in code.
              That because the user can change his mind and the fields are, both of them, required.

              Comment

              • Mihail
                Contributor
                • Apr 2011
                • 759

                #8
                Founded the answer (with the help from other forum):
                Code:
                Me.Tip.DefaultValue = "'" & Me.OpenArgs & "'"
                Works also with double quotes:
                Code:
                Me.Tip.DefaultValue = """" & Me.OpenArgs & """"
                Thank you for your time and patience !

                Cheers !

                Comment

                • jimatqsi
                  Moderator Top Contributor
                  • Oct 2006
                  • 1293

                  #9
                  Thank you for posting the solution. That was a tricky one.

                  Comment

                  • Mihail
                    Contributor
                    • Apr 2011
                    • 759

                    #10
                    This forums should be (in my opinion) a school for every one.
                    Bytes policy don't allow me to post a link to other forums. OK. I am a guest here and I must respect their rules. No problem. But I like to inform you (all) that I post my question ONLY here. In the Bytes. The reason was that I have not can access the "other" forum, at that time. Then, in that "other forum" I post a link and an ask. And they have helped me with no problem.
                    And no one attempt to remove my post as I think that will happen with this one (unfortunately) . And I don't refer to you, jimatqsi.

                    One more info related to the thread's subject:
                    The same guy that give me the solution have said that the same #name? error is raised if the size of the field is too small. I don't test yet.

                    All the best to all !

                    Comment

                    • jimatqsi
                      Moderator Top Contributor
                      • Oct 2006
                      • 1293

                      #11
                      Mihail, I have always found Bytes to be a very, very good place to get information. I have learned so much on this forum, and I'm very grateful for that. And sometimes I use other forums when it is necessary, but Bytes is my favorite.

                      Within the last year I think they have eliminated the ban on links to other sites. I know I have put links in several recent posts.

                      Thanks for sharing your thoughts. I look forward to seeing more posts from you, maybe solving my problems. :)

                      Jim

                      Comment

                      • zmbd
                        Recognized Expert Moderator Expert
                        • Mar 2012
                        • 5501

                        #12
                        It's my understanding that links to other forums that compete directly with Bytes are still frowned on; however they may not be automatically removed - either directly from the post, or by removing the post. I know that I would tend to simply redact the direct url link and leave the post and the reference to the site.

                        The issue with links is that they tend to become obsolete.
                        It is usually preferred to also include the relevant information; thus, the link becomes like a citation in a book.

                        Comment

                        Working...