Results 1 to 8 of 8

Thread: Code error (XP)

  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code error (XP)

    The line<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = username ")</pre>

    should be<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = '" & Username & "'")</pre>


  2. #2
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Code error (XP)

    Thank you, Hans, but...

    I still get the compile error. The highlight appears over the single quotation mark that starts the string, <font color=red>'" & Username & "'")</font color=red>.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code error (XP)

    Lucas,

    The code you posted originally had a continuation character and a line break within a string; this is invalid. I don't know if you still have the line break; if so, it should not be inside the quotes. For example:<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = '" & _
    Username & "'")</pre>

    Otherwise, the error is probably caused by wrong placement of quotes earlier in the code, for it compiles without problem for me. Check the placement of single and double quotes very carefully.

  4. #4
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Code error (XP)

    The code you posted originally had a continuation character and a line break within a string; this is invalid.

    OK, I think I need a little help with terminology. How do I recognize a "continuation character" and a "line break" ? I'm thinking the continuation character is the underscore ( _ ). But the line break...? Also, can you please elaborate on the "invalid" part.

    I know that we customarily break up long lines with continuation marks, but, if I were to ignore that convention and let the code run out on one continuous line, my original code would work...? Thank you!

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code error (XP)

    This is the offending line in the first post in this thread:<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = _
    username ")</pre>

    The underscore character _ is the continuation character, and it is followed by a return, or line end, or line break, whatever you want to call it. A continuation character is used to indicate that the instruction is continued on the next line. Ordinarily, a Visual Basic instruction must be entirely on one line, but we can get around that by using a continuation character.

    However in this code, the underscore is contained within the string starting at "[Username] and ending at username ", so the VBA interpreter considers it to be a part of the string, and not a real continuation character. Hence, the instruction should be entirely on one line. The end-of-line is invalid, since VBA hasn't "seen" the _ as continuation character.

    But..., even if you omit the underscore and end-of-line, the instruction is still not going to do what you want:<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = username ")</pre>

    Since username is within the quotes, DLookup uses "[Username] = username" as condition, it doesn't use the value of the variable username. Now, "username = username" is not a very meaningful condition, it is always true. To get the value of username, it should be outside the quotes, and since it is a string, we should enclose it in quotes. We already use double quotes in this code, so we use single quotes to enclose the value of the variable.

    So the final version is<pre>q1 = DLookup("[Username]", "FirstTable", "[Username] = '" & username & "'")</pre>

    If this causes a compilation error, there must be an error (probably a misplaced quote) somewhere before this in the code.

  6. #6
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Re: Code error (XP)

    Thanks, Hans! It works!

    What still puzzles me is why two people who clearly know what they're doing would plant errors in the code in their book?

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Virginia, USA
    Posts
    1,560
    Thanks
    37
    Thanked 1 Time in 1 Post

    Code error (XP)

    I'm trying to learn Access VBA programming. So I bought this book, Access 2000 Programming Blue Book, by Wayne Brooks and Lars Klander (published by the now-defunct Coriolis Technology Press). Right up front in this book they start me off with writing this simple little program, using "First Database" as the learning vehicle. I'm following the example in the book very carefully, but near the end of the writing of the very first module I hit an error.

    Here's the code (as far as I got before I hit the error):

    Option Compare Database

    Sub Logon()
    'Variable Declaration Section
    Dim Username
    Dim Password
    Dim q1
    Dim q2
    'Variable Definition Section
    Username = InputBox( _
    "You're programming with Access 2000." & _
    Chr(13) & Chr(13) & _
    "Please enter your username.")
    Password = InputBox( _
    "You're doing a great job!" & _
    Chr(13) & Chr(13) & _
    "Now, please enter your password.")
    'Query Execution Section
    <font color=red>q1 = DLookup("[Username]", "FirstTable", "[Username] = _
    username ")</font color=red>
    <font color=red> ...right here is where I get "Compile error

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Code error (XP)

    I hope you don't expect a serious reply from me to your last question <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

Posting Permissions

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