PDA

View Full Version : Solved: Variables into an SQL?



mud2
01-05-2007, 05:24 PM
I'm 81 years old, and

I only have a few hairs left!
I want to transfer variables into an SQL...Here is a simple example;

Private Sub Start()
Dim This_Comment As String
This_Comment = Inputbox("Enter a comment")
Call Insert_This(This_Comment)
End sub

Private Sub Insert_This(This_Comment)
Dim Insert_Sql As String
Insert_Sql = "Insert INTO Table1(Comments) " _
& "Values(This_Comment);"
End Sub

OK, the thing runs, asks for and gets a comment, then a box pops up asking for This_Comment.
No matter how many combinations of
This_Comment = Chr(34) & This_Comment & Chr(34), with or without
Chr(39)s I use, still the same request for This_Comment. I've even used the MID() function to strip the quotes from This Comment, but ubfortunately MID requires a string!

XLGibbs
01-05-2007, 05:59 PM
Insert_Sql = "Insert INTO Table1(Comments) " _
& "Values(" & This_Comment & ");"

You have to put your variable outside the quotes.

You can check the SQL by using

MsgBox Insert_SQL

during testing

mud2
01-05-2007, 06:18 PM
Thank you XLGibbs! But it doesn't work!
" & Variable & " NOGO,
'" & Variable & "' Does go!
For some reason, when I tried '" & ..... & "', VBA interpreted the first ' as the start of a comment! Now it doesn't?!! That's why I had tried
Chr(39)'s!
Oh well, bald is beautiful!

XLGibbs
01-05-2007, 06:21 PM
Oh yeah, I forgot about adding the carots to encapsulate the string so SQL would see it as such.

Nice job figuring out my mistake!

(And I spend about 35 hours a week writing SQL, doh!)