Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Syntax for passing control value to SQL in Access VBA

Posted on 2008-10-14
12
Medium Priority
?
553 Views
Last Modified: 2013-11-27
Hi Experts,

I have an update query that runs an update via ADODB command, but I don't know how to pass the value of the form control to the SQL string. Here is what I have:

Private Sub Form_Close()

Dim myStr, strGroupSQL As String
Dim myRad24, myRad16, myRad4 As Boolean
Dim cmdCommand As ADODB.Command


Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = CurrentProject.Connection

myRad24 = Forms!frmLabels!radio24
myRad16 = Forms!frmLabels!radio16
myRad4 = Forms!frmLabels!radio4

strGroupSQL = "UPDATE Tbl_Persistent SET Radio24 = & myRad24 & "

cmdCommand.CommandText = strGroupSQL
cmdCommand.Execute

End Sub

I know the issue is with the syntax for the control, but I don't know where to find the "rules" for how to treat the control to pass it in.

THanks!

Kevin
0
Comment
Question by:k_smee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 

Author Comment

by:k_smee
ID: 22711833
OK..I got this to work by changing this:
strGroupSQL = "UPDATE Tbl_Persistent SET Radio24 =  " & myRad24 & ""

The variable myRad now has a double quote, ampersand on both sides, but it would still benice to know how the rules.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22711919
Since you are building an inline SQL statement, you need to escape any special characters.

So if you want your update statement to be:

update tbl_persistent set radio24 = "Tommy Boy"

where Tommy Boy is the value of forms!frmlabels!radio24, then that value can contain no special characters that would terminate the SQL.  So if the value were Tommy "AKA Tom" Boy, your resultant SQL statement would be:

update tbl_persistent set radio24 = "Tommy "AKA Tom" Boy"

The problem is that the " before AKA stops the string you are setting radio24 equal to and you will get an invalid syntax near AKA error.  To solve this, you can replace any single quotes with double quotes. by doing the following:


myRad24 = replace(Forms!frmLabels!radio24,"""", """""")
myRad16 = replaceForms!frmLabels!radio16 ,"""", """""")
myRad4 = replace(Forms!frmLabels!radio4 ,"""", """""")


Now this may look a bit confusing because there are more than one " in the lines, but even in this case, they need to be escaped.

so inside of the " " that creates a string, a "" represents the value of ".  So """" represents "", which will escape down to " once you execute strGroupSQL.
0
 

Author Comment

by:k_smee
ID: 22712077
My problem is I need to be able to dyanmically pass whatever value is in Forms!frmLabels!radio24. So I couldn't set a literal value like "Tommy Boy".

I know there are different syntax for passing in the value of different data types - this one is a boolean.
I just did trial and error. I don't know how to do it again.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22712219
What kind of value is the Field Radio24 in the Database?  If it is Text field, then the val7ue to be passed must be enclosed in quote marks '...'.  If it a numeric field, then you do not use the enclosing quote marks.
0
 

Author Comment

by:k_smee
ID: 22712248
The field Radio24 is set "Yes/No". So that would be a boolean, correct?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22712290
I did say:
"So if you want your update statement to be:"

as arthur pointed out in http:#22712219, the use of quotes is dependent upon the data type of the field.  For example, datetime fields I believe need to be wrapped in # for access.
0
 

Author Comment

by:k_smee
ID: 22712384
Gotcha, Brandon. I guess my main question at this point is there some guide to how to treat a variable like myRad24? So for a boolean this worked:

" & myRad24 & ""

For a date this might work:

# & myRad24 & #"

For the rest. I don't know...
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 120 total points
ID: 22712409
For a Number (integer, double, any number type), not extra quotes:
" & myRad24
 
for dates:
#" & MyDate & "#"
0
 

Author Comment

by:k_smee
ID: 22712473
What if it is set to String?
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 80 total points
ID: 22712722
0
 

Author Closing Comment

by:k_smee
ID: 31505904
Thanks guys!
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 22713519
Glad to be of assistance
AW
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question