Results 1 to 7 of 7

Thread: SQL beginner question... [Solved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    SQL beginner question... [Solved]

    Can you insert SQL code directly into a Access Module and if so can someone provide me with a example statement to update a blank field with a variable?

    Thanks for any help I am pulling my hair out. :P
    Last edited by Besoup; Feb 10th, 2005 at 08:52 AM. Reason: solved

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: SQL beginner question...

    VB Code:
    1. Dim dbsCurrent As Database
    2. Dim qryTest As QueryDef
    3.  
    4. Set dbsCurrent = CurrentDb
    5. Set qryTest = dbsCurrent.QueryDefs("UpdateQuery")
    6. qryTest.SQL = "UPDATE [TestTable] SET [TestTable].[Field1] = "test"
    7. WHERE ((([TestTable].[Field1])=""));"

    This is pretty much what I want to do but I would like to replace the "test" with a variable if that is at all possible. Can anyone help out with this? Thanks in advance

  3. #3
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Re: SQL beginner question...

    You'll have to set your variable somewhere, through user input or however you're filling it, then try this...


    VB Code:
    1. "UPDATE [TestTable] SET [TestTable].[Field1] = '" & test & "' WHERE ((([TestTable].[Field1])=""));"

    Note - you don't need the single quotes before and after the variable if the variable is numeric, only need them for text.
    Here's to us!
    Who's like us?
    Darned few, and they're all dead!

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: SQL beginner question...

    VB Code:
    1. Dim dbsCurrent As Database
    2. Dim qryTest As QueryDef
    3. Dim stTest as string
    4.  
    5. stTest = "what I want"
    6.  
    7. Set dbsCurrent = CurrentDb
    8. Set qryTest = dbsCurrent.QueryDefs("UpdateQuery")
    9. qryTest.SQL = "UPDATE [TestTable] SET [TestTable].[Field1] = " & stTest & _ "WHERE ((([TestTable].[Field1])=""));"

    Tried this with the single quotes and without and I keep getting a syntax error.... beginning to think its impossible =/

  5. #5
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Re: SQL beginner question...

    VB Code:
    1. qryTest.SQL = "UPDATE [TestTable] SET [TestTable].[Field1] = '" & stTest & "' WHERE ((([TestTable].[Field1]) Is Null));"

    Keep it all on the same line, your line continuation is throwing it off, and remove the quotes where you're looking for the empty field, us is null instead.
    Here's to us!
    Who's like us?
    Darned few, and they're all dead!

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: SQL beginner question...

    Will try that when I get to work in the morning... gawd I hope it works. Thanks for the help

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: SQL beginner question...

    Quote Originally Posted by demotivater
    VB Code:
    1. qryTest.SQL = "UPDATE [TestTable] SET [TestTable].[Field1] = '" & stTest & "' WHERE ((([TestTable].[Field1]) Is Null));"

    Keep it all on the same line, your line continuation is throwing it off, and remove the quotes where you're looking for the empty field, us is null instead.

    Thanks alot man got it running was a big help.

Posting Permissions

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



Click Here to Expand Forum to Full Width