Results 1 to 6 of 6
  1. #1
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544

    Question Set column name with variable

    I am running some update SQL and I need to have the field that is being updated contingent on my variable. Is there a way to do such a thing? Thanks in advance for your help.

    Code:
    dim vDivisionNum
    vDivisionNum = Column1
    
    mySQL = "UPDATE tblDivBreakdown SET [vDivisionNum]=52 WHERE [FirstName]='Martin'  "AND [LastName]='Green';"
    CurrentDb.Execute mySQL, dbFailOnError


  2. #2
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Code:
    DECLARE @sql nvarchar(1000)
    SET @sql = 'SELECT ' + @columnName + ' FROM Table1'
    EXEC(@sql)
    Code:
    DECLARE @SQL nvarchar(1000)
    SELECT @SQL = 'UPDATE Table SET ' + @field + ' = ''0'' WHERE AnotherField = "" 
    EXEC(@SQL)
    Is that the kind of thing you want to do?

    If so, i'm not sure thats always a good idea.

  3. #3
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    What I'm doing is kind of conveluted and I'll be the first to admit. I believe it's a bit too complicated to describe in text. Essentially I am obtaining data from one source and I need to append it to the table. The field names are in a standard format and really the only way to get the source in there is to define the field name in my "for...each" loop and then dump the value into the specific field.

    I'm not familiar with SQL in VBA so I'm not sure I understand what you gave me until I play around with it. I want to take the value of the variable vDivisionName in my example and use that for the Column/Field name in my UPDATE SQL.

  4. #4
    Rixxe is offline Blessed Bear
    Windows 7 Access 2007
    Join Date
    Sep 2010
    Location
    London (Essex ... yep!)
    Posts
    184
    Quote Originally Posted by jgelpi16 View Post
    What I'm doing is kind of conveluted and I'll be the first to admit. I believe it's a bit too complicated to describe in text. Essentially I am obtaining data from one source and I need to append it to the table. The field names are in a standard format and really the only way to get the source in there is to define the field name in my "for...each" loop and then dump the value into the specific field.

    I'm not familiar with SQL in VBA so I'm not sure I understand what you gave me until I play around with it. I want to take the value of the variable vDivisionName in my example and use that for the Column/Field name in my UPDATE SQL.
    To use a value of a field and declare it as a field name is possible, However my lack of knowledge with VBA means i'm unable to help you with that side of things.

  5. #5
    jgelpi16 is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544
    Thanks for your help. I have the below SQL that does not work. I have a table with the field with the same exact name. I have the table values defaulted to 0. I don't receive any errors when I step through the code. Any ideas as to why it isn't working?

    Code:
        mySQL = "UPDATE tblDivBreakdown SET [HSLC00_Div5]=" & vFieldVal & " WHERE [BegWk]=#" & vBegWk & "#"
        CurrentDb.Execute mySQL, dbFailOnError

  6. #6
    SteveF is offline Generally AccessAble
    Windows XP Access 2010 (version 14.0)
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Quote Originally Posted by jgelpi16 View Post
    Thanks for your help. I have the below SQL that does not work. I have a table with the field with the same exact name. I have the table values defaulted to 0. I don't receive any errors when I step through the code. Any ideas as to why it isn't working?

    Code:
        mySQL = "UPDATE tblDivBreakdown SET [HSLC00_Div5]=" & vFieldVal & " WHERE [BegWk]=#" & vBegWk & "#"
        CurrentDb.Execute mySQL, dbFailOnError
    What's the data type of [HSLC00_Div5]? If it's a text/string data type, you need to enclose it in quotes:

    mySQL = "UPDATE tblDivBreakdown SET [HSLC00_Div5]=" & chr(34) & vFieldVal & chr(34) & " WHERE [BegWk]=#" & vBegWk & "#;"

    Also, don't forget the semicolon at the end -- that's how SQL knows that a multi-line statement has been completed and can be executed.

    Steve

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

Similar Threads

  1. Replies: 4
    Last Post: 08-05-2010, 01:26 PM
  2. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM
  3. Replies: 7
    Last Post: 04-27-2010, 02:47 PM
  4. Replies: 1
    Last Post: 04-15-2010, 02:07 AM
  5. inserting values in column based another column
    By wasim_sono in forum Database Design
    Replies: 1
    Last Post: 06-27-2006, 05:23 AM

Tags for this Thread

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