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!

Problem passing variables SQL query

Status
Not open for further replies.

pjammer1

Technical User
Jan 18, 2010
8
US
USING VB6 AND ACCESS 2002 (Sorry Old Stuff)

I am trying to pass 3 variables into an SQL query. I get error: "No value give for one or more required parameters."
There seems to be a problem with the SQL syntax although it compiles perfectly. The SQL statement executes with this:


strSQL = "SELECT * FROM SCHEDULE Where WEEK =" & intWeek & " AND YEAR =" & intYear

But when adding the third variable with AND I get the error message as stated :

strSQL = "SELECT * FROM SCHEDULE Where WEEK =" & intWeek & " AND YEAR =" & intYear & " AND SEASON =" & strSeason

The variable name SEASON matches the correct field name.

Here is the VB code:

Private Sub cmdSubmit_Click()
Dim intYear As Integer
Dim intWeek As Integer
Dim strSeason As String
Dim strSQL As String


If optRegSeason.Value = True Then
strSeason = "R"
Else
strSeason = "P"
End If

intYear = Combo1.Text
intWeek = Combo2.Text

Dim db As Database, rs As Recordset
Set db = OpenDatabase("C:\Documents and Settings\G'Day Mate\My Documents\WASUP.mdb")
Set rs = db.OpenRecordset("SCHEDULE", dbOpenDynaset)
On Error Resume Next

strSQL = "SELECT * FROM SCHEDULE Where WEEK =" & intWeek & " AND YEAR =" & intYear & " AND SEASON =" & strSeason
Adodc1.RecordSource = strSQL
Adodc1.Refresh

Any help would be appreciated.

Gary

 
How are ya pjammer1 . . .

Strings need to go inside quotes:
Code:
[blue]strSQL = "SELECT * FROM SCHEDULE Where [red][b]([/b][/red]WEEK =" & intWeek & " AND YEAR =" & intYear & " AND SEASON = [red][b]'[/b][/red]" & strSeason & "[red][b]'[/b][/red]"[red][b])[/b][/red][/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
And keep yourself on the safe side by changing
Code:
YEAR =

to

Code:
[YEAR] =

Even better, if possible, change the name of the field. "Year" is a reserved word (a function)

HTH

[pipe]
Daniel Vlas
Systems Consultant

 
Sorry for the belated reply. Forgot to bookmark this site and could not remember where I had posted. The string variable in quotes worked! Thanks so much for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top