Results 1 to 2 of 2
  1. #1
    jtkjames is offline Novice
    Windows XP Access 2002
    Join Date
    Jul 2010
    Posts
    9

    Not sure why textbox not working for some values

    I have the following VB code for a button that adds a new field, named after a textbox's value, then populates it with today's date in YYYY-MM-DD format for all entries

    Code:
     
    DoCmd.RunSQL "ALTER TABLE tblMyTable ADD " & Text1.Value & " CHAR NULL"
     
    dat$ = Format(Now(), "yyyy-mm-dd")
     
    DoCmd.RunSQL "UPDATE tblMyTable SET " & Text1.Value & " = " & dat$ & " ;"
    which runs fine for any value of text1 with just letters and numbers. But if I add a space, or other character, it gives me an error "syntax error in field definition".



    any ideas why? im still a bit of an access n00b so any help would be greatly appreciated. thanks!

  2. #2
    jtkjames is offline Novice
    Windows XP Access 2002
    Join Date
    Jul 2010
    Posts
    9
    Well i've solved it, so I thought I would share the solution in case anyone else has a similar problem - just by adding square brackets either side of the textbox value bits:

    Code:
    DoCmd.RunSQL "ALTER TABLE tblMyTable ADD [" & Text1.Value & "] CHAR NULL"
     
    dat$ = Format(Now(), "yyyy-mm-dd")
     
    DoCmd.RunSQL "UPDATE tblMyTable SET [" & Text1.Value & "] = " & dat$ & " ;"
    Last edited by jtkjames; 07-23-2010 at 05:08 AM.

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

Similar Threads

  1. Replies: 3
    Last Post: 04-04-2010, 05:26 PM
  2. Replies: 1
    Last Post: 03-27-2010, 06:13 AM
  3. Lookup from Textbox
    By mastromb in forum Forms
    Replies: 4
    Last Post: 01-02-2010, 07:59 PM
  4. Replies: 1
    Last Post: 03-25-2009, 02:20 PM
  5. Textbox will not allow input
    By cwf in forum Forms
    Replies: 0
    Last Post: 04-04-2008, 04:08 PM

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