Error passing string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • onyris
    New Member
    • Aug 2008
    • 42

    #1

    Error passing string

    Hi all

    Trying to pass the value from a textbox to another form and i get an error .
    The error is this :

    Microsoft Office Access can't find the field "|" referred to in your expression.

    What i have is a form called " names_form", with a textbox called "txtname" and another form called "searched_f orm" , with a label called "lbl_searchedte xt"
    When i type some text into "txtname" and i click a button , i want to display the text in the label " lbl_searchedtex t" on the "searched_form" .

    The code i have now is this, and is placed on the "searched_f orm" load event.

    Code:
     Dim text As String
    text = [names_form]![txtname].text
    lbl_searchedtext.Caption = text
    lbl_searchedtext.Visible = True
    I found the code somewhere online and the guy who posted said is working ,but for me is NOT,
    Any help , please.
  • onyris
    New Member
    • Aug 2008
    • 42

    #2
    I have to say sorry again to NeoPa , tried to use the [code] tag ,but i can see that i got it worg again .
    Do u mind sending me a message explaing how to use those tags , please ,cose i don't want to be banned or something.
    Thanks , and sorry again!

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      When writing your post simply do it like this

      [ code ]
      'Your code goes here
      [ /code ]

      When you actually do this, you have to eliminate the spaces between the brackets and code and /code for it to work.

      Linq ;0)>

      Comment

      • Denburt
        Recognized Expert Top Contributor
        • Mar 2007
        • 1356

        #4
        Try this:
        Code:
         Dim text As String
        text = [B]Forms![/B][names_form]![txtname].text
        [B]Me![/B]lbl_searchedtext.Caption = text
        [B]Me![/B]lbl_searchedtext.Visible = True
        Last edited by Denburt; May 21 '09, 09:10 PM. Reason: lol code tags bombed

        Comment

        • onyris
          New Member
          • Aug 2008
          • 42

          #5
          Not working .
          The same error again .

          Comment

          • onyris
            New Member
            • Aug 2008
            • 42

            #6
            no . sorry now is this error:

            you can't refference a property or method for a control unless the control has the focus.

            and is pointing on this line
            Code:
            text = Forms![names_form]![txtname].text

            Comment

            • Denburt
              Recognized Expert Top Contributor
              • Mar 2007
              • 1356

              #7
              ok I am nearing the end of a long day here I just realized also that you used text as a variable and although you did declare it in a Dim statement it is likely a reserved word. on strings I usually in str to help me try to avoid things such as that. This should work though:
              Code:
               Dim strText As String
              strText = Forms![names_form]![txtname]
              Me!lbl_searchedtext.Caption = strText
              Me!lbl_searchedtext.Visible = True

              Comment

              • onyris
                New Member
                • Aug 2008
                • 42

                #8
                ok I am nearing the end of a long day here I just realized also that you used text as a variable and although you did declare it in a Dim statement it is likely a reserved word. on strings I usually in str to help me try to avoid things such as that. This should work though:
                Code:
                 Dim strText As String 
                strText = Forms![names_form]![txtname] 
                Me!lbl_searchedtext.Caption = strText 
                Me!lbl_searchedtext.Visible = True

                Still not working.
                ??

                Comment

                • missinglinq
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3533

                  #9
                  This code never worked for anyone, not in the fashion you've described, not in Access!

                  text = [names_form]![txtname].text

                  will only work if [txtname] has focus, and it obviously doesn't when it's not on the current (Searched_Form) form! And Text should not be used as a variable name, as it's a Reserved Word in Access.

                  In your first form, behind your button to open the the form Searched_Form, use the OpenArgs to send the TextName value to the label on the second form:

                  Code:
                  Private Sub YourCommandButtonName_Click()
                    DoCmd.OpenForm "Searched_Form", , , , , , Me.TextName
                  End Sub
                  Then in the Searched_Form load event:
                  Code:
                  Private Sub Form_Load()
                    If Not IsNull(Me.OpenArgs) Then 
                     lbl_searchedtext.Caption = Me.OpenArgs
                     lbl_searchedtext.Visible = True 
                   End If
                  End Sub
                  If you need to, you can include other arguments in your Open Form command as well.

                  Linq ;0)>

                  Comment

                  • onyris
                    New Member
                    • Aug 2008
                    • 42

                    #10
                    Right , it makes sens now ,but the only problem is that behind the button i run a Macro which opens the Second Form.
                    How do i put the cod in there??

                    Comment

                    • onyris
                      New Member
                      • Aug 2008
                      • 42

                      #11
                      In design View of the Macro , i have a set of actions .

                      Can i use one of those actions to put the code in there and if yes which one do i have to use?

                      Comment

                      • onyris
                        New Member
                        • Aug 2008
                        • 42

                        #12
                        My Macro is doing this at the moment:

                        OpenQuery - query to search
                        OpenForm - searched form [searched_form] (where i want to pass the string)
                        Close - query to search
                        Close - names form [names_form]

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32669

                          #13
                          Originally posted by onyris
                          I have to say sorry again to NeoPa , tried to use the [code] tag ,but i can see that i got it worg again .
                          Do u mind sending me a message explaing how to use those tags , please ,cose i don't want to be banned or something.
                          Thanks , and sorry again!
                          No worries if you've made the attempt Onyris :)

                          Here are a couple of generally helpful pointers for posting on Bytes. I noticed your code wasn't compiled as the .Text part of line #2 is displayed as .text.

                          CODE TAGS

                          Tags are done as matching pairs where the opening one is surrounded by [...] and the closing one by [/...]. A set of buttons is available for ease of use in the Standard Editor (Not the Basic Editor). The one for the [ CODE ] tags has a hash (#) on it. You can choose which editor to use in your profile options (Look near the bottom of the page).

                          POSTING UNCOMPILED CODE
                          It is always a good idea to ensure that variable name checking is enabled, AND your code compiles (at least compilation has been attempted), before submitting a question.

                          This avoids asking questions which are much more easily resolved on your own PC than on a forum.

                          To ensure variable name checking is enabled for all new modules, go to - Tools / Options / Editor (from the VBA Editor window) and set Require Variable Declaration to True (checked). For existing modules, ensure that the Option lines at the very top include :
                          Code:
                          Option Explicit
                          To compile your project, select (again from the VBA Editor window) Debug / Compile Project Name.

                          We ARE generally happy to help with compilation problems too (If you find an error reported and you can't resolve it, let us know), but we do expect members to have tried compiling before submitting a question. That way we have a better idea of the sort of problem we're looking at.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32669

                            #14
                            Originally posted by onyris
                            My Macro is doing this at the moment:

                            OpenQuery - query to search
                            OpenForm - searched form [searched_form] (where i want to pass the string)
                            Close - query to search
                            Close - names form [names_form]
                            Generally Macros are not very widely used. Full control is at least difficult when constrained to usage of macros.

                            Instead you can use VBA code. If you are using VBA code then it is sensible to replace your macro with VBA code which can then include this code that you're currently working through.

                            Does this make sense?

                            Comment

                            Working...