Results 1 to 14 of 14
  1. #1
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14

    Creating a String in VBA over multiple lines


    I am trying to create a string in VBA and have it span multiple lines in the code. Is there a way to do this?
    Here is what im trying to do:
    Code:
    query = "INSERT INTO Users(...)"
                "VALUES(...."
                "....);
    I have 10 fields to insert and would like to make it easier to read the code by splitting the single query into multiple lines.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The string continuation sequence is " & _" so:
    query = "INSERT INTO Users(...)" & _
    "VALUES(...." & _
    "....);
    ...would work.

  3. #3
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    When i try this
    Code:
        query = "INSERT INTO Users(ClockNum, FirstName, MiddleInital, LastName, Dept, DeptLocation, Location, Phone, Supervisor, Email)" & _
                "VALUES('" + Forms!Insert_User.ClockNum + "', '" + Forms!Insert_User.FirstName + "', '" + Forms!Insert_User.MiddleInitial "', " & _
                "'+ Forms!Insert_User.LastName + "', '" +Forms!Insert_User.Dept+"', '" + Forms!Insert_User.DeptLocation +"', '"+Forms!Insert_User.Location+"', " & _
                "' + Forms!Insert_User.Phone +"', '" +Forms!Insert_User.Supervisor+"', '"+Forms!Insert_User.Email+ "');"
    I get a syntax error on this statement.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Use "&" for string concatenation rather than "+". You are trying to pull a bunch of values from a form *other* than where this string is being constructed, right?

  5. #5
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    That seemed to work, it does not give me a compile error now. I noticed i was missing a "+" when i was switching them over to "&". Thanks for the help.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is this string being constructed on the Insert_User form?

  7. #7
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    I wrote a function that when the user clicks a button on the form, the function is called, and inserts the user into the database.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by RuralGuy View Post
    Is this string being constructed on the Insert_User form?
    If the answer to this question is yes then there is no need to reference the controls through the Forms collection. You can reference them directly with Me.

  9. #9
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    Quote Originally Posted by RuralGuy View Post
    If the answer to this question is yes then there is no need to reference the controls through the Forms collection. You can reference them directly with Me.
    Sorry, i guess i was totally understanding what you were asking. Yes, this is being constructed on the Insert_Users Form. So i could use Me!Insert_Users rather than Forms!Insert_Users?

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Instead of using:
    Forms!Insert_User.LastName
    ...you could be using...
    Me.LastName

  11. #11
    Mazdaspeed6 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    14
    Quote Originally Posted by RuralGuy View Post
    Instead of using:
    Forms!Insert_User.LastName
    ...you could be using...
    Me.LastName
    Ok, thanks.

  12. #12
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by RuralGuy View Post
    The string continuation sequence is " & _" so:
    query = "INSERT INTO Users(...)" & _
    "V
    ALUES(...." & _
    "....);
    ...would work.
    Allan,

    This is missing a space, thus syntax error. Does it work for you? It doesn't for me. At least not in the immediate window. I'm curious.

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You're right Adam, it needs a space.

  14. #14
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by RuralGuy View Post
    You're right Adam, it needs a space.
    Okies! Thought I may have learned something new, but I'll just keep a truckin.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Creating Multiple Lines per Member
    By SecretGeek in forum Access
    Replies: 2
    Last Post: 08-20-2010, 06:19 AM
  2. Replies: 4
    Last Post: 08-16-2010, 10:46 AM
  3. Multiple Criteria in a string
    By cksm4 in forum Programming
    Replies: 3
    Last Post: 08-04-2010, 11:54 AM
  4. Multiple lines per record: Report?
    By cjbuechler in forum Queries
    Replies: 7
    Last Post: 07-30-2009, 08:32 AM
  5. Creating a string from all records in a table
    By ocordero in forum Programming
    Replies: 2
    Last Post: 08-07-2006, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums