Auto - Start Code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JimmyFly
    New Member
    • Feb 2008
    • 5

    Auto - Start Code

    Hello,
    I have a form that will be used to display data from a table using 'Dlookup' based on what is entered into a textbox. I can get it to work by callng the code via a form button, but wanted to know if there is a way to call the code without having to use a button. I will be using a barcode scanner to read the data to be queried on but I don't want the end-user to have to scan their barcode and then click a form button but rather just scan the barcode and it automatically initiate the 'Dlookup' code.

    Thanks for your help.
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    Originally posted by JimmyFly
    Hello,
    I have a form that will be used to display data from a table using 'Dlookup' based on what is entered into a textbox. I can get it to work by callng the code via a form button, but wanted to know if there is a way to call the code without having to use a button. I will be using a barcode scanner to read the data to be queried on but I don't want the end-user to have to scan their barcode and then click a form button but rather just scan the barcode and it automatically initiate the 'Dlookup' code.

    Thanks for your help.
    Not sure how you have this set up, but have you tried shifting the code from the button, to the "On Change" event of the text field? That might trigger it everytime it's updated via the scanner.

    Comment

    • JimmyFly
      New Member
      • Feb 2008
      • 5

      #3
      Code tags

      Originally posted by Megalog
      Not sure how you have this set up, but have you tried shifting the code from the button, to the "On Change" event of the text field? That might trigger it everytime it's updated via the scanner.
      Thanks, I tried that and I get the following error:
      "Run- time error '3075':
      Syntax error (missing operator) in query expression 'ID = '."

      Below is the code I am using:

      Code:
       Private Sub CODE_Change() 
      Dim BAR, NAME
      BAR = CODE.Value
      NAME = DLookup("NAME", "TBL_NAME", "ID = " & (BAR) & "")
      FIRST.Value = NAME
      End Sub
      CODE.value is the text box that the scanner is inputting the data.
      FIRST.value is the text box that will display the queried data from the DLookup.

      Any ideas?

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Originally posted by JimmyFly
        Thanks, I tried that and I get the following error:
        "Run- time error '3075':
        Syntax error (missing operator) in query expression 'ID = '."

        Below is the code I am using:

        Private Sub CODE_Change()
        Dim BAR, NAME
        BAR = CODE.Value
        NAME = DLookup("NAME", "TBL_NAME", "ID = " & (BAR) & "")
        FIRST.Value = NAME
        End Sub

        CODE.value is the text box that the scanner is inputting the data.
        FIRST.value is the text box that will display the queried data from the DLookup.

        Any ideas?
        Try:
        [CODE=vb]
        Private Sub CODE_Change()
        'If [ID] is Text
        Me![FIRST] = DLookup("[<Lookup Field in Table>]", "TBL_NAME", "ID = '" &
        Me![CODE] & "'")

        'If [ID] is Numeric
        Me![FIRST] = DLookup("[<Lookup Field in Table>]", "TBL_NAME", "ID = " &
        Me!
        Code:
        )
        End Sub

        Comment

        • JimmyFly
          New Member
          • Feb 2008
          • 5

          #5
          Originally posted by ADezii
          Try:
          [CODE=vb]
          Private Sub CODE_Change()
          'If [ID] is Text
          Me![FIRST] = DLookup("[<Lookup Field in Table>]", "TBL_NAME", "ID = '" &
          Me![CODE] & "'")

          'If [ID] is Numeric
          Me![FIRST] = DLookup("[<Lookup Field in Table>]", "TBL_NAME", "ID = " &
          Me!
          Code:
          )
          End Sub

          Thanks, although still get the same error.

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            Can we see an exact copy of the code you tried last?

            Linq ;0)>

            Comment

            • JimmyFly
              New Member
              • Feb 2008
              • 5

              #7
              Originally posted by missinglinq
              Can we see an exact copy of the code you tried last?

              Linq ;0)>
              Sure thing.
              [CODE=VB]Private Sub CODE_Change()
              Me![FIRST] = DLookup("name", "tbl_name", "id = " & Me!
              Code:
               & "")
              End Sub
              Thanks

              Comment

              • missinglinq
                Recognized Expert Specialist
                • Nov 2006
                • 3533

                #8
                You've left out some single quotes from around the Criteria in ADezii's code. Instead of

                [CODE=vb]Me![FIRST] = DLookup("name", "tbl_name", "id = " & Me!
                Code:
                 & "")
                it should be

                [CODE=vb]Me![FIRST] = DLookup("name", "tbl_name", "id = '" & Me!
                Code:
                 & "'")
                if id is text.

                Also, if you actually have a field named Name in your table, as your code indicates, you really have to change this! Name is a Reserved Word in Access , and a very important one, because it's the name of an Objects Property!

                Me.ProductName. Visible = False

                will make a control named ProductName invisible, but

                Me.Name.Visible = False

                will throw an error, as will any other property you try to access.

                Linq ;0)>

                Comment

                • JimmyFly
                  New Member
                  • Feb 2008
                  • 5

                  #9
                  Thank you to all those who helped me with this. I was able make it work by using a 2nd form. The issue with the error is becasue even though the textbox named CODE had a value in it from the barcode scanner, Access did not see the value until that textbox lost focus. To make it work, on the On Change event for textbox CODE I had it set focus on another textbox named Text5. Then using the On Enter event on Text5, set it to open a new form called Form2. Then using the On Open event on Form2 I called back to the first form and was able to get the value out of the CODE textbox and do my Dlookup line. I know this is a lot of steps but it worked. Apparently it all boiled down to getting the focus off the that CODE textbox so that Acces could see the value it in.

                  Thanks again.

                  Comment

                  Working...