Results 1 to 14 of 14

Creating a String in VBA over multiple lines

  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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    The string continuation sequence is " & _" so:
    query = "INSERT INTO Users(...)" & _
    "VALUES(...." & _
    "....);
    ...would work.
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    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?
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    Is this string being constructed on the Insert_User form?
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    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.
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    Instead of using:
    Forms!Insert_User.LastName
    ...you could be using...
    Me.LastName
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  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 Banned
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,696
    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 online now Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    10,907
    You're right Adam, it needs a space.
    (RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 Pro - acXP, ac07, ac10, ac13
    If your issue is resolved...go ahead and use the Thread Tools at the top of the thread and mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  14. #14
    ajetrumpet is offline Banned
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,696
    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, 05:19 AM
  2. Replies: 4
    Last Post: 08-16-2010, 09:46 AM
  3. Multiple Criteria in a string
    By cksm4 in forum Programming
    Replies: 3
    Last Post: 08-04-2010, 10:54 AM
  4. Multiple lines per record: Report?
    By cjbuechler in forum Queries
    Replies: 7
    Last Post: 07-30-2009, 07:32 AM
  5. Creating a string from all records in a table
    By ocordero in forum Programming
    Replies: 2
    Last Post: 08-07-2006, 08: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
  •  
Tech Forums: Microsoft Office Forums