Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building SQL String - confusion

Status
Not open for further replies.

PatriciaShoe

Technical User
Mar 23, 2008
9
US
Good AM:

I have struggling with constructing a string to execute some SQL. Here is my update statement



update teacherresources set [student_enroll] = (([student_enroll] / variable1) * Variable 2 )) where(( trschool_id = 12 and [tryear] = forms!form1!yearcombo) or (where trschool_id = 13 and [tryear = forms!form!yearcombo))

the variables are both numeric. I know the variables have to be converted to strings, i.e Crst().

If anyone knows of a good tutorial on this subject I would live to see it. I am lost in a sea of quotes and data mismatches. Thanks much,

Patti
 
Just looking quicly as I'm on my way out

Code:
(([student_enroll] / variable1) * Variable 2 ))

One to many close brackets

Code:
(( trschool_id = 12 and [tryear] = forms!form1!yearcombo) or (where trschool_id = 13 and [tryear = forms!form!yearcombo))

second [tryear] has no closing ]

 
Try
Code:
UPDATE teacherresources SET teacherresources.student_enroll = ((teacherresources.studentenroll / variable1) * Variable2)
WHERE ((((teacherresources.trschool_id) = 12) AND ((teacherresources.tryear) = '" & forms!form1!yearcombo))
OR (((teacherresources.trschool_id) = 13) AND ((teacherresources.tryear) = '" & forms!form!yearcombo)));
 
If using the VBA IDE Try
Code:
codedb.Execute "UPDATE teacherresources SET " _
             & "teacherresources.student_enroll = ((teacherresources.studentenroll / variable1) * Variable2) " _
             & "WHERE ((((teacherresources.trschool_id) = 12) AND ((teacherresources.tryear) = '" & forms!form1!yearcombo)) " _
             & "OR (((teacherresources.trschool_id) = 13) AND ((teacherresources.tryear) = '" & forms!form!yearcombo)))",dbfailonerror
 
I suspect tryear is numeric, so I would try:

Code:
strSQL="UPDATE teacherresources " _
& "SET [student_enroll] = ([student_enroll] / variable1) * Variable 2 " _
& "WHERE (trschool_id = 12 OR trschool_id = 13) " _
& "AND [tryear] = " & forms!form1!yearcombo)
 
How are ya PatriciaShoe . . .

As you learn to write SQL, learn how to format your SQL for easiest reading as well! Example:
Code:
[blue]   Dim SQL As String, valCBx As Long
   
   valCBx = Forms!Form1!YearCombo.Column([purple][b]?[/b][/purple])
   
   SQL = "UPDATE TeacherResources " & _
         "SET [student_enroll] = [student_enroll] * V2/ V1) " & _
         "WHERE (([trSchool_ID] = 12 AND [trYear] = " & valCBx & ") OR " & _
                "([trSchool_ID] = 13 AND [tryear] = " & valCBx & "));"[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
It is generally best to build your own SQL once you get the hang of it, rather than relying on the query window. For example the query window frequently includes far too many parentheses or uses multiple AND / OR lines where fewer lines would be clearer.
 
Thanks for all the quick responses. One problem, I need to pass than value of the variable into the string. In this case it is numeric. When I use the immediate window I would expect to see the numerics that will be part of the string. Otherwise, I get prompted for the value of the variables when I run the sql. Am I missing something?

Patti
 
Possibly.

What is the result of
?strSQL
In the immediate window?
 
Using a simple exmaple, the result in the IW looks like this

UPDATE TeacherResources SET [student_enroll] = v2

The value of v2 = 25 so I would expect that the update statement should look like:

UPDATE TeacherResources SET [student_enroll] = 25

Otherwise I am prompted for the value of the variable. HOw about cstr(v2) ?

Thanks - Patti
 
Try:

Code:
strSQL="UPDATE teacherresources " _
& "SET [student_enroll] = ([student_enroll] / " _
& variable1 & ") * " & Variable2 _
& "WHERE (trschool_id = 12 OR trschool_id = 13) " _
& "AND [tryear] = " & forms!form1!yearcombo)

It should not be necessary to convert the variable to anything, because adding it is to be added to a string.
 
PatriciaShoe . . .

Again attempting a more readable SQL, we have:
Code:
[blue]   Dim SQL As String, valCBx As Long, Eq As String
   
   valCBx = Forms!Form1!YearCombo.Column(?)
   [purple][b]Eq[/b][/purple] = "[student_enroll]*" & V2 & "/" & V1 & ") "
   
   SQL = "UPDATE TeacherResources " & _
         "SET [student_enroll] = " & [purple][b]Eq[/b][/purple] & _
         "WHERE (([trSchool_ID] = 12 AND [trYear] = " & valCBx & ") OR " & _
                "([trSchool_ID] = 13 AND [tryear] = " & valCBx & "));"[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Thank you all. I finally got it working and learned a great deal as well. Many thanks,Patti
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top